I am using the Arduino board with a Pi-bot. Our challenge is to navigate and obstacle course using the ultrasonic sensor and the line following sensor. The robot has to approach the first object, sense it with the ultrasonic, make a 90 degree left, approach a second object then make 60-90 degree turn right. From there it has to pick up a black line and follow it to the end.
The line following I can figure out. The ultrasonic is where i'm having issues. Right now I have it set up to where it senses and object, backs up, turns slightly to the right then goes forward which is fine once I get to the second object. Before that though I need to sense the first object and make a left. After that I can go back into the regular loop.
This is my first time ever programming so all of this is was very difficult to find and put together
I was thinking of; using a counter, making the first loop while approaching the first obstacle an even integer, and the second loop an odd integer, or some how putting the first loop on a timer. Not really sure how to set up either option.
/* Line Following/ Ultrasonic Program
const int Line1 = 7; // Left Line Sensor
const int Line2 = 8; // Center Line Sensor
const int Line3 = 10; // Right Sensor
const int In1 = 3; // In1 right forward (RF)
const int In2 = 5; // In2 right backward (RB)
const int In3 = 6; // In3 left forward (LF)
const int In4 = 11; // In4 left backward (LB)
const int In5 = 12; // In5 green LED
const int In6 = 13; // In6 red LED
const int OFF = 0; //slow off
const int ON = 100; // fast on
const int ON2 = 115; // fast 2 equalizing forward speed of wheels
const int turn = 150; //faster
const int trigPin = 2; //Aduino pin 2
const int echoPin = 4; //Aduino pin 4
long duration, distance, inches, cm;
const int MaxDistance = 10; //10 cm
//int count=2;
//const int i=1
void setup()
{
// initialize the pins
pinMode(In1, OUTPUT); //RF
pinMode(In2, OUTPUT); //RB
pinMode(In3, OUTPUT); //LF
pinMode(In4, OUTPUT); //LB
pinMode(In5, OUTPUT); //RED
pinMode(In6, OUTPUT); //GREEN
pinMode(Line1, INPUT); //left sensor
pinMode(Line2, INPUT); //center sensor
pinMode(Line3, INPUT); //right sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
} //end of void setup
void loop()
{//beginning of void loop
int L1; //Left
int L2; //Center
int L3; //Right
while(true)
{ //beginning of while loop
ping();
distance = cm;
delay(10);
L1 = digitalRead(Line1); //left sensor
L2 = digitalRead(Line2); //center sensor
L3 = digitalRead(Line3); // right sensor
if (distance < MaxDistance)
{
stopNow();
reverse();
delay(1000);
leftturn();
forward();
}
else
{//beginning of first else loop
if (L1 == 1 & L2 == 0 & L3 == 0)
{
rightturn();//black, white, white. Turn right. Red light on
}
else if (L1==1 & L2==0 & L3==1)
{
reverse(); //black, white, black, robot will reverse. red light on
}
else if (L1 == 0 & L2 == 0 & L3 == 0)
{
forward();//white, white, white,go forward fast,green light on
}
else if (L1 == 0 & L2 == 1 & L3 == 0)
{
reverse();//white, black, white, reverse, red light on
}
else if (L1 == 1 & L2 == 1 & L3 == 1)
{
reverse();//black, black, black, reverse and turn, red light on
delay(500);
rightturn();
}
else if (L1 == 0 & L2 == 0 & L3 == 1)
{
rightturn();//white, white, black, turn left, red light on
}
else if (L1 == 1 & L2 == 1 & L3 == 0)
{
rightturn();//black, black, white, turn right, red light on
}
else if (L1 == 0 & L2 == 1 & L3 == 1)
{
leftturn();//white, black, black, turn left, red light on
}
else
{
forward();
}
delay(3);
return;
}//end of first else loop
}//end of while loop 1 == 1
}// end of void loop
void reverse()
{
analogWrite(In6, In6); //red light on
analogWrite(In5, OFF); //green light off
analogWrite(In4, ON); //left backwards
analogWrite(In3, OFF); //left forwards
analogWrite(In2, ON); //right backwards
analogWrite(In1, OFF); //right forwards
return;
}
void leftturn ()//left turn
{
analogWrite(In6, In6); //red light on
analogWrite(In5, OFF); //green light off
analogWrite(In4, turn); //left backwards
analogWrite(In3, OFF); //left forwards
analogWrite(In2, OFF); //right backwards
analogWrite(In1, turn); //right forwards
return;
}
void rightturn ()//right turn
{
analogWrite(In6, In6); //red light on
analogWrite(In5, OFF); //green light off
analogWrite(In4, OFF); //left backwards
analogWrite(In3, turn); //left forwards
analogWrite(In2, turn); //right backwards
analogWrite(In1, OFF); //right forwards
return;
}
void backturn() // turning right while backing up
{
analogWrite(In6, In6); //red light on
analogWrite(In5, OFF); //green light off
analogWrite(In4, OFF); //left backwards
analogWrite(In3, ON2); //left forwards
analogWrite(In2, ON); //right backwards
analogWrite(In1, OFF); //right forwards
}
void forward ()
{
analogWrite(In6, OFF); //red light off
analogWrite(In5, In5); //green light on
analogWrite(In4, OFF); //left backwards
analogWrite(In3, ON2); //left forwards
analogWrite(In2, OFF); //right backwards
analogWrite(In1, ON); //right forwards
return;
}
void stopNow()
{
analogWrite(In6, In6); //red light on
analogWrite(In5, OFF); //green light on
analogWrite(In4, OFF); //left backwards
analogWrite(In3, OFF); //left forwads
analogWrite(In2, OFF); //right backwards
analogWrite(In1, OFF);//right forwards
return;
}
void ping ()
{
//The sensor is triggered by a HIGH pulse of 10 or more microseconds.
//Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);//Prime the sensor
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);//Actual pulse
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(echoPin, HIGH);
inches = duration / 74 / 2; // convert the time into a distance
cm = duration / 29 /2;
return;
}