/* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ int right_echo_pin = 6; int right_trigger_pin = 7; int ping_microseconds = 0; double sound_cm_per_microsecond_at_sea_level = 0.034029; int count = 0; void setup(){ Serial.begin(9600); pinMode(right_echo_pin, INPUT); pinMode(right_trigger_pin, OUTPUT);} void loop(){ // make sure trigger pin is off, a.k.a. LOW digitalWrite(right_trigger_pin, LOW); delayMicroseconds(2); // send a 10 microsecond HIGH pulse to the trigger pin digitalWrite(right_trigger_pin, HIGH); delayMicroseconds(10); // leave the pin on for 10 microseconds digitalWrite(right_trigger_pin, LOW); ping_microseconds = pulseIn(right_echo_pin, HIGH); // wait for the sonar sensor hardware to recover from pinging delayMicroseconds(50); // print out the pulse time Serial.print(ping_microseconds); Serial.print(" = ping time (microseconds), "); Serial.print(ping_microseconds * sound_cm_per_microsecond_at_sea_level / 2); Serial.print(" = distance (cm). #"); Serial.println(count++); // wait so it's easier to read the serial monitor. // change delay to 0 for fullspeed. // default is 333, which is about 1/3 of a second delay(333);}