Created
December 28, 2020 00:44
This file contains 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
//Code Written by ahmed@deeplift.tech | |
//For more projects check our YouTube channel https://www.youtube.com/c/DeepLift | |
//Ultrasonic Sensor Pins | |
#define Trig 13 | |
#define Echo 12 | |
//Motor Pins | |
#define OUT1 8 | |
#define OUT2 9 | |
#define OUT3 7 | |
#define OUT4 6 | |
void setup() { | |
Serial.begin(9600); | |
pinMode (Trig, OUTPUT); | |
pinMode (Echo, INPUT); | |
pinMode(OUT1, OUTPUT); | |
pinMode(OUT2, OUTPUT); | |
pinMode(OUT3, OUTPUT); | |
pinMode(OUT4, OUTPUT); | |
} | |
void loop() { | |
int duration, distance; | |
digitalWrite(Trig, HIGH); | |
delayMicroseconds(2); | |
digitalWrite(Trig, LOW); | |
delayMicroseconds(10); | |
duration = pulseIn(Echo, HIGH); | |
distance = ( duration / 2)/ 29.1; | |
delay(10); | |
if (distance > 19) | |
{ | |
digitalWrite(OUT1, HIGH); // move forward | |
digitalWrite(OUT2, LOW); | |
digitalWrite(OUT3, HIGH); | |
digitalWrite(OUT4, LOW); | |
} | |
if (distance < 18) | |
{ | |
digitalWrite(OUT1, LOW); //Stops | |
digitalWrite(OUT2, LOW); | |
digitalWrite(OUT3, LOW); | |
digitalWrite(OUT4, LOW); | |
delay(500); | |
digitalWrite(OUT1, LOW); //Moves Backwards | |
digitalWrite(OUT2, HIGH); | |
digitalWrite(OUT3, LOW); | |
digitalWrite(OUT4, HIGH); | |
delay(500); | |
digitalWrite(OUT1, LOW); //Stops | |
digitalWrite(OUT2, LOW); | |
digitalWrite(OUT3, LOW); | |
digitalWrite(OUT4, LOW); | |
delay(100); | |
digitalWrite(OUT1, HIGH); | |
digitalWrite(OUT2, LOW); | |
digitalWrite(OUT4, LOW); | |
digitalWrite(OUT3, LOW); | |
delay(500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment