Adding Sonar library for controlling Parallax Ultrasonic PING))) sensors.

This commit is contained in:
David A. Mellis 2006-11-21 18:16:03 +00:00
parent e0dfa25d13
commit 8d6176d0a4
5 changed files with 167 additions and 0 deletions

View File

@ -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().

View File

@ -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;
}

40
targets/libraries/Sonar/Sonar.h Executable file
View File

@ -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 <inttypes.h>
class Sonar
{
private:
uint8_t _pinSignal;
public:
Sonar(uint8_t);
unsigned long readMicroseconds();
unsigned long readInches();
unsigned long readCentimeters();
};
#endif

View File

@ -0,0 +1,22 @@
#include <Ultrasound.h>
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);
}

View File

@ -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)
#######################################