[問題] 問Arduino

作者: a128374045 (WWI)   2018-12-14 22:25:34
大家好 我現在在做紅外線循跡的程式,現在我們遇到一個問題。就是我們遇到直角時要
先原地旋轉,之後在平移回來。所以我想要用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);
}
作者: swich45314 (swich)   2018-12-24 01:51:00
看起來是要多一組動作模式和知道那種情況的senser值來判斷
作者: johnpage (johnpage)   2018-12-24 11:55:00
多裝幾組感應器

Links booklink

Contact Us: admin [ a t ] ucptt.com