Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Servo.h>
- Servo Myservo;
- #define trigPin 10 // Trig Pin Of HC-SR04
- #define echoPin 9 // Echo Pin Of HC-SR04
- #define trigout 8 //left motor 1st pin
- int pos;
- long duration, distance;
- void setup() {
- Myservo.attach(3);
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT); // Set Trig Pin As O/P To Transmit Waves
- pinMode(echoPin, INPUT); //Set Echo Pin As I/P To Receive Reflected Waves
- pinMode(trigout, OUTPUT);
- Myservo.write(0);
- }
- void loop() {
- readUltrasonic();
- if (distance < 15 && distance > 0){
- Myservo.write(90);
- delay(2000);
- Myservo.write(0);
- delay(2000);
- }
- }
- void readUltrasonic() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH); // Transmit Waves For 10us
- delayMicroseconds(10);
- duration = pulseIn(echoPin, HIGH); // Receive Reflected Waves
- distance = duration / 58.2; // Get Distance
- Serial.println(distance);
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement