You Reply: Thank you to everyone that offered advice and code. I made some slight modifications to add right and left turns, as well as longer stop times. Everything is running fine and as expected. Now this is for the HCR platform with Romeo All in One from DFrobots. It has 3 bumper switchs and 5 IR sensors. Could someone offer advice on adding these sensors to the code? Example:
Robot moves forward and bumps into a wall. Stop, back up for 2 seconds, turn right for 3 seconds, continue with loop. I know how to stop and turn. I don't know the code to trigger the correction.
With the sensors it is similar but avoidence at 3 or 4 inches from wall.
Here is my sketch it is working great:
[code]int E1 = 5;
int E2 = 6;
int M1 = 4;
int M2 = 7;
void forward(char a,char b) //Move forward
{
analogWrite(E1,a);
digitalWrite(M1,HIGH);
analogWrite(E2,b);
digitalWrite(M2,HIGH);
}
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void backward (char a,char b) //Move backward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void Left (char a,char b) //Turn left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void Right (char a,char b) //Turn right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void setup()
{
for(int i=4;i<=7;i++)
pinMode(i, OUTPUT);
}
void loop()
{
forward(100,100); //Forward at 50% full speed
delay(10000); //delay for 10 seconds
stop(); //Stop
delay(600000); //delay for 60 seconds
backward(100,100); //backward at 50% full speed
delay(10000); //delay for 10 seconds
stop(); //Stop
delay(600000); //delay for 60 seconds
Left(100,100); //Left at 50% full speed
delay(10000); //delay for 10 seconds
stop(); //Stop
delay(600000); //delay for 60 seconds
Right(100,100); //Left at 50% full speed
delay(10000); //delay for 10 seconds
stop(); //Stop
delay(600000); //delay for 60 seconds
}
[/code]