Created
October 25, 2019 00:47
-
-
Save tj-devel709/0977647486bb8a574a9c4db1a0cde607 to your computer and use it in GitHub Desktop.
UltraSonic Sensor
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
//www.elegoo.com | |
#include <Servo.h> //servo library | |
Servo myservo; // create servo object to control servo | |
int Echo = A4; | |
int Trig = A5; | |
#define ENA 5 | |
#define ENB 6 | |
#define IN1 7 | |
#define IN2 8 | |
#define IN3 9 | |
#define IN4 11 | |
#define carSpeed 250 | |
int rightDistance = 0, leftDistance = 0, middleDistance = 0; | |
void forward(){ | |
analogWrite(ENA, carSpeed); | |
analogWrite(ENB, carSpeed); | |
digitalWrite(IN1, HIGH); | |
digitalWrite(IN2, LOW); | |
digitalWrite(IN3, LOW); | |
digitalWrite(IN4, HIGH); | |
Serial.println("Forward"); | |
} | |
void back() { | |
analogWrite(ENA, carSpeed); | |
analogWrite(ENB, carSpeed); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, HIGH); | |
digitalWrite(IN3, HIGH); | |
digitalWrite(IN4, LOW); | |
Serial.println("Back"); | |
} | |
void left() { | |
analogWrite(ENA, carSpeed); | |
analogWrite(ENB, carSpeed); | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, HIGH); | |
digitalWrite(IN3, LOW); | |
digitalWrite(IN4, HIGH); | |
Serial.println("Left"); | |
} | |
void right() { | |
analogWrite(ENA, carSpeed); | |
analogWrite(ENB, carSpeed); | |
digitalWrite(IN1, HIGH); | |
digitalWrite(IN2, LOW); | |
digitalWrite(IN3, HIGH); | |
digitalWrite(IN4, LOW); | |
Serial.println("Right"); | |
} | |
void stop() { | |
digitalWrite(ENA, LOW); | |
digitalWrite(ENB, LOW); | |
Serial.println("Stop!"); | |
} | |
//Ultrasonic distance measurement Sub function | |
int Distance_test() { | |
digitalWrite(Trig, LOW); | |
delayMicroseconds(2); | |
digitalWrite(Trig, HIGH); | |
delayMicroseconds(20); | |
digitalWrite(Trig, LOW); | |
float Fdistance = pulseIn(Echo, HIGH); | |
Fdistance= Fdistance / 58; | |
return (int)Fdistance; | |
} | |
void setup() { | |
myservo.attach(3,700,2400); // attach servo on pin 3 to servo object | |
Serial.begin(9600); | |
pinMode(Echo, INPUT); | |
pinMode(Trig, OUTPUT); | |
pinMode(IN1, OUTPUT); | |
pinMode(IN2, OUTPUT); | |
pinMode(IN3, OUTPUT); | |
pinMode(IN4, OUTPUT); | |
pinMode(ENA, OUTPUT); | |
pinMode(ENB, OUTPUT); | |
stop(); | |
} | |
void loop() { | |
myservo.write(90); //setservo position according to scaled value | |
delay(500); | |
middleDistance = Distance_test(); | |
if(middleDistance <= 40) { | |
stop(); | |
delay(500); | |
myservo.write(10); | |
delay(1000); | |
rightDistance = Distance_test(); | |
delay(500); | |
myservo.write(90); | |
delay(1000); | |
myservo.write(180); | |
delay(1000); | |
leftDistance = Distance_test(); | |
delay(500); | |
myservo.write(90); | |
delay(1000); | |
if(rightDistance > leftDistance) { | |
right(); | |
delay(360); | |
} | |
else if(rightDistance < leftDistance) { | |
left(); | |
delay(360); | |
} | |
else if((rightDistance <= 40) || (leftDistance <= 40)) { | |
back(); | |
delay(180); | |
} | |
else { | |
forward(); | |
} | |
} | |
else { | |
forward(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment