Created
February 12, 2017 14:06
-
-
Save danya02/277666d1ef000366bcf5fee3326e8c9a to your computer and use it in GitHub Desktop.
Arduino turtle avoidance program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
#include <Ultrasonic.h> | |
Servo servo; | |
Ultrasonic ultrasonic(13,12); | |
int l=0; | |
int r=0; | |
void forward() { | |
digitalWrite(5, 1); | |
digitalWrite(7, 1); | |
digitalWrite(6, 0); | |
digitalWrite(8, 0); | |
} | |
void back() { | |
digitalWrite(5, 0); | |
digitalWrite(7, 0); | |
digitalWrite(6, 1); | |
digitalWrite(8, 1); | |
} | |
void right() { | |
digitalWrite(5, 0); | |
digitalWrite(7, 1); | |
digitalWrite(6, 1); | |
digitalWrite(8, 0); | |
} | |
void left() { | |
digitalWrite(5, 1); | |
digitalWrite(7, 0); | |
digitalWrite(6, 0); | |
digitalWrite(8, 1); | |
} | |
void halt() { | |
digitalWrite(5, 0); | |
digitalWrite(7, 0); | |
digitalWrite(6, 0); | |
digitalWrite(8, 0); | |
} | |
void head_right() { | |
servo.write(0); | |
} | |
void head_center() { | |
servo.write(90); | |
} | |
void head_left() { | |
servo.write(180); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
servo.attach(11); | |
pinMode(3, OUTPUT); | |
pinMode(5, OUTPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, OUTPUT); | |
digitalWrite(3, 1); | |
servo.write(90); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
head_center(); | |
forward(); | |
while (ultrasonic.Ranging(CM) > 20){delay(10);} | |
halt(); | |
head_left(); | |
delay(500); | |
l = ultrasonic.Ranging(CM); | |
delay(500); | |
head_right(); | |
delay(500); | |
r = ultrasonic.Ranging(CM); | |
delay(250); | |
if (l>r){head_left();left();} | |
else {head_right();right();} | |
delay(500); | |
halt(); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment