Devices & Components
Arduino Uno Rev3
Hardware & Tools
Hot glue gun (generic)
lego wheels
Project description
Code
autonomous car code
c_cpp
here is the car code
1#define trigPin 12 2#define echoPin 13 3 4int n; 5int duration, distance; 6 7String readString; 8 9#include <Servo.h> 10Servo myservo1; // create servo object to control servo 11Servo myservo2; 12 13void setup() { 14 Serial.begin(9600); 15 myservo1.attach(8); // assigns the servo to a pin 16 myservo2.attach(9); 17 pinMode(trigPin, OUTPUT); 18 pinMode(echoPin, INPUT); 19 pinMode(3, OUTPUT); 20} 21 22void loop() { 23 digitalWrite(trigPin, HIGH); 24 _delay_ms(500); 25 digitalWrite(trigPin, LOW); 26 duration = pulseIn(echoPin, HIGH); 27 distance = (duration/2) / 29.1; 28 29 if (distance < 40) { // this is the distance which the rover has to stop 30 digitalWrite(3, HIGH); 31 myservo1.write(n); // controls the direction of the motors 32 myservo2.write(180-n); 33 delay(1000); // how long the wheels spin 34 myservo1.write(n); 35 myservo2.write(90-n); 36 delay(500); 37} 38 else { // what the rover will do if it doen't sense anything 39 digitalWrite(3, LOW); 40 myservo1.write(180-n); 41 myservo2.write(n); 42} 43}
Downloadable files
how to wire...
how to wire...

how to wire...
how to wire...

Comments
Only logged in users can leave comments