Professional Documents
Culture Documents
h>
#include <NewPing.h>
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library to use the correct pins to measure distance.
int pos = 0; // this sets up variables for use in the sketch (code)
int curDist = 0; //Current distance
int maxDist = 0; //Maximum distance from a sweep of pings
int maxPos = 0; //Angle at which maximum distance was detected.
String motorSet = "";
int speedSet = 0;
}
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------MAIN LOOP --------------------------------------------------------------------
void loop() {
checkForward(); // check robot is moving forward
checkPath(); // set ultrasonic sensor to scan for any possible obstacles
}
//--------------------------------------------------------------------------------------------------------------------------------
void checkPath() {
myservo.write(144); // set servo to face left 54-degrees from forward
delay(100); // wait 100ms for servo to reach position
for(pos = 144; pos >= 36; pos-=18) // loop to sweep the servo (& sensor) from 144deg left to 18deg right at 18deg intervals.
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(60); // wait 60ms for servo to get to position
curDist = readPing(); // get the current distance to any object in front of sensor
//Serial.println (curDist);
if (curDist < COLL_DIST) { // if the current distance to object is less than the collision distance
checkCourse(); // run the checkCourse function
break; // jump out of this loop
}
if (curDist < TURN_DIST) { // if current distance is less than the turn distance
changePath(); // run the changePath function
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------
void lookAround() { //have a look around and identify at what angle is the furthest ping. Only run when staionary.
maxDist = 0;
maxPos= 144;
myservo.write(144); // set servo to face left 54-degrees from forward
delay(100); // wait 100ms for servo to reach position
for(pos = 144; pos >= 36; pos-=18) // loop to sweep the servo (& sensor) from 144deg left to 18deg right at 18deg intervals.
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(60); // wait 60ms for servo to get to position
curDist = readPing(); // get the current distance to any object in front of sensor
//Serial.println (curDist);
if (curDist > maxDist) {maxDist = curDist; maxPos=pos;}
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void setCourse() { // have a look around turn towards the furthest object and then move forward
lookAround ();
if (maxPos <= 54) {turnRight();} // turn right as furthest object was to the right
if (maxPos >54 && maxPos < 90) {veerRight();} // veer right as furthest object is forward and slightly right
if (maxPos >= 126) {turnLeft();} // turn left as furthest object was to the left
if (maxPos >= 90 && maxPos < 126) {veerLeft();} //veer left as furthest object is ahead or slightly to the Left
}
//----------------------------------------------------------------------------------------------------------------------------------
void checkCourse() { // we're about to hit something so stop, move back a bit , stop and work out best course
moveStop();
delay(100);
moveBackward();
delay(600); //move backwards for 600ms
moveStop();
setCourse();
}
//----------------------------------------------------------------------------------------------------------------------------------
void changePath() {
if (pos < 90) {veerLeft();} // if current pos of sensor is less than 90deg, then object is on the right hand side so veer left
if (pos >= 90) {veerRight();} // if current pos of sensor is >= than 90deg, then object is on the left hand side or ahead so veer right
}
//----------------------------------------------------------------------------------------------------------------------------------
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's direction BACK
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's direction BACK
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void checkRight() { // Motor A BACK and Motor B FWD
if (motorSet=="RIGHT") {
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's direction BACK
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);//setting motorB's direction FWD
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void checkLeft() { // Motor A FWD and Motor B Back
if (motorSet=="LEFT") {
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's direction FWD
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's direction BACK
}
}
//----------------------------------------------------------------------------------------------------------------------------------
//
// The above functions simply set the L298 IN1,2,3,&4 pins for the required Motor direction
//----------------------------------------------------------------------------------------------------------------------------------
void moveStop() { // stop the motors
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
}
//-----------------------------------------------------------------------------------------------------------------------------------
void moveForward() {
motorSet = "FORWARD";
checkForward();
for (speedSet = 140; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up
{
analogWrite(ENA,speedSet);//start driving motorA
analogWrite(ENB,speedSet+MAX_SPEED_OFFSET);//start driving motorB
delay(5);
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void moveBackward() {
motorSet = "BACKWARD";
checkBackward();
for (speedSet = 140; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up
{
analogWrite(ENA,speedSet);//start driving motorA
analogWrite(ENB,speedSet+MAX_SPEED_OFFSET);//start driving motorB
delay(5);
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void turnRight() {
motorSet = "RIGHT";
checkRight();
analogWrite(ENA,140);//start driving motorA
analogWrite(ENB,140+MAX_SPEED_OFFSET);//start driving motorB
delay(400); // run motors this way for 400ms
moveForward ();
}
//-----------------------------------------------------------------------------------------------------------------------------------
void turnLeft() {
motorSet = "LEFT";
checkLeft();
analogWrite(ENA,140);//start driving motorA
analogWrite(ENB,140+MAX_SPEED_OFFSET);//start driving motorB
delay(400); // run motors this way for 400ms
moveForward();
}
//-----------------------------------------------------------------------------------------------------------------------------------
void veerRight() { // veering right
motorSet = "RIGHT";
checkRight();
analogWrite(ENA,140);//start driving motorA
analogWrite(ENB,140+MAX_SPEED_OFFSET);//start driving motorB
delay(300); // run motors this way for 300ms
moveForward ();
}
//----------------------------------------------------------------------------------------------------------------------------------
void veerLeft() { // veering left
motorSet = "LEFT";
checkLeft();
analogWrite(ENA,140);//start driving motorA
analogWrite(ENB,140+MAX_SPEED_OFFSET);//start driving motorB
delay(300); // run motors this way for 300ms
moveForward();
}