Devices & Components
Arduino Uno Rev3
RobotGeek Continuous Rotation Servo
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
Breadboard (generic)
Software & Tools
Arduino IDE
Project description
Code
Autonomous Car
arduino
This is the code used to make the car detect obstacles. There is another LED light that activates when lights are off. To make the front lights blink and have the reverse lights activate when backing up, uncomment the commented sections, however this may break something else in the code.
1#define trigPin 12 2#define echoPin 13 3int LED1 = 2; 4int LED2 = 3; 5int LED3 = 4; 6int LED4 = 5; 7int LED5 = 6; 8int LightPin = 0; 9int threshold = 980; 10unsigned long previousMillis = 0; 11const long interval = 1000; 12int ledState = LOW; 13 14 15int n; 16long duration, distance; 17String readString; 18 19#include <Servo.h> 20Servo myservo1; 21Servo myservo2; 22 23void setup() { 24 Serial.begin(9600); 25 myservo1.attach(8); 26 myservo2.attach(9); 27 pinMode(trigPin, OUTPUT); 28 pinMode(echoPin, INPUT); 29 pinMode(LED5, OUTPUT); 30} 31 32void loop() { 33 Serial.println(analogRead(LightPin)); 34 35 digitalWrite(trigPin, HIGH); 36 _delay_ms(500); 37 digitalWrite(trigPin, LOW); 38 duration = pulseIn(echoPin, HIGH); 39 distance = (duration / 2) / 29.1; 40 41 unsigned long currentMillis = millis(); 42 43 if (analogRead(LightPin) > threshold && currentMillis - previousMillis > interval) { 44 if (ledState == HIGH) { 45 ledState = LOW; 46 Serial.println("low"); 47 } else { 48 ledState = HIGH; 49 Serial.println("high"); 50 } 51 digitalWrite(LED5 , ledState); 52 53 } 54 if (distance < 30) { 55 // digitalWrite(LED1,LOW); 56 // digitalWrite(LED2,LOW); 57 // digitalWrite(LED3,HIGH); 58 // digitalWrite(LED4,HIGH); 59 myservo1.write(n); 60 myservo2.write(180 - n); 61 delay(1000); 62 myservo1.write(n); 63 myservo2.write(90 - n); 64 delay(500); 65 } 66 else { 67 // digitalWrite(LED1, HIGH); 68 // digitalWrite(LED2, HIGH); 69 // digitalWrite(LED3, LOW); 70 // digitalWrite(LED4,LOW); 71 myservo1.write(180 - n); 72 myservo2.write(n); 73 74 } 75 }
Autonomous Car
arduino
This is the code used to make the car detect obstacles. There is another LED light that activates when lights are off. To make the front lights blink and have the reverse lights activate when backing up, uncomment the commented sections, however this may break something else in the code.
1#define trigPin 12 2#define echoPin 13 3int LED1 = 2; 4int LED2 = 3; 5int LED3 = 4; 6int LED4 = 5; 7int LED5 = 6; 8int LightPin = 0; 9int threshold = 980; 10unsigned long previousMillis = 0; 11const long interval = 1000; 12int ledState = LOW; 13 14 15int n; 16long duration, distance; 17String readString; 18 19#include <Servo.h> 20Servo myservo1; 21Servo myservo2; 22 23void setup() { 24 Serial.begin(9600); 25 myservo1.attach(8); 26 myservo2.attach(9); 27 pinMode(trigPin, OUTPUT); 28 pinMode(echoPin, INPUT); 29 pinMode(LED5, OUTPUT); 30} 31 32void loop() { 33 Serial.println(analogRead(LightPin)); 34 35 digitalWrite(trigPin, HIGH); 36 _delay_ms(500); 37 digitalWrite(trigPin, LOW); 38 duration = pulseIn(echoPin, HIGH); 39 distance = (duration / 2) / 29.1; 40 41 unsigned long currentMillis = millis(); 42 43 if (analogRead(LightPin) > threshold && currentMillis - previousMillis > interval) { 44 if (ledState == HIGH) { 45 ledState = LOW; 46 Serial.println("low"); 47 } else { 48 ledState = HIGH; 49 Serial.println("high"); 50 } 51 digitalWrite(LED5 , ledState); 52 53 } 54 if (distance < 30) { 55 // digitalWrite(LED1,LOW); 56 // digitalWrite(LED2,LOW); 57 // digitalWrite(LED3,HIGH); 58 // digitalWrite(LED4,HIGH); 59 myservo1.write(n); 60 myservo2.write(180 - n); 61 delay(1000); 62 myservo1.write(n); 63 myservo2.write(90 - n); 64 delay(500); 65 } 66 else { 67 // digitalWrite(LED1, HIGH); 68 // digitalWrite(LED2, HIGH); 69 // digitalWrite(LED3, LOW); 70 // digitalWrite(LED4,LOW); 71 myservo1.write(180 - n); 72 myservo2.write(n); 73 74 } 75 }
Downloadable files
untitled
untitled
Comments
Only logged in users can leave comments