#include <Servo.h>
Servo leftServo;
Servo rightServo;
Servo angleServo;
const int pingPin = 10;
long duration, cm, inches;
void setup() {
// put your setup code here, to run once:
rightServo.attach(12);
leftServo.attach(13);
angleServo.attach(10);
Serial.begin(9600);
}
void loop() {
cm = pingDetect();
if (cm < 15){ // if there is a wall infront of the robot
Stop();
sensorLeft(); // Turn the sensor to the left
cm = pingDetect();
if (cm < 15){
Stop();
sensorRight();
cm = pingDetect();
if (cm < 15){
Stop();
sensorStraight();
cm = pingDetect();}
else {
turnRight();
Stop();
sensorStraight();}
}
else {
turnLeft();
Stop();
sensorStraight();}
}
else {
sensorStraight();
Forward();
}
}
long inchConversion(long microseconds){ // convert time to distance
return microseconds / 74 / 2; }
long cmConversion(long microseconds){ // convert time to distance
return microseconds / 29 / 2;}
void Forward() {
leftServo.write(180);
rightServo.write(0);
delay(160); // moves forward 1 inch
}
void turnLeft() {
leftServo.write(0); // turn to the left
rightServo.write(0);
delay(900);
}
void Stop() {
leftServo.write(95);
rightServo.write(95);
delay(500);}
void turnRight() {
leftServo.write(180); // turn to the right
rightServo.write(180);
delay(900);
}
void sensorStraight(){
angleServo.write(90);
delay(1000);}
void sensorRight(){
angleServo.write(0);
delay(1000);}
void sensorLeft(){
angleServo.write(180);
delay(1000);}
long pingDetect(){
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = inchConversion(duration);
cm = cmConversion(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
return cm; }
-This includes the Servo command library embedded in the Arduino application.
-These declare the "leftServo" or "rightServo" as servos.
-pingPin is the pin attached to the ping sensor.
-Long is a variable used to define an integer and can hold up to 32 bits of information.
-This is where you set up the code, this area runs once.
-Attach the servos to a pin.
-Tells the Arduino to show a value.
-This is where the meat of the code is written. Write the commands here for the robot to execute.
-Reads if there is a wall in front.
-If so, the robot stops, and the sensor turns left
-Detects for another wall. If found, sensor turns to the right and detects again.
-If there is a wall present to the right as well, sensor returns to straight position to start over in case of a miss-read.
-If there is no wall to the right, the robot turns left
-Sensor returns straight
-If no wall to left, robot turns left
-Sensor returns straight
-If no wall is detected, the robot continues forward 1 inch at a time
-Converts time to distance in inches
-Converts time to distance is cm
-Function: move forward
-Function: turn left
-Function: stop
-Function: turn right
-Function: turn sensor straight
-Function: turn sensor right
-Function: turn sensor left
-Funtion: Tells ping sensor to send out a sound signal, then switch to an input. It receives the sound signal back, then tells the Arduino a the time it took the sound to return.