Created
December 16, 2014 09:13
-
-
Save goFrendiAsgard/577276cb33097807d270 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * Gunvarrel v 0.0.3 | |
| * Purpose: Simple sonar robot | |
| * Description: Good enough | |
| * Author: Go Frendi Gunawan | |
| */ | |
| int left_motor = 11; // left motor | |
| int right_motor = 10; // right motor | |
| int sonar_trig = 12; // sonar trigger | |
| int sonar_echo = 13; // sonar echo | |
| void setup(){ | |
| Serial.begin(9600); | |
| pinMode(left_motor, OUTPUT); | |
| pinMode(right_motor, OUTPUT); | |
| pinMode(sonar_trig, OUTPUT); | |
| pinMode(sonar_echo, INPUT ); | |
| } | |
| void straight(){ | |
| analogWrite(left_motor, 100); | |
| analogWrite(right_motor, 100); | |
| } | |
| void turn(){ | |
| analogWrite(left_motor, 200); | |
| analogWrite(right_motor, 0); | |
| } | |
| void loop(){ | |
| // shout out | |
| digitalWrite(sonar_trig, LOW); | |
| digitalWrite(sonar_trig, HIGH); | |
| delayMicroseconds(20); | |
| digitalWrite(sonar_trig, LOW); | |
| // listen | |
| int duration = pulseIn(sonar_echo, HIGH); | |
| int distance = (duration/2) / 29.1; | |
| Serial.println(distance); | |
| if(distance < 20){ | |
| turn(); | |
| }else{ | |
| straight(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment