SHARE
TWEET

Untitled

a guest Apr 21st, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. #define SPEED_OF_SOUND 0.034
  3. const int trigPin = 4;
  4. const int echoPin = 5;
  5. int IN1 = 6;
  6. int IN2 = 7;
  7. int IN3 = 8;
  8. int IN4 = 9;
  9. Servo servo;  
  10.  
  11. void setup() {
  12.   servo.attach(3);
  13.   pinMode(trigPin, OUTPUT);
  14.   pinMode(echoPin, INPUT);
  15. pinMode(IN1, OUTPUT);
  16. pinMode(IN2, OUTPUT);
  17. pinMode(IN3, OUTPUT);
  18. pinMode(IN4, OUTPUT);
  19.   Serial.begin(9600);
  20. }
  21. void loop() {
  22. servo.write(90);
  23.  
  24.       long duration;
  25.         int distance;  
  26.         digitalWrite(trigPin, LOW);
  27.         delayMicroseconds(2);
  28.         digitalWrite(trigPin, HIGH);
  29.         delayMicroseconds(10);
  30.         digitalWrite(trigPin, LOW);
  31.  
  32.         duration = pulseIn(echoPin, HIGH);
  33.         distance= duration*(SPEED_OF_SOUND/2);
  34.         Serial.print("Distance: ");
  35.         Serial.println(distance);  
  36.         delay(100);
  37.      
  38. digitalWrite(IN1, LOW);
  39. digitalWrite(IN2, HIGH);
  40. digitalWrite(IN3, HIGH);
  41. digitalWrite(IN4, LOW);  
  42.  
  43.  if (distance < 25) {
  44.  Serial.println("back up");  
  45. digitalWrite(IN1, HIGH);
  46. digitalWrite(IN2, LOW);
  47. digitalWrite(IN3, LOW);
  48. digitalWrite(IN4, HIGH);
  49. delay(250);
  50. Serial.println("stop");    
  51. digitalWrite(IN1, LOW);
  52. digitalWrite(IN2, LOW);
  53. digitalWrite(IN3, LOW);
  54. digitalWrite(IN4, LOW);
  55.   servo.write(180);
  56.       int distancel;  
  57.         digitalWrite(trigPin, LOW);
  58.         delayMicroseconds(2);
  59.         digitalWrite(trigPin, HIGH);
  60.         delayMicroseconds(10);
  61.         digitalWrite(trigPin, LOW);
  62.         duration = pulseIn(echoPin, HIGH);
  63.         distancel = duration*(SPEED_OF_SOUND/2);
  64.         Serial.print("Distancel: ");
  65.         Serial.println(distancel);  
  66.      delay(500);  
  67.      servo.write(0);
  68.           int distancer;  
  69.         digitalWrite(trigPin, LOW);
  70.         delayMicroseconds(2);
  71.         digitalWrite(trigPin, HIGH);
  72.         delayMicroseconds(10);
  73.         digitalWrite(trigPin, LOW);
  74.  
  75.         duration = pulseIn(echoPin, HIGH);
  76.         distancer = duration*(SPEED_OF_SOUND/2);
  77.        Serial.print("Distancer: ");
  78.         Serial.println(distancer);  
  79.         delay(500);      
  80.      
  81.           if(distancer > distancel) {
  82.           Serial.println("right ");
  83.           digitalWrite(IN1, LOW);
  84.           digitalWrite(IN2, HIGH);
  85.           digitalWrite(IN3, LOW);
  86.           digitalWrite(IN4, HIGH);
  87.           delay(1500); }
  88.             else{
  89.                Serial.println("left ");
  90.             digitalWrite(IN1, HIGH);
  91.             digitalWrite(IN2, LOW);
  92.             digitalWrite(IN3, HIGH);
  93.             digitalWrite(IN4, LOW);
  94.             delay(1500); }
  95.  
  96.         }
  97.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top