Advertisement
safwan092

Untitled

Oct 26th, 2023
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include<Servo.h>
  2. Servo Myservo;
  3. #define trigPin 10 // Trig Pin Of HC-SR04
  4. #define echoPin 9 // Echo Pin Of HC-SR04
  5. #define trigout 8 //left motor 1st pin
  6.  
  7. int pos;
  8. long duration, distance;
  9.  
  10.  
  11. void setup() {
  12. Myservo.attach(3);
  13. Serial.begin(9600);
  14. pinMode(trigPin, OUTPUT); // Set Trig Pin As O/P To Transmit Waves
  15. pinMode(echoPin, INPUT); //Set Echo Pin As I/P To Receive Reflected Waves
  16. pinMode(trigout, OUTPUT);
  17. Myservo.write(0);
  18. }
  19.  
  20.  
  21. void loop() {
  22. readUltrasonic();
  23.  
  24. if (distance < 15 && distance > 0){
  25. Myservo.write(90);
  26. delay(2000);
  27. Myservo.write(0);
  28. delay(2000);
  29. }
  30. }
  31.  
  32. void readUltrasonic() {
  33. digitalWrite(trigPin, LOW);
  34. delayMicroseconds(2);
  35. digitalWrite(trigPin, HIGH); // Transmit Waves For 10us
  36. delayMicroseconds(10);
  37. duration = pulseIn(echoPin, HIGH); // Receive Reflected Waves
  38. distance = duration / 58.2; // Get Distance
  39. Serial.println(distance);
  40. delay(10);
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement