作者a128374045 (WWI)
看板ASM
标题[问题] 问Arduino
时间Fri Dec 14 22:25:34 2018
大家好 我现在在做红外线循迹的程式,现在我们遇到一个问题。就是我们遇到直角时要
先原地旋转,之後在平移回来。所以我想要用if的功能写就是平移到感应到黑线停下来,
可是我不会写,希望有人可以教我怎麽写?或是提点一下怎麽写?下面是我的程式
const int negR=4;
const int posR=5;
const int negL=6;
const int posL=7;
const int pwmR=9;
const int pwmL=10;
const int irD1=A1;
const int irD2=A2;
const int irD3=A3;
const int Rspeed=200;
const int Lspeed=200;
byte IRstatus=0;
void setup()
{
pinMode(posR,OUTPUT);
pinMode(negR,OUTPUT);
pinMode(posL,OUTPUT);
pinMode(negL,OUTPUT);
pinMode(irD1,INPUT_PULLUP);
pinMode(irD2,INPUT_PULLUP);
pinMode(irD3,INPUT_PULLUP);
}
void loop()
{
int val;
IRstatus=0;
val=analogRead(irD1);
if(val>=150)
IRstatus=(IRstatus+4);
val=analogRead(irD2);
if(val>=150)
IRstatus=(IRstatus+2);
val=analogRead(irD3);
if(val>=150)
IRstatus=(IRstatus+1);
driveMotor(IRstatus);
}
void driveMotor(byte IRstatus)
{
switch(IRstatus)
{
case 0:
forward(Rspeed,Lspeed);
break;
case 1:
right(Rspeed,Lspeed);
break;
case 2:
forward(Rspeed,Lspeed);
break;
case 3:
right(Rspeed,Lspeed);
break;
case 4:
left(Rspeed,Lspeed);
break;
case 5:
pause(0,0);
break;
case 6:
left(Rspeed,Lspeed);
break;
case 7:
pause(0,0);
break;
}
}
void forward(byte RmotorSpeed, byte LmotorSpeed)
{
analogWrite(pwmR,RmotorSpeed);
analogWrite(pwmL,LmotorSpeed);
digitalWrite(posR,HIGH);
digitalWrite(negR,LOW);
digitalWrite(posL,LOW);
digitalWrite(negL,HIGH);
}
void back(byte RmotorSpeed, byte LmotorSpeed)
{
analogWrite(pwmR,RmotorSpeed);
analogWrite(pwmL,LmotorSpeed);
digitalWrite(posR,LOW);
digitalWrite(negR,HIGH);
digitalWrite(posL,HIGH);
digitalWrite(negL,LOW);
}
void pause(byte RmotorSpeed, byte LmotorSpeed)
{
analogWrite(pwmR,RmotorSpeed);
analogWrite(pwmL,LmotorSpeed);
digitalWrite(posR,LOW);
digitalWrite(negR,LOW);
digitalWrite(posL,LOW);
digitalWrite(negL,LOW);
}
void right(byte RmotorSpeed, byte LmotorSpeed)
{
analogWrite(pwmR,RmotorSpeed);
analogWrite(pwmL,LmotorSpeed);
digitalWrite(posR,LOW);
digitalWrite(negR,HIGH);
digitalWrite(posL,LOW);
digitalWrite(negL,HIGH);
}
void left(byte RmotorSpeed, byte LmotorSpeed)
{
analogWrite(pwmR,RmotorSpeed);
analogWrite(pwmL,LmotorSpeed);
digitalWrite(posR,HIGH);
digitalWrite(negR,LOW);
digitalWrite(posL,HIGH);
digitalWrite(negL,LOW);
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 27.246.34.187
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/ASM/M.1544797536.A.BA6.html
1F:→ swich45314: 看起来是要多一组动作模式和知道那种情况的senser值 12/24 01:51
2F:→ swich45314: 来判断 12/24 01:51
3F:推 johnpage: 多装几组感应器 12/24 11:55