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