Dear Sirs,
I use Arduino DUE, five sensors, and 32 styles to drive my motor. However, the motor did not follow my program setting. For example, case 0 (sensor styles are all 0), motor should be frozen in my program setting, but it did not. The motor ran at full speed instead. Have my code any question? Please help me to find out how to fix it.
this code control AGV Wheel
const int IRPeople =10; //Infrared sensor in front of obstacle avoidance
const int SLeft_1 = 5; //Infrared two signal input to the left direction
const int SLeft = 6; //An infrared signal input to the left direction
const int SMiddle = 7; //Intermediate direction infrared signal input
const int SRight = 11; //An infrared signal input the right direction
const int SRight_1 = 12; //Infrared two signal input the right direction
const int PeopleLED = 13; //
const int Motor_Left = 9;
const int Motor_Right = 8;
const int Motor_M1 = DAC0; //right
const int Motor_M2 = DAC1; //left
byte byteSensorStatus = 0;
void setup(){
Serial.begin(9600);
pinMode(IRPeople,INPUT); //Infrared sensor in front of obstacle avoidance
pinMode(SLeft_1,INPUT);
pinMode(SLeft,INPUT);
pinMode(SMiddle,INPUT);
pinMode(SRight,INPUT);
pinMode(SRight_1,INPUT);
pinMode(Motor_M1,OUTPUT);
pinMode(Motor_M2,OUTPUT);
pinMode(Motor_Left,OUTPUT);
pinMode(Motor_Right,OUTPUT);
pinMode(PeopleLED,OUTPUT);
delay(5000);
}
void loop(){
int nIRStatus;
byteSensorStatus = 0;
nIRStatus = digitalRead(IRPeople);
if(nIRStatus == 0){
motorstop(0,0);
delay(5000);
}
else{
nIRStatus = digitalRead(SLeft_1);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 4));
nIRStatus = digitalRead(SLeft);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 3));
nIRStatus = digitalRead(SMiddle);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 2));
nIRStatus = digitalRead(SRight);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 1));
nIRStatus = digitalRead(SRight_1);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | 0x01);
drivemotor(byteSensorStatus);
}
}
void drivemotor(byte nStatus)
{
switch(nStatus)
{
case 31:// SL1:1 SL:1 SM:1 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 30:// SL1:1 SL:1 SM:1 SR:1 SR1:0//??????
motorstop(0,0);
break;
case 29:// SL1:1 SL:1 SM:1 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 28:// SL1:1 SL:1 SM:1 SR:0 SR1:0//??????
right(0,0);
break;
case 27:// SL1:1 SL:1 SM:0 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 26:// SL1:1 SL:1 SM:0 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 25:// SL1:1 SL:1 SM:0 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 24:// SL1:1 SL:1 SM:0 SR:0 SR1:0//??????
slowright_1(0,0);
break;
case 23:// SL1:1 SL:0 SM:1 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 22:// SL1:1 SL:0 SM:1 SR:1 SR1:0//??????
motorstop(0,0);
break;
case 21:// SL1:1 SL:0 SM:1 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 20:// SL1:1 SL:0 SM:1 SR:0 SR1:0//??????
motorstop(0,0);
break;
case 19:// SL1:1 SL:0 SM:0 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 18:// SL1:1 SL:0 SM:0 SR:1 SR1:0//??????
motorstop(0,0);
break;
case 17:// SL1:1 SL:0 SM:0 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 16:// SL1:1 SL:0 SM:0 SR:0 SR1:0//??????
slowright_1(0,0);
break;
case 15:// SL1:0 SL:1 SM:1 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 14:// SL1:0 SL:1 SM:1 SR:1 SR1:0//??????
forward(0,130);
break;
case 13:// SL1:0 SL:1 SM:1 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 12:// SL1:0 SL:1 SM:1 SR:0 SR1:0//??????
slowright(0,0);
break;
case 11:// SL1:0 SL:1 SM:0 SR:1 SR1:1//??????
motorstop(0,0);
break;
case 10:// SL1:0 SL:1 SM:0 SR:1 SR1:0//??????
motorstop(0,0);
break;
case 9:// SL1:0 SL:1 SM:0 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 8:// SL1:0 SL:1 SM:0 SR:0 SR1:0//??????
slowright(0,0);
break;
case 7:// SL1:0 SL:0 SM:1 SR:1 SR1:1//??????
left(0,0);
break;
case 6:// SL1:0 SL:0 SM:1 SR:1 SR1:0//??????
slowleft(0,0);
break;
case 5:// SL1:0 SL:0 SM:1 SR:0 SR1:1//??????
motorstop(0,0);
break;
case 4:// SL1:0 SL:0 SM:1 SR:0 SR1:0//??????
forward(0,130);
break;
case 3:// SL1:0 SL:0 SM:0 SR:1 SR1:1//??????
slowleft_1(0,0);
break;
case 2:// SL1:0 SL:0 SM:0 SR:1 SR1:0//??????
slowleft(0,0);
break;
case 1:// SL1:0 SL:0 SM:0 SR:0 SR1:1//??????
slowleft_1(0,0);
break;
case 0:// SL1:0 SL:0 SM:0 SR:0 SR1:0//??????
motorstop(0,0);
break;
}
}
void motorstop(byte flag, byte motorspeed)
{
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,motorspeed);
}
void forward(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,motorspeed);
}
void right(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,70);
}
void slowright(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,70);
}
void slowright_1(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,motorspeed);
analogWrite(Motor_M2,80);
}
void left(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,70);
analogWrite(Motor_M2,motorspeed);
}
void slowleft(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,70);
analogWrite(Motor_M2,motorspeed);
}
void slowleft_1(byte flag, byte motorspeed)
{
digitalWrite(Motor_Left,LOW);
digitalWrite(Motor_Right,LOW);
analogWrite(Motor_M1,80);
analogWrite(Motor_M2,motorspeed);
}