Advertisement
1not

DeathStar_Version 1_Old

Apr 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. // Sensor
  2. const int trig = 3;
  3. const int echo = 2;
  4. int distance, u_seconds;
  5. // Motor 1
  6. const int motorPin1  = 5;  // Pin 15 of L293
  7. const int motorPin2  = 6;  // Pin 10 of L293
  8. // Motor 2
  9. const int motorPin3  = 7; // Pin  7 of L293
  10. const int motorPin4  = 4;  // Pin  2 of L293
  11.  
  12. void setup()
  13. {
  14. // Sensor
  15.   Serial.begin(9600);
  16.   pinMode(trig, OUTPUT);
  17.   pinMode(echo, INPUT);
  18. // Motors
  19.   pinMode(motorPin1, OUTPUT);
  20.   pinMode(motorPin2, OUTPUT);
  21.   pinMode(motorPin3, OUTPUT);
  22.   pinMode(motorPin4, OUTPUT);
  23. /* Motor Control - Motor 1: motorPin1, motorpin2
  24.                   Motor 2: motorpin3, motorpin4
  25. */
  26. }
  27.  
  28. void loop()
  29. {
  30.   digitalWrite(trig, LOW);  // Trig reset
  31.   delayMicroseconds(2);
  32.   digitalWrite(trig, HIGH);
  33.   delayMicroseconds(10);
  34.   digitalWrite(trig, LOW);
  35.  
  36.   u_seconds = pulseIn(echo, HIGH);  // Receiving US signal in time interval
  37.   distance = u_seconds * 0.034 / 2; // Distance between sensor and object
  38.  
  39.   if (distance < 15)
  40.   {
  41.     digitalWrite(motorPin1, HIGH); // Go back
  42.     digitalWrite(motorPin2, LOW);
  43.     digitalWrite(motorPin3, HIGH);
  44.     digitalWrite(motorPin4, LOW);
  45.          delay(400);
  46.     digitalWrite(motorPin1, HIGH); // Go left
  47.     digitalWrite(motorPin2, LOW);
  48.     digitalWrite(motorPin3, LOW);
  49.     digitalWrite(motorPin4, HIGH);
  50.          delay(100);
  51.   }
  52.   else
  53.   {
  54.     digitalWrite(motorPin1, LOW); // Turn ON motors
  55.     digitalWrite(motorPin2, HIGH);
  56.     digitalWrite(motorPin3, LOW);
  57.     digitalWrite(motorPin4, HIGH);
  58.   }
  59.  
  60.   Serial.print(distance);
  61.   Serial.println(" cm");
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement