Last active
June 18, 2016 11:17
-
-
Save tzach/fcd5ee29826211b76d2aa95d05c4ed47 to your computer and use it in GitHub Desktop.
arduino car 01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /***************************/ | |
| /* simple root car */ | |
| /***************************/ | |
| // engine part from | |
| // http://www.instructables.com/id/Control-DC-and-stepper-motors-with-L298N-Dual-Moto/?ALLSTEPS | |
| // sndor part from | |
| // http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/?ALLSTEPS | |
| /* | |
| HC-SR04 Ping distance sensor] | |
| VCC to arduino 5v GND to arduino GND | |
| Echo to Arduino pin 13 Trig to Arduino pin 12 | |
| */ | |
| // Sensors | |
| #define trigPinA 13 | |
| #define echoPinA 12 | |
| #define trigPinB 3 | |
| #define echoPinB 2 | |
| // Engines | |
| // connect motor controller pins to Arduino digital pins | |
| // motor one | |
| int enA = 10; | |
| int in1 = 9; | |
| int in2 = 8; | |
| // motor two | |
| int enB = 5; | |
| int in3 = 7; | |
| int in4 = 6; | |
| const int min_dist = 20; | |
| const int slowdown_dist=50; | |
| const int speedA=180; | |
| const int speedB=250; | |
| boolean direction; | |
| long dist(short trig, short echo) { | |
| digitalWrite(trig, LOW); // Added this line | |
| delayMicroseconds(2); // Added this line | |
| digitalWrite(trig, HIGH); | |
| delayMicroseconds(10); // Added this line | |
| digitalWrite(trig, LOW); | |
| const long duration = pulseIn(echo, HIGH); | |
| const long distance = (duration/2) / 29.1; | |
| return distance; | |
| } | |
| void set_direction(boolean set_engine_dir) { | |
| // turn on motor A | |
| if (set_engine_dir) { | |
| digitalWrite(in1, HIGH); | |
| digitalWrite(in2, LOW); | |
| digitalWrite(in3, HIGH); | |
| digitalWrite(in4, LOW); | |
| } else { | |
| digitalWrite(in2, HIGH); | |
| digitalWrite(in1, LOW); | |
| digitalWrite(in4, HIGH); | |
| digitalWrite(in3, LOW); | |
| } | |
| } | |
| void setup() { | |
| Serial.begin (9600); | |
| pinMode(trigPinA, OUTPUT); | |
| pinMode(echoPinA, INPUT); | |
| pinMode(trigPinB, OUTPUT); | |
| pinMode(echoPinB, INPUT); | |
| // set all the motor control pins to outputs | |
| pinMode(enA, OUTPUT); | |
| pinMode(enB, OUTPUT); | |
| pinMode(in1, OUTPUT); | |
| pinMode(in2, OUTPUT); | |
| pinMode(in3, OUTPUT); | |
| pinMode(in4, OUTPUT); | |
| direction=false; | |
| set_direction(direction); | |
| analogWrite(enA, speedA); | |
| analogWrite(enB, speedB); | |
| } | |
| void loop() { | |
| // long duration, distance; | |
| const long dA = dist(trigPinA,echoPinA); | |
| Serial.print("Side A distance:"); | |
| Serial.print(dA); | |
| Serial.println(" cm"); | |
| const long dB = dist(trigPinB,echoPinB); | |
| Serial.print("Side B distance:"); | |
| Serial.print(dB); | |
| Serial.println(" cm"); | |
| if ((direction && (dA<slowdown_dist)) || ((!direction) && (dB<slowdown_dist))) { | |
| analogWrite(enA, speedA / 2); | |
| analogWrite(enB, speedB / 2); | |
| Serial.println("Slow"); | |
| } else { | |
| Serial.println("Full Speed"); | |
| analogWrite(enA, speedA); | |
| analogWrite(enB, speedB); | |
| } | |
| if ((dA<min_dist) and (dB<min_dist)) { | |
| analogWrite(enA, 0); | |
| analogWrite(enB, 0); | |
| Serial.println("Stop"); | |
| } | |
| if ((direction && (dA<min_dist)) || ((!direction) && (dB<min_dist))) { | |
| direction = (!direction); | |
| set_direction(direction); | |
| delay(200); | |
| } | |
| if (direction) { | |
| Serial.println("Diraction A"); | |
| } | |
| else { | |
| Serial.println("Diraction B"); | |
| } | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment