X-Git-Url: http://challenge-bot.com/repos/?p=challenge-bot;a=blobdiff_plain;f=build-stages%2Fb_left_sonar%2Fb_left_sonar.ino;fp=build-stages%2Fb_left_sonar%2Fb_left_sonar.ino;h=4b648b238c8131902b27c1a4f11b6fbe284b103a;hp=0000000000000000000000000000000000000000;hb=35ffc3435298c78927cd70d765d6291f16325e5d;hpb=4f98acb2deaa2b3b89ee1cc212ca3109ba4ad973 diff --git a/build-stages/b_left_sonar/b_left_sonar.ino b/build-stages/b_left_sonar/b_left_sonar.ino new file mode 100644 index 0000000..4b648b2 --- /dev/null +++ b/build-stages/b_left_sonar/b_left_sonar.ino @@ -0,0 +1,52 @@ +/* + 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 left_echo_pin = 11; +int left_trigger_pin = 12; + +int ping_microseconds = 0; +double sound_cm_per_microsecond_at_sea_level = 0.034029; +int count = 0; + +void setup(){ + Serial.begin(9600); + pinMode(left_echo_pin, INPUT); + pinMode(left_trigger_pin, OUTPUT);} + +void loop(){ + // make sure trigger pin is off, a.k.a. LOW + digitalWrite(left_trigger_pin, LOW); + delayMicroseconds(2); + + // send a 10 microsecond HIGH pulse to the trigger pin + digitalWrite(left_trigger_pin, HIGH); + delayMicroseconds(10); // leave the pin on for 10 microseconds + digitalWrite(left_trigger_pin, LOW); + + ping_microseconds = pulseIn(left_echo_pin, HIGH); + + // wait for the sonar sensor hardware to recover from pinging + delayMicroseconds(50); + + // print out the pulse time + Serial.print(ping_microseconds); + Serial.print(" = ping time (microseconds), "); + Serial.print(ping_microseconds * sound_cm_per_microsecond_at_sea_level / 2); + Serial.print(" = distance (cm). #"); + 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);}