[请问] 问Arduino

楼主: a128374045 (WWI)   2018-12-14 21:23:31
大家好 我现在在做红外线循迹的程式,现在我们遇到一个问题。就是我们遇到直角时要
先原地旋转,之后在平移回来。所以我想要用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);
}

Links booklink

Contact Us: admin [ a t ] ucptt.com