[問題] 問Arduino
大家好 我現在在做紅外線循跡的程式,現在我們遇到一個問題。就是我們遇到直角時要
先原地旋轉,之後在平移回來。所以我想要用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://www.ptt.cc/bbs/ASM/M.1544797536.A.BA6.html
→
12/24 01:51,
6年前
, 1F
12/24 01:51, 1F
→
12/24 01:51,
6年前
, 2F
12/24 01:51, 2F
推
12/24 11:55,
6年前
, 3F
12/24 11:55, 3F
ASM 近期熱門文章
PTT數位生活區 即時熱門文章