Components and supplies
2
DC Motor, 12 V
2
battery 18650
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Arduino UNO
1
adafruit motor shield V1
1
Battery Holder, 18650 x 2
Apps and platforms
1
Arduino IDE
Project description
Code
edge avoiding code
arduino
1#include <AFMotor.h> 2 3AF_DCMotor MotorL(4); // Motor for drive Left on M4 4AF_DCMotor MotorR(3); // Motor for drive Right on M3 5 6//ultrasonic setup: 7 const int trigPin = A4; // trig pin connected to Arduino's pin A4 8 const int echoPin = A5; // echo pin connected to Arduino's pin A5 9 10// defines variables 11long duration; 12int distanceCm=0; 13 14void setup() { 15 Serial.begin(115200); // set up Serial library at 115200 bps 16 Serial.println("*robot Edge Avoidance Mod*"); 17 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 18 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 19 20 // Set the speed to start, from 0 (off) to 255 (max speed) 21 // sometimes the motors don't have the same speed, so use these values tomake your robot move straight 22 MotorL.setSpeed(100); 23 MotorR.setSpeed(100); 24 // turn on motor 25 MotorL.run(RELEASE); 26 MotorR.run(RELEASE); 27} 28 29// main program loop 30void loop() { 31 distanceCm=getDistance(); // variable to store the distance measured by the sensor 32 33 Serial.println(distanceCm); // print the distance that was measured 34 35 //if the distance is more than 10cm, robot will go backward for 1 second, and turn right for 1 second 36 if(distanceCm > 7){ 37 MotorL.run(BACKWARD); 38 MotorR.run(BACKWARD); 39 delay(500); 40 MotorL.run(FORWARD); 41 MotorR.run(BACKWARD); 42 delay(500); 43 } else { 44 MotorL.run(FORWARD); //otherwise it will continue forward 45 MotorR.run(FORWARD); 46 } 47} 48 49 50 51//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR 52int getDistance() { 53 int echoTime; //variable to store the time it takes for a ping to bounce off an object 54 int calcualtedDistance; //variable to store the distance calculated from the echo time 55 56 //send out an ultrasonic pulse that's 10ms long 57 digitalWrite(trigPin, HIGH); 58 delayMicroseconds(10); 59 digitalWrite(trigPin, LOW); 60 61 echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the 62 //pulse to bounce back to the sensor 63 64 calcualtedDistance = echoTime / 58.26; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound) 65 return calcualtedDistance; //send back the distance that was calculated 66} 67
edge avoiding code
arduino
1#include <AFMotor.h> 2 3AF_DCMotor MotorL(4); // Motor for drive Left on M4 4AF_DCMotor MotorR(3); // Motor for drive Right on M3 5 6//ultrasonic setup: 7 const int trigPin = A4; // trig pin connected to Arduino's pin A4 8 const int echoPin = A5; // echo pin connected to Arduino's pin A5 9 10// defines variables 11long duration; 12int distanceCm=0; 13 14void setup() { 15 Serial.begin(115200); // set up Serial library at 115200 bps 16 Serial.println("*robot Edge Avoidance Mod*"); 17 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 18 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 19 20 // Set the speed to start, from 0 (off) to 255 (max speed) 21 // sometimes the motors don't have the same speed, so use these values tomake your robot move straight 22 MotorL.setSpeed(100); 23 MotorR.setSpeed(100); 24 // turn on motor 25 MotorL.run(RELEASE); 26 MotorR.run(RELEASE); 27} 28 29// main program loop 30void loop() { 31 distanceCm=getDistance(); // variable to store the distance measured by the sensor 32 33 Serial.println(distanceCm); // print the distance that was measured 34 35 //if the distance is more than 10cm, robot will go backward for 1 second, and turn right for 1 second 36 if(distanceCm > 7){ 37 MotorL.run(BACKWARD); 38 MotorR.run(BACKWARD); 39 delay(500); 40 MotorL.run(FORWARD); 41 MotorR.run(BACKWARD); 42 delay(500); 43 } else { 44 MotorL.run(FORWARD); //otherwise it will continue forward 45 MotorR.run(FORWARD); 46 } 47} 48 49 50 51//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR 52int getDistance() { 53 int echoTime; //variable to store the time it takes for a ping to bounce off an object 54 int calcualtedDistance; //variable to store the distance calculated from the echo time 55 56 //send out an ultrasonic pulse that's 10ms long 57 digitalWrite(trigPin, HIGH); 58 delayMicroseconds(10); 59 digitalWrite(trigPin, LOW); 60 61 echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the 62 //pulse to bounce back to the sensor 63 64 calcualtedDistance = echoTime / 58.26; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound) 65 return calcualtedDistance; //send back the distance that was calculated 66} 67
Downloadable files
sketch_car_edgeavoiding_iN21hPwMdj.ino
sketch_car_edgeavoiding_iN21hPwMdj.ino
Comments
Only logged in users can leave comments