Components and supplies
2
battery 18650
1
adafruit motor shield V1
1
Battery Holder, 18650 x 2
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
5 mm LED: Green
1
Arduino UNO
2
DC Motor, 12 V
1
5 mm LED: Red
Tools and machines
1
Hot glue gun (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
avoiding car
arduino
this project is very cool
1#include <AFMotor.h> 2 3 4AF_DCMotor MotorL(4); // Motor sebelah kiri tersambung ke M4 5AF_DCMotor MotorR(3); // Motor sebelah kanan tersambung M3 6 7//led setup 8const int ledpinG = A2; // LED hijau tersambung ke arduino pin A2 9const int ledpinR = A0; // LED merah tersambung ke arduino pin A0 10 11//buzzer setup 12 const int buzPin = 2; // buzzer tersambung ke arduino pin 2 13 14//ultrasonic setup: 15 const int trigPin = A4; // trig pin tersambung ke arduino pin A4 16 const int echoPin = A5; // echo pin tersambung ke arduino pin A5 17 18// defines variables 19long duration; 20int distanceCm=0; 21 22 23void setup() { 24 Serial.begin(115200); // set up Serial library at 115200 bps 25 Serial.println("robot Obstacle Avoidance Mod"); 26 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 27 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 28 pinMode(buzPin, OUTPUT); // sets the buzpin as an output 29 pinMode(ledpinG,OUTPUT); // sets the ledpin GREEN as an output 30 pinMode(ledpinR,OUTPUT); // sets the ledpin as RED an output 31 32 // Set the speed to start, from 0 (off) to 255 (max speed) 33 // sometimes the motors don't have the same speed, so use these values tomake your robot move straight 34 MotorL.setSpeed(140); 35 MotorR.setSpeed(140); 36 // turn off motor 37 MotorL.run(RELEASE); 38 MotorR.run(RELEASE); 39 40} 41 42// main program loop 43void loop() { 44 distanceCm=getDistance(); // variable to store the distance measured by the sensor 45 46 Serial.println(distanceCm); // print the distance that was measured 47 48 49 //jika jarak 25cm/kurang dari 25cm, robot akan berhenti, mundur, berbelok ke kanan, "beep" 2x dan LED merah menyala 50 if(distanceCm <= 25){ 51 52 MotorL.run(RELEASE); 53 MotorR.run(RELEASE); 54 ledR(); 55 delay(50); 56 57 buz();ledR();buz();ledR(); 58 buz();ledR();buz();ledR(); 59 MotorL.run(BACKWARD); 60 MotorR.run(BACKWARD); 61 62 delay(300); 63 64 MotorL.run(FORWARD); 65 MotorR.run(BACKWARD); 66 ledR(); 67 delay(300); 68 69 } 70 71 else { 72 73 MotorL.run(FORWARD); 74 MotorR.run(FORWARD); 75 ledG(); 76 } 77 78} 79 80void buz(){ 81 digitalWrite(2, HIGH); 82 delay(100); 83 digitalWrite(2, LOW); 84 delay(100); 85} 86 87void ledG(){ 88 digitalWrite(A2, HIGH); 89 delay(100); 90 digitalWrite(A2, LOW); 91 delay(100); 92 93} 94 95void ledR(){ 96 digitalWrite(A0, HIGH); 97 delay(100); 98 digitalWrite(A0, LOW); 99 delay(100); 100 101} 102 103//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR 104int getDistance() { 105 int echoTime; //variable to store the time it takes for a ping to bounce off an object 106 int calcualtedDistance; //variable to store the distance calculated from the echo time 107 108 //send out an ultrasonic pulse that's 10ms long 109 digitalWrite(trigPin, HIGH); 110 delayMicroseconds(10); 111 digitalWrite(trigPin, LOW); 112 113 echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the 114 //pulse to bounce back to the sensor 115 116 calcualtedDistance = echoTime / 58.26; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound) 117 return calcualtedDistance; //send back the distance that was calculated 118} 119
avoiding car
arduino
this project is very cool
1#include <AFMotor.h> 2 3 4AF_DCMotor MotorL(4); // Motor sebelah kiri tersambung ke M4 5AF_DCMotor MotorR(3); // Motor sebelah kanan tersambung M3 6 7//led setup 8const int ledpinG = A2; // LED hijau tersambung ke arduino pin A2 9const int ledpinR = A0; // LED merah tersambung ke arduino pin A0 10 11//buzzer setup 12 const int buzPin = 2; // buzzer tersambung ke arduino pin 2 13 14//ultrasonic setup: 15 const int trigPin = A4; // trig pin tersambung ke arduino pin A4 16 const int echoPin = A5; // echo pin tersambung ke arduino pin A5 17 18// defines variables 19long duration; 20int distanceCm=0; 21 22 23void setup() { 24 Serial.begin(115200); // set up Serial library at 115200 bps 25 Serial.println("robot Obstacle Avoidance Mod"); 26 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 27 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 28 pinMode(buzPin, OUTPUT); // sets the buzpin as an output 29 pinMode(ledpinG,OUTPUT); // sets the ledpin GREEN as an output 30 pinMode(ledpinR,OUTPUT); // sets the ledpin as RED an output 31 32 // Set the speed to start, from 0 (off) to 255 (max speed) 33 // sometimes the motors don't have the same speed, so use these values tomake your robot move straight 34 MotorL.setSpeed(140); 35 MotorR.setSpeed(140); 36 // turn off motor 37 MotorL.run(RELEASE); 38 MotorR.run(RELEASE); 39 40} 41 42// main program loop 43void loop() { 44 distanceCm=getDistance(); // variable to store the distance measured by the sensor 45 46 Serial.println(distanceCm); // print the distance that was measured 47 48 49 //jika jarak 25cm/kurang dari 25cm, robot akan berhenti, mundur, berbelok ke kanan, "beep" 2x dan LED merah menyala 50 if(distanceCm <= 25){ 51 52 MotorL.run(RELEASE); 53 MotorR.run(RELEASE); 54 ledR(); 55 delay(50); 56 57 buz();ledR();buz();ledR(); 58 buz();ledR();buz();ledR(); 59 MotorL.run(BACKWARD); 60 MotorR.run(BACKWARD); 61 62 delay(300); 63 64 MotorL.run(FORWARD); 65 MotorR.run(BACKWARD); 66 ledR(); 67 delay(300); 68 69 } 70 71 else { 72 73 MotorL.run(FORWARD); 74 MotorR.run(FORWARD); 75 ledG(); 76 } 77 78} 79 80void buz(){ 81 digitalWrite(2, HIGH); 82 delay(100); 83 digitalWrite(2, LOW); 84 delay(100); 85} 86 87void ledG(){ 88 digitalWrite(A2, HIGH); 89 delay(100); 90 digitalWrite(A2, LOW); 91 delay(100); 92 93} 94 95void ledR(){ 96 digitalWrite(A0, HIGH); 97 delay(100); 98 digitalWrite(A0, LOW); 99 delay(100); 100 101} 102 103//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR 104int getDistance() { 105 int echoTime; //variable to store the time it takes for a ping to bounce off an object 106 int calcualtedDistance; //variable to store the distance calculated from the echo time 107 108 //send out an ultrasonic pulse that's 10ms long 109 digitalWrite(trigPin, HIGH); 110 delayMicroseconds(10); 111 digitalWrite(trigPin, LOW); 112 113 echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the 114 //pulse to bounce back to the sensor 115 116 calcualtedDistance = echoTime / 58.26; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound) 117 return calcualtedDistance; //send back the distance that was calculated 118} 119
Downloadable files
the code
this is the code for the robot
the code
Comments
Only logged in users can leave comments