Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int trigPin = 9; // trig pin of HC-SR04
- int echoPin = 10; // Echo pin of HC-SR04
- int left_1 = 4; //Reverse motion of Left motor
- int left_2 = 5; //Forward motion of Left motor
- int right_1 = 6; //Reverse motion of Right motor
- int right_2 = 7; //Forward motion of Right motor
- long duration, distance;
- void setup() {
- Serial.begin(9600);
- pinMode(left_1, OUTPUT);
- pinMode(left_2, OUTPUT);
- pinMode(right_1, OUTPUT);
- pinMode(right_2, OUTPUT);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- }
- void loop() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- duration = pulseIn(echoPin, HIGH);
- distance = duration / 58.0;
- delay(10);
- if (distance > 19)
- {
- digitalWrite(left_1, HIGH); // move forward
- digitalWrite(left_2, LOW);
- digitalWrite(right_1, HIGH);
- digitalWrite(right_2, LOW);
- }
- if (distance < 18)
- {
- digitalWrite(left_1, LOW); //stop
- digitalWrite(left_2, LOW);
- digitalWrite(right_1, LOW);
- digitalWrite(right_2, LOW);
- delay(500);
- digitalWrite(left_1, LOW);
- digitalWrite(left_2, HIGH);
- digitalWrite(right_1, LOW);
- digitalWrite(right_2, HIGH);
- delay(500);
- digitalWrite(left_1, LOW); //stop
- digitalWrite(left_2, LOW);
- digitalWrite(right_1, LOW);
- digitalWrite(right_2, LOW);
- delay(100);
- digitalWrite(left_1, HIGH);
- digitalWrite(left_2, LOW);
- digitalWrite(right_1, LOW);
- digitalWrite(right_2, LOW);
- delay(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement