From: daniel watson Date: Fri, 8 Nov 2013 05:35:28 +0000 (-0800) Subject: add both sonars X-Git-Url: http://challenge-bot.com/repos/?p=challenge-bot;a=commitdiff_plain;h=481b24d3794404dae8ecd9047cb7b9b5d0183795 add both sonars --- diff --git a/arduino-sketches/both_sonars/both-sonars.fzz b/arduino-sketches/both_sonars/both-sonars.fzz new file mode 100644 index 0000000..70765e3 Binary files /dev/null and b/arduino-sketches/both_sonars/both-sonars.fzz differ diff --git a/arduino-sketches/both_sonars/both_sonars.ino b/arduino-sketches/both_sonars/both_sonars.ino new file mode 100644 index 0000000..2d850ff --- /dev/null +++ b/arduino-sketches/both_sonars/both_sonars.ino @@ -0,0 +1,70 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ +int right_echo_pin = 6; +int right_trigger_pin = 7; + +int left_echo_pin = 11; +int left_trigger_pin = 12; + +int right_ping_microseconds = 0; +int left_ping_microseconds = 0; +double sound_cm_per_microsecond_at_sea_level = 0.034029; +int count = 0; + +void on(int pin){ + digitalWrite(pin, HIGH);} + +void off(int pin){ + digitalWrite(pin, LOW);} + +int ping(int trigger, int echo){ + int ping_time = 0; + // turn off trigger + off(trigger); + delayMicroseconds(2); + // turn on the trigger and leave it on long enough for the + // sonar sensor to notice + on(trigger); + delayMicroseconds(10); + off(trigger); + ping_time = pulseIn(echo, HIGH); + if(ping_time <= 0){ + ping_time = 3000;} + // sonar needs some time to recover before pinging again, + // so make sure it gets enough sleep right here. 50 milliseconds + delay(50); + return ping_time;} + +void setup(){ + Serial.begin(9600); + pinMode(right_echo_pin, INPUT); + pinMode(right_trigger_pin, OUTPUT); + pinMode(left_echo_pin, INPUT); + pinMode(left_trigger_pin, OUTPUT);} + +void loop(){ + right_ping_microseconds = ping(right_trigger_pin, right_echo_pin); + left_ping_microseconds = ping(left_trigger_pin, left_echo_pin); + // print out the pulse time + Serial.print(right_ping_microseconds); + Serial.print(" = right ping microseconds, "); + Serial.print(left_ping_microseconds); + Serial.print(" = left ping microseconds. #"); + Serial.println(count++); + + // wait so it's easier to read the serial monitor. + // change delay to 0 for fullspeed. + // default is 333, which is about 1/3 of a second + delay(333);}