Devices & Components
Arduino Uno Rev3
Jumper wires (generic)
Dual H-Bridge motor drivers L298
IR Proximity Sensor
Geared DC Motor, 12 V
9V battery (generic)
Hardware & Tools
Hot glue gun (generic)
Multitool, Screwdriver
Software & Tools
Arduino IDE
Project description
Code
Code
c_cpp
1 2 3 int vSpeed = 110; // MAX 255 4 int turn_speed = 230; // MAX 255 5 int turn_delay = 10; 6 7//L293 Connection 8 const int motorA1 = 8; 9 const int motorA2 = 10; 10 const int motorAspeed = 9; 11 const int motorB1 = 12; 12 const int motorB2 = 13; 13 const int motorBspeed = 11; 14 15//Sensor Connection 16 const int left_sensor_pin =A0; 17 const int right_sensor_pin =A1; 18 19 20 21 int left_sensor_state; 22 int right_sensor_state; 23 24void setup() { 25 pinMode(motorA1, OUTPUT); 26 pinMode(motorA2, OUTPUT); 27 pinMode(motorB1, OUTPUT); 28 pinMode(motorB2, OUTPUT); 29 30 Serial.begin(9600); 31 32 delay(3000); 33 34} 35 36void loop() { 37 38 39 40 41 42left_sensor_state = analogRead(left_sensor_pin); 43right_sensor_state = analogRead(right_sensor_pin); 44 45if(right_sensor_state > 500 && left_sensor_state < 500) 46{ 47 Serial.println("turning right"); 48 49 digitalWrite (motorA1,LOW); 50 digitalWrite(motorA2,HIGH); 51 digitalWrite (motorB1,LOW); 52 digitalWrite(motorB2,HIGH); 53 54 analogWrite (motorAspeed, vSpeed); 55 analogWrite (motorBspeed, turn_speed); 56 57 } 58if(right_sensor_state < 500 && left_sensor_state > 500) 59{ 60 Serial.println("turning left"); 61 62 digitalWrite (motorA1,HIGH); 63 digitalWrite(motorA2,LOW); 64 digitalWrite (motorB1,HIGH); 65 digitalWrite(motorB2,LOW); 66 67 analogWrite (motorAspeed, turn_speed); 68 analogWrite (motorBspeed, vSpeed); 69 70 delay(turn_delay); 71 } 72 73if(right_sensor_state > 500 && left_sensor_state > 500) 74{ 75 Serial.println("going forward"); 76 77 digitalWrite (motorA2,LOW); 78 digitalWrite(motorA1,HIGH); 79 digitalWrite (motorB2,HIGH); 80 digitalWrite(motorB1,LOW); 81 82 analogWrite (motorAspeed, vSpeed); 83 analogWrite (motorBspeed, vSpeed); 84 85 delay(turn_delay); 86 87 } 88 89if(right_sensor_state < 500 && left_sensor_state < 500) 90{ 91 Serial.println("stop"); 92 93 analogWrite (motorAspeed, 0); 94 analogWrite (motorBspeed, 0); 95 96 } 97 98 99} 100 101 102 103 104 105 106 107 108 109 110 111
Downloadable files
circuit diagram
circuit diagram
circuit diagram
circuit diagram
Comments
Only logged in users can leave comments