long Ping::getDistance() {//inches long time = getDuration(); if(units == INCHES) { time = microsecondsToInches(time); } else { time = microsecondsToCentimeters(time); } return time; }
void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); // The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(inPin, INPUT); duration = pulseIn(inPin, HIGH); // convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.println(cm, DEC); if (cm < 3) { // play the note corresponding to this sensor: tone(8, notes[4], 30); } if ((cm >= 3) && (cm <= 9)) { // play the note corresponding to this sensor: tone(8, notes[0], 30); } if ((cm >= 10) && (cm <= 19)) { // play the note corresponding to this sensor: tone(8, notes[1], 30); } if ((cm >= 20) && (cm <= 29)) { // play the note corresponding to this sensor: tone(8, notes[2], 30); } if ((cm >= 30) && (cm <= 49)) { // play the note corresponding to this sensor: tone(8, notes[3], 30); } delay(100); }
long Sonar::getInchesSampled(long samples) { return microsecondsToInches(this->getRawSampled(samples)); }
long Sonar::getInches() { return microsecondsToInches(this->getRaw()); }