Components and supplies
Jumper wires (generic)
Rocker Switch, SPST
Adafruit Motor shield
Maker Essentials - Micro-motors & Grippy Wheels
DC Motor, 12 V
IR Transceiver (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
SG90 Micro-servo motor
Arduino UNO
9V battery (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
1// www.YouTube.com/Electronics is Fun // 2 3#include<Servo.h> 4#include<AFMotor.h> 5#define LEFT A0 6#define echopin A1 // echo pin 7#define trigpin A2 // Trigger pin 8#define RIGHT A3 9 10AF_DCMotor Motor1(1,MOTOR12_1KHZ); 11AF_DCMotor Motor2(2,MOTOR12_1KHZ); 12AF_DCMotor Motor3(3,MOTOR34_1KHZ); 13AF_DCMotor Motor4(4,MOTOR34_1KHZ); 14 15Servo myservo; 16 17int pos =0; 18long time; 19 20void setup(){ 21 22Serial.begin(9600); 23myservo.attach(10); 24 25for(pos = 90; pos <= 180; pos += 1){ 26myservo.write(pos); 27delay(15); 28} 29 30for(pos = 180; pos >= 0; pos-= 1) { 31myservo.write(pos); 32delay(15); 33} 34 35for(pos = 0; pos<=90; pos += 1) { 36myservo.write(pos); 37delay(15); 38} 39 40pinMode(RIGHT, INPUT); 41pinMode(LEFT, INPUT); 42 43pinMode(trigpin, OUTPUT); 44pinMode(echopin, INPUT); 45 46} 47 48void loop(){ 49unsigned int distance = read_cm(); 50 51int Right_Value = digitalRead(RIGHT); 52int Left_Value = digitalRead(LEFT); 53 54Serial.print("R= "); 55Serial.print(Right_Value); 56Serial.print(" L= "); 57Serial.print(Left_Value); 58Serial.print(" D= "); 59Serial.println(distance); 60 61 if((Right_Value==1) && (distance>=10 && distance<=30)&&(Left_Value==1)){forword();} 62else if((Right_Value==0) && (Left_Value==1)){turnRight();} 63else if((Right_Value==1) && (Left_Value==0)){turnLeft();} 64else if((Right_Value==1) && (Left_Value==1)){stop();} 65else if(distance > 5 && distance < 10){stop();} 66else if(distance < 5){backword();} 67 68delay(50); 69} 70 71long read_cm(){ 72 digitalWrite(trigpin, LOW); 73 delayMicroseconds(2); 74 digitalWrite(trigpin, HIGH); 75 delayMicroseconds(10); 76 time = pulseIn (echopin, HIGH); 77 return time / 29 / 2; 78} 79 80void forword(){// turn it on going forward 81Motor1.setSpeed(120); 82Motor1.run(FORWARD); 83Motor2.setSpeed(120); 84Motor2.run(FORWARD); 85Motor3.setSpeed(120); 86Motor3.run(FORWARD); 87Motor4.setSpeed(120); 88Motor4.run(FORWARD); 89} 90 91void backword(){ // the other way 92Motor1.setSpeed(120); 93Motor1.run(BACKWARD); 94Motor2.setSpeed(120); 95Motor2.run(BACKWARD); 96Motor3.setSpeed(120); 97Motor3.run(BACKWARD); 98Motor4.setSpeed(120); 99Motor4.run(BACKWARD); 100} 101 102void turnRight(){ // the other right 103Motor1.setSpeed(200); 104Motor1.run(FORWARD); 105Motor2.setSpeed(200); 106Motor2.run(FORWARD); 107Motor3.setSpeed(100); 108Motor3.run(BACKWARD); 109Motor4.setSpeed(100); 110Motor4.run(BACKWARD); 111} 112 113void turnLeft(){ // turn it on going left 114Motor1.setSpeed(100); 115Motor1.run(BACKWARD); 116Motor2.setSpeed(100); 117Motor2.run(BACKWARD); 118Motor3.setSpeed(200); 119Motor3.run(FORWARD); 120Motor4.setSpeed(200); 121Motor4.run(FORWARD); 122} 123 124void stop(){ // stopped 125Motor1.setSpeed(0); 126Motor1.run(RELEASE); 127Motor2.setSpeed(0); 128Motor2.run(RELEASE); 129Motor3.setSpeed(0); 130Motor3.run(RELEASE); 131Motor4.setSpeed(0); 132Motor4.run(RELEASE); 133} 134
Code
arduino
1// www.YouTube.com/Electronics is Fun // 2 3#include<Servo.h> 4#include<AFMotor.h> 5#define LEFT A0 6#define echopin A1 // echo pin 7#define trigpin A2 // Trigger pin 8#define RIGHT A3 9 10AF_DCMotor Motor1(1,MOTOR12_1KHZ); 11AF_DCMotor Motor2(2,MOTOR12_1KHZ); 12AF_DCMotor Motor3(3,MOTOR34_1KHZ); 13AF_DCMotor Motor4(4,MOTOR34_1KHZ); 14 15Servo myservo; 16 17int pos =0; 18long time; 19 20void setup(){ 21 22Serial.begin(9600); 23myservo.attach(10); 24 25for(pos = 90; pos <= 180; pos += 1){ 26myservo.write(pos); 27delay(15); 28} 29 30for(pos = 180; pos >= 0; pos-= 1) { 31myservo.write(pos); 32delay(15); 33} 34 35for(pos = 0; pos<=90; pos += 1) { 36myservo.write(pos); 37delay(15); 38} 39 40pinMode(RIGHT, INPUT); 41pinMode(LEFT, INPUT); 42 43pinMode(trigpin, OUTPUT); 44pinMode(echopin, INPUT); 45 46} 47 48void loop(){ 49unsigned int distance = read_cm(); 50 51int Right_Value = digitalRead(RIGHT); 52int Left_Value = digitalRead(LEFT); 53 54Serial.print("R= "); 55Serial.print(Right_Value); 56Serial.print(" L= "); 57Serial.print(Left_Value); 58Serial.print(" D= "); 59Serial.println(distance); 60 61 if((Right_Value==1) && (distance>=10 && distance<=30)&&(Left_Value==1)){forword();} 62else if((Right_Value==0) && (Left_Value==1)){turnRight();} 63else if((Right_Value==1) && (Left_Value==0)){turnLeft();} 64else if((Right_Value==1) && (Left_Value==1)){stop();} 65else if(distance > 5 && distance < 10){stop();} 66else if(distance < 5){backword();} 67 68delay(50); 69} 70 71long read_cm(){ 72 digitalWrite(trigpin, LOW); 73 delayMicroseconds(2); 74 digitalWrite(trigpin, HIGH); 75 delayMicroseconds(10); 76 time = pulseIn (echopin, HIGH); 77 return time / 29 / 2; 78} 79 80void forword(){// turn it on going forward 81Motor1.setSpeed(120); 82Motor1.run(FORWARD); 83Motor2.setSpeed(120); 84Motor2.run(FORWARD); 85Motor3.setSpeed(120); 86Motor3.run(FORWARD); 87Motor4.setSpeed(120); 88Motor4.run(FORWARD); 89} 90 91void backword(){ // the other way 92Motor1.setSpeed(120); 93Motor1.run(BACKWARD); 94Motor2.setSpeed(120); 95Motor2.run(BACKWARD); 96Motor3.setSpeed(120); 97Motor3.run(BACKWARD); 98Motor4.setSpeed(120); 99Motor4.run(BACKWARD); 100} 101 102void turnRight(){ // the other right 103Motor1.setSpeed(200); 104Motor1.run(FORWARD); 105Motor2.setSpeed(200); 106Motor2.run(FORWARD); 107Motor3.setSpeed(100); 108Motor3.run(BACKWARD); 109Motor4.setSpeed(100); 110Motor4.run(BACKWARD); 111} 112 113void turnLeft(){ // turn it on going left 114Motor1.setSpeed(100); 115Motor1.run(BACKWARD); 116Motor2.setSpeed(100); 117Motor2.run(BACKWARD); 118Motor3.setSpeed(200); 119Motor3.run(FORWARD); 120Motor4.setSpeed(200); 121Motor4.run(FORWARD); 122} 123 124void stop(){ // stopped 125Motor1.setSpeed(0); 126Motor1.run(RELEASE); 127Motor2.setSpeed(0); 128Motor2.run(RELEASE); 129Motor3.setSpeed(0); 130Motor3.run(RELEASE); 131Motor4.setSpeed(0); 132Motor4.run(RELEASE); 133} 134
Downloadable files
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments