create a list of parts for a single robot
[challenge-bot] / arduino-sketches / both_sonars / both_sonars.ino
CommitLineData
481b24d3 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 */
15int right_echo_pin = 6;
16int right_trigger_pin = 7;
17
18int left_echo_pin = 11;
19int left_trigger_pin = 12;
20
21int right_ping_microseconds = 0;
22int left_ping_microseconds = 0;
23double sound_cm_per_microsecond_at_sea_level = 0.034029;
24int count = 0;
25
26void on(int pin){
27 digitalWrite(pin, HIGH);}
28
29void off(int pin){
30 digitalWrite(pin, LOW);}
31
32int ping(int trigger, int echo){
33 int ping_time = 0;
34 // turn off trigger
35 off(trigger);
36 delayMicroseconds(2);
37 // turn on the trigger and leave it on long enough for the
38 // sonar sensor to notice
39 on(trigger);
40 delayMicroseconds(10);
41 off(trigger);
42 ping_time = pulseIn(echo, HIGH);
43 if(ping_time <= 0){
44 ping_time = 3000;}
45 // sonar needs some time to recover before pinging again,
46 // so make sure it gets enough sleep right here. 50 milliseconds
47 delay(50);
48 return ping_time;}
49
50void setup(){
51 Serial.begin(9600);
52 pinMode(right_echo_pin, INPUT);
53 pinMode(right_trigger_pin, OUTPUT);
54 pinMode(left_echo_pin, INPUT);
55 pinMode(left_trigger_pin, OUTPUT);}
56
57void loop(){
58 right_ping_microseconds = ping(right_trigger_pin, right_echo_pin);
59 left_ping_microseconds = ping(left_trigger_pin, left_echo_pin);
60 // print out the pulse time
61 Serial.print(right_ping_microseconds);
62 Serial.print(" = right ping microseconds, ");
63 Serial.print(left_ping_microseconds);
64 Serial.print(" = left ping microseconds. #");
65 Serial.println(count++);
66
67 // wait so it's easier to read the serial monitor.
68 // change delay to 0 for fullspeed.
69 // default is 333, which is about 1/3 of a second
70 delay(333);}