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.
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.
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/>.
15 int left_echo_pin
= 11;
16 int left_trigger_pin
= 12;
18 int ping_microseconds
= 0;
19 double sound_cm_per_microsecond_at_sea_level
= 0.034029;
24 pinMode(left_echo_pin
, INPUT
);
25 pinMode(left_trigger_pin
, OUTPUT
);}
28 // make sure trigger pin is off, a.k.a. LOW
29 digitalWrite(left_trigger_pin
, LOW
);
32 // send a 10 microsecond HIGH pulse to the trigger pin
33 digitalWrite(left_trigger_pin
, HIGH
);
34 delayMicroseconds(10); // leave the pin on for 10 microseconds
35 digitalWrite(left_trigger_pin
, LOW
);
37 ping_microseconds
= pulseIn(left_echo_pin
, HIGH
);
39 // wait for the sonar sensor hardware to recover from pinging
40 delayMicroseconds(50);
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
++);
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