SHARE
TWEET

Obstacle Avoiding Robot using Arduino

rabirajkhadka Aug 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int trigPin = 9;      // trig pin of HC-SR04
  2. int echoPin = 10;     // Echo pin of HC-SR04
  3.  
  4. int left_1 = 4;       //Reverse motion of Left motor
  5. int left_2 = 5;       //Forward motion of Left motor
  6. int right_1 = 6;      //Reverse motion of Right motor
  7. int right_2 = 7;      //Forward motion of Right motor
  8.  
  9. long duration, distance;
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.   pinMode(left_1, OUTPUT);      
  14.   pinMode(left_2, OUTPUT);
  15.   pinMode(right_1, OUTPUT);
  16.   pinMode(right_2, OUTPUT);
  17.  
  18.   pinMode(trigPin, OUTPUT);        
  19.   pinMode(echoPin, INPUT);          
  20. }
  21.  
  22. void loop() {
  23.  
  24.   digitalWrite(trigPin, LOW);
  25.   delayMicroseconds(2);  
  26.   digitalWrite(trigPin, HIGH);
  27.   delayMicroseconds(10);
  28.   duration = pulseIn(echoPin, HIGH);
  29.   distance = duration / 58.0;  
  30.   delay(10);
  31.  
  32.   if (distance > 19)            
  33.   {
  34.     digitalWrite(left_1, HIGH);                    // move forward
  35.     digitalWrite(left_2, LOW);
  36.     digitalWrite(right_1, HIGH);                                
  37.     digitalWrite(right_2, LOW);                                                      
  38.   }
  39.  
  40.   if (distance < 18)
  41.   {
  42.     digitalWrite(left_1, LOW);   //stop
  43.     digitalWrite(left_2, LOW);
  44.     digitalWrite(right_1, LOW);                                
  45.     digitalWrite(right_2, LOW);
  46.     delay(500);
  47.     digitalWrite(left_1, LOW);            
  48.     digitalWrite(left_2, HIGH);
  49.     digitalWrite(right_1, LOW);                                
  50.     digitalWrite(right_2, HIGH);
  51.     delay(500);
  52.     digitalWrite(left_1, LOW);   //stop
  53.     digitalWrite(left_2, LOW);
  54.     digitalWrite(right_1, LOW);                                
  55.     digitalWrite(right_2, LOW);  
  56.     delay(100);  
  57.     digitalWrite(left_1, HIGH);      
  58.     digitalWrite(left_2, LOW);  
  59.     digitalWrite(right_1, LOW);                                
  60.     digitalWrite(right_2, LOW);  
  61.     delay(500);
  62.   }
  63. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top