#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo1; //tilt
Servo servo2; //pan
//******Pin assignments, Modify only with recpect to circuit wiring!!************
//Analog Pins
int LDR = 0;
//Digital Pins
int piezo = 22;
int pan = 23;
int tilt = 24;
int RFwd = 25;
int RRvs = 26;
int pingPin = 27;
int LRvs = 28;
int LFwd = 29;
int sonarRight = 30;
int sonarLeft = 31;
int sonarCenterLeft = 32;
int sonarCenterRight = 33;
//******************************************************************************
void setup() {
Serial.begin(9600); // initialize serial communication:
bot_stop(); //stop robot
beep();
start(); //start system by touching the LDR
delay(1000);
servo_home(); //Assign starting sevos
delay(500);
beep(); //ready
}//end Void setup()
//***********************Function PING************************************
long PING(){
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(15);
digitalWrite(pingPin, LOW);
delayMicroseconds(20);
// The same pin is used to read the signal from the PING))): 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.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
// Serial.println(inches);
return inches;
}// end function "void PING"
//***********************Function left_sonar************************************
long left_sonar(){
pinMode(sonarLeft, INPUT);
long rangeLS = pulseIn(sonarLeft,HIGH);
return(rangeLS/147);
}// end function "left_sonar"
//***********************Function center_left_sonar************************************
long center_left_sonar(){
pinMode(sonarCenterLeft, INPUT);
long rangeCLS = pulseIn(sonarCenterLeft,HIGH);
return(rangeCLS/147);
}// end function "Center_left_sonar"
//***********************Function center_right_sonar************************************
long center_right_sonar(){
pinMode(sonarCenterRight, INPUT);
long rangeCRS = pulseIn(sonarCenterRight,HIGH);
return(rangeCRS/147);
}// end function "Center_right_sonar"
//***********************Function right_sonar************************************
long right_sonar(){
pinMode(sonarRight, INPUT);
long rangeRS = pulseIn(sonarRight,HIGH);
return(rangeRS/147);
}// end function "right_sonar"
//***********************Function start************************************
void start(){
int val = analogRead(LDR);
while(val>30){val=analogRead(LDR);
if(val<=30){break;}}//end if
}// end function "beep"
//***********************Function beep************************************
void beep(){
pinMode(piezo, OUTPUT);
digitalWrite(piezo, HIGH);
delay(1000);
digitalWrite(piezo, LOW);
}// end function "beep"
//***********************Function servo_home************************************
void servo_home(){
servo1.attach(tilt);
servo2.attach(pan);
servo1.write(160); //tilt
delay(15);
servo2.write(90); //pan
delay(15);
}// end function "servo_home"
//***********************Function bot_stop************************************
void bot_stop(){
pinMode(LFwd, OUTPUT);
pinMode(LRvs, OUTPUT);
pinMode(RFwd, OUTPUT);
pinMode(RRvs, OUTPUT);
digitalWrite(LFwd, LOW);
digitalWrite(LRvs, LOW);
digitalWrite(RFwd, LOW);
digitalWrite(RRvs, LOW);
}// end function "bot_stop"
//***********************Function bot_fwd************************************
void bot_fwd(){
pinMode(LFwd, OUTPUT);
pinMode(LRvs, OUTPUT);
pinMode(RFwd, OUTPUT);
pinMode(RRvs, OUTPUT);
servo_home();
digitalWrite(LFwd, HIGH);
digitalWrite(LRvs, LOW);
digitalWrite(RFwd, HIGH);
digitalWrite(RRvs, LOW);
}// end function "bot_fwd"
//***********************Function bot_rvs************************************
void bot_rvs(){
pinMode(LFwd, OUTPUT);
pinMode(LRvs, OUTPUT);
pinMode(RFwd, OUTPUT);
pinMode(RRvs, OUTPUT);
servo_home();
digitalWrite(LFwd, LOW);
digitalWrite(LRvs, HIGH);
digitalWrite(RFwd, LOW);
digitalWrite(RRvs, HIGH);
}// end function "bot_RVS"
//***********************Function clear_path************************************
int clear_path(){
int pos2=0;
int pos3=0;
int dir=0;
for(pos2=0; pos2<=180; pos2++) { //scan 180deg
long inches = PING(); //Detect
if(dir<inches){dir=inches;
pos3=pos2;}
if(dir>=inches){dir=dir;}
servo2.write(pos2); // scan
delay(40); // waits 15ms for the servo to reach the position
} //end for(pos2=0; pos2<=180; pos2++)
servo2.write(pos3); // point to the clearest path
delay(40); // waits 15ms for the servo to reach the position
delay(2000);
beep();
beep();
return dir; //return the clearest path
}// end function "clear_path"
//***********************Function object_detected************************************
int object_detect(int safe_zone){
int detected = 0;
long inches_ping = PING(); //Detect
long inches_left_sonar = left_sonar(); //Detect
long inches_right_sonar = right_sonar(); //Detect
long inches_CRS = center_right_sonar(); //Detect
long inches_CLS = center_left_sonar(); //Detect
if(inches_ping<=safe_zone || inches_left_sonar<=safe_zone || inches_right_sonar<=safe_zone || inches_CRS<=safe_zone || inches_CLS<=safe_zone){ //object detected within safe zone?
bot_stop(); //stop robot
detected = 1; //set flag
delay(1000); // Pause for a second
}//end if(inches_ping<=safe_zone || inches_left_sonar<=safe_zone || inches_right_sonar<=safe_zone)
return detected;
}//end object_detect()
//***********************Function good_clear_path()************************************
void good_clear_path(int safe_zone,int path){
while(path<=safe_zone){
servo_home();
delay(3000); // Pause for 3 secondS
bot_rvs();
delay(3000); // reverse for 3 secondS
bot_stop(); //stop robot
int path = clear_path(); //keep checking for clear path and point to it
if(path>safe_zone){break;} //found clear path, get out of loop
}//while(path<=safe_zone)
} // end function good_clear_path()
//***********************Function microsecondsToInches()************************************
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
} //end function long microsecondsToInches()
/*
//***********************Function compas()************************************
int compas()
{
//CODE!!!!!!!!!!!!!!!!!!!!!!!!!
} //end function int compas()
//***********************Function logdata()************************************
int logdata()
{
//CODE!!!!!!!!!!!!!!!!!!!!!!!!!
} //end function int logdata()
//***********************Function getdata()************************************
int getdata()
{
//CODE!!!!!!!!!!!!!!!!!!!!!!!!!
} //end function int getdata()
*/
//***********************Main Code Start******************************
//********************************************************************
//********************************************************************
//********************************************************************
void loop()
{
int safe_zone =7;
bot_fwd(); //Keep going forward
// int obstacal = object_detect(safe_zone); //returns detected=1 , not detected=0, if =1 stops robot
// if(obstacal=1){
//int path = clear_path(); //check for clear path and points to it and beeps
// good_clear_path(safe_zone,path); //checks if the pointed path has any obstacals. if not good, robot reverses and checks for a clearer path
//CODE----turn heading to the clearest path
//CODE----if heading at new path, obstacal=0;
}//end if(obstacal=1)
}//end void loop
//***********************Main Code End********************************
//********************************************************************
//********************************************************************
//********************************************************************