3d4af2ebac3596bfd01959628745c26eaa66f1a9
[challenge-bot] / arduino-sketches / right_sonar / right_sonar.ino
1 /*
2 This program is free software: you can redistribute it and/or modify
3 it under the terms of the GNU Affero General Public License as
4 published by the Free Software Foundation, either version 3 of the
5 License, or (at your option) any later version.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU Affero General Public License for more details.
11
12 You should have received a copy of the GNU Affero General Public License
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15 int right_echo_pin = 6;
16 int right_trigger_pin = 7;
17
18 int ping_microseconds = 0;
19 double sound_cm_per_microsecond_at_sea_level = 0.034029;
20 int count = 0;
21
22 void setup(){
23 Serial.begin(9600);
24 pinMode(right_echo_pin, INPUT);
25 pinMode(right_trigger_pin, OUTPUT);}
26
27 void loop(){
28 // make sure trigger pin is off, a.k.a. LOW
29 digitalWrite(right_trigger_pin, LOW);
30 delayMicroseconds(2);
31
32 // send a 10 microsecond HIGH pulse to the trigger pin
33 digitalWrite(right_trigger_pin, HIGH);
34 delayMicroseconds(10); // leave the pin on for 10 microseconds
35 digitalWrite(right_trigger_pin, LOW);
36
37 ping_microseconds = pulseIn(right_echo_pin, HIGH);
38
39 // wait for the sonar sensor hardware to recover from pinging
40 delayMicroseconds(50);
41
42 // print out the pulse time
43 Serial.print(ping_microseconds);
44 Serial.print(" = ping time (microseconds), ");
45 Serial.print(ping_microseconds * sound_cm_per_microsecond_at_sea_level / 2);
46 Serial.print(" = distance (cm). #");
47 Serial.println(count++);
48
49 // wait so it's easier to read the serial monitor.
50 // change delay to 0 for fullspeed.
51 // default is 333, which is about 1/3 of a second
52 delay(333);}