diff --git a/readme.txt b/readme.txt index 81de85258..5c4bef8cc 100644 --- a/readme.txt +++ b/readme.txt @@ -47,6 +47,7 @@ UPDATES 0007 +Adding Sonar library for controlling Parallax Ultrasonic PING))) sensors. Defining binary constants: e.g. B1010 is 6. Digital pins 0 and 1 can be used for i/o until a call to Serial.begin(). diff --git a/targets/libraries/Sonar/Sonar.cpp b/targets/libraries/Sonar/Sonar.cpp new file mode 100755 index 000000000..59b856f05 --- /dev/null +++ b/targets/libraries/Sonar/Sonar.cpp @@ -0,0 +1,82 @@ +/* + Sonar.cpp - Parallax PING))) Ultrasonic Sensor Library + Copyright (c) 2006 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + For more information on the Parallax PING))) Ultrasonic Sensor, see: + http://www.parallax.com/detail.asp?product_id=28015 +*/ + +/****************************************************************************** + * Includes + ******************************************************************************/ + +#include "WConstants.h" +#include "Sonar.h" + +/****************************************************************************** + * Definitions + ******************************************************************************/ + +/****************************************************************************** + * Constructors + ******************************************************************************/ + +Sonar::Sonar(uint8_t signal) +{ + _pinSignal = signal; +} + +/****************************************************************************** + * User API + ******************************************************************************/ + +unsigned long Sonar::readMicroseconds() +{ + pinMode(_pinSignal, OUTPUT); + + // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. + // We give a short LOW pulse beforehand to ensure a clean HIGH pulse. + digitalWrite(_pinSignal, LOW); + delayMicroseconds(2); + digitalWrite(_pinSignal, HIGH); + delayMicroseconds(5); + digitalWrite(_pinSignal, LOW); + + // The same pin is used to read the signal from the PING))): a HIGH + // pulse whose duration is the time from the sending of the ping to + // to reception of the echo. + pinMode(_pinSignal, INPUT); + + return pulseIn(_pinSignal, HIGH); +} + +unsigned long Sonar::readInches() +{ + // According to Parallax's datasheet for the PING))), there are + // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per + // second). This gives the distance travelled by the ping, outbound + // and return, so we divide by 2 to get the distance of the obstacle. + // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf + return readMicroseconds() / 74 / 2; +} + +unsigned long Sonar::readCentimeters() +{ + // 73.746 microseconds per inch works out to about 29 microseconds per + // centimeter (there are 2.54 centimeters per inch). + return readMicroseconds() / 29 / 2; +} diff --git a/targets/libraries/Sonar/Sonar.h b/targets/libraries/Sonar/Sonar.h new file mode 100755 index 000000000..3dee8c55c --- /dev/null +++ b/targets/libraries/Sonar/Sonar.h @@ -0,0 +1,40 @@ +/* + Sonar.h - Parallax PING))) Ultrasonic Sensor Library + Copyright (c) 2006 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + For more information on the Parallax PING))) Ultrasonic Sensor, see: + http://www.parallax.com/detail.asp?product_id=28015 +*/ + +#ifndef Sonar_h +#define Sonar_h + +#include + +class Sonar +{ + private: + uint8_t _pinSignal; + public: + Sonar(uint8_t); + unsigned long readMicroseconds(); + unsigned long readInches(); + unsigned long readCentimeters(); +}; + +#endif + diff --git a/targets/libraries/Sonar/examples/sonar/sonar.pde b/targets/libraries/Sonar/examples/sonar/sonar.pde new file mode 100644 index 000000000..36e487a15 --- /dev/null +++ b/targets/libraries/Sonar/examples/sonar/sonar.pde @@ -0,0 +1,22 @@ +#include + +Ultrasound ultrasound(2); + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + Serial.print("Ping took: "); + Serial.print(ultrasound.readMicroseconds()); + Serial.print(" us. Distance is: "); + Serial.print(ultrasound.readCentimeters()); + Serial.print(" cm, "); + Serial.print(ultrasound.readInches()); + Serial.print(" in."); + Serial.println(); + + delay(1000); +} diff --git a/targets/libraries/Sonar/keywords.txt b/targets/libraries/Sonar/keywords.txt new file mode 100644 index 000000000..20fa05706 --- /dev/null +++ b/targets/libraries/Sonar/keywords.txt @@ -0,0 +1,22 @@ +####################################### +# Syntax Coloring Map For Ultrasound +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Sonar KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +readMicroseconds KEYWORD2 +readInches KEYWORD2 +readCentimeters KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +