315f0739 |
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 | */ |
661ac21e |
15 | // yellow wire |
35ffc343 |
16 | int left_echo_pin = 11; |
661ac21e |
17 | // blue wire |
35ffc343 |
18 | int left_trigger_pin = 12; |
315f0739 |
19 | |
20 | int ping_microseconds = 0; |
21 | double sound_cm_per_microsecond_at_sea_level = 0.034029; |
22 | int count = 0; |
23 | |
24 | void setup(){ |
25 | Serial.begin(9600); |
35ffc343 |
26 | pinMode(left_echo_pin, INPUT); |
27 | pinMode(left_trigger_pin, OUTPUT);} |
315f0739 |
28 | |
29 | void loop(){ |
30 | // make sure trigger pin is off, a.k.a. LOW |
35ffc343 |
31 | digitalWrite(left_trigger_pin, LOW); |
315f0739 |
32 | delayMicroseconds(2); |
33 | |
34 | // send a 10 microsecond HIGH pulse to the trigger pin |
35ffc343 |
35 | digitalWrite(left_trigger_pin, HIGH); |
315f0739 |
36 | delayMicroseconds(10); // leave the pin on for 10 microseconds |
35ffc343 |
37 | digitalWrite(left_trigger_pin, LOW); |
315f0739 |
38 | |
35ffc343 |
39 | ping_microseconds = pulseIn(left_echo_pin, HIGH); |
315f0739 |
40 | |
41 | // wait for the sonar sensor hardware to recover from pinging |
42 | delayMicroseconds(50); |
43 | |
44 | // print out the pulse time |
45 | Serial.print(ping_microseconds); |
46 | Serial.print(" = ping time (microseconds), "); |
47 | Serial.print(ping_microseconds * sound_cm_per_microsecond_at_sea_level / 2); |
48 | Serial.print(" = distance (cm). #"); |
49 | Serial.println(count++); |
50 | |
51 | // wait so it's easier to read the serial monitor. |
52 | // change delay to 0 for fullspeed. |
53 | // default is 333, which is about 1/3 of a second |
54 | delay(333);} |