start with left sonar and left motor
[challenge-bot] / build-stages / b_left_sonar / b_left_sonar.ino
CommitLineData
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 */
35ffc343 15int left_echo_pin = 11;
16int left_trigger_pin = 12;
315f0739 17
18int ping_microseconds = 0;
19double sound_cm_per_microsecond_at_sea_level = 0.034029;
20int count = 0;
21
22void setup(){
23 Serial.begin(9600);
35ffc343 24 pinMode(left_echo_pin, INPUT);
25 pinMode(left_trigger_pin, OUTPUT);}
315f0739 26
27void loop(){
28 // make sure trigger pin is off, a.k.a. LOW
35ffc343 29 digitalWrite(left_trigger_pin, LOW);
315f0739 30 delayMicroseconds(2);
31
32 // send a 10 microsecond HIGH pulse to the trigger pin
35ffc343 33 digitalWrite(left_trigger_pin, HIGH);
315f0739 34 delayMicroseconds(10); // leave the pin on for 10 microseconds
35ffc343 35 digitalWrite(left_trigger_pin, LOW);
315f0739 36
35ffc343 37 ping_microseconds = pulseIn(left_echo_pin, HIGH);
315f0739 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);}