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/>.
16 int left_echo_pin
= 11;
18 int left_trigger_pin
= 12;
20 int ping_microseconds
= 0;
21 double sound_cm_per_microsecond_at_sea_level
= 0.034029;
26 pinMode(left_echo_pin
, INPUT
);
27 pinMode(left_trigger_pin
, OUTPUT
);}
30 // make sure trigger pin is off, a.k.a. LOW
31 digitalWrite(left_trigger_pin
, LOW
);
34 // send a 10 microsecond HIGH pulse to the trigger pin
35 digitalWrite(left_trigger_pin
, HIGH
);
36 delayMicroseconds(10); // leave the pin on for 10 microseconds
37 digitalWrite(left_trigger_pin
, LOW
);
39 ping_microseconds
= pulseIn(left_echo_pin
, HIGH
);
41 // wait for the sonar sensor hardware to recover from pinging
42 delayMicroseconds(50);
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
++);
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