Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo Left;
- Servo Right;
- int echo = 2;
- int trigger = 3;
- long distance;
- long duration;
- void setup(){
- Serial.begin(9600);
- Left.attach(12);
- Right.attach(13);
- pinMode(trigger, OUTPUT);
- pinMode(echo, INPUT);
- }
- void moveForward(int time)
- {
- Left.writeMicroseconds(1700);
- Right.writeMicroseconds(1300);
- delay(time);
- }
- void moveLeft(int time)
- {
- Left.writeMicroseconds(1300);
- Right.writeMicroseconds(1300);
- delay(time);
- }
- void moveRight(int time)
- {
- Left.writeMicroseconds(1700);
- Right.writeMicroseconds(1700);
- delay(time);
- }
- void moveBackward(int time)
- {
- Left.writeMicroseconds(1300);
- Right.writeMicroseconds(1700);
- delay(time);
- }
- void loop(){
- digitalWrite(trigger, HIGH);
- delay(10);
- digitalWrite(trigger, LOW);
- duration = pulseIn(echo, HIGH);
- distance = (duration/2) / 29.1;
- Serial.println(distance);
- if(distance < 15) {
- moveBackward(2000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement