Last active
December 17, 2015 14:09
-
-
Save Wicker25/5622012 to your computer and use it in GitHub Desktop.
A DIY ultrasonic meter with display.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Title --- ultrasonic_meter.cpp [example] | |
Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com | |
This file is part of Rpi-hw. | |
Rpi-hw 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 Droid Software Foundation version 3 of the License. | |
Rpi-hw 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 Rpi-hw. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
// Include Rpi-hw headers | |
#include <rpi-hw.hpp> | |
#include <rpi-hw/utils.hpp> | |
#include <rpi-hw/display/hd44780.hpp> | |
#include <rpi-hw/sensor/hcsr04.hpp> | |
// Use some namespaces | |
using namespace rpihw; | |
int | |
main( int argc, char *args[] ) { | |
// Create the display controller | |
display::hd44780 lcd( 14, 18, 4, 17, 21, 22 ); | |
// Initialize the 16x2 display | |
lcd.init( 16, 2 ); | |
// Create the ultrasonic sensor controller | |
sensor::hcsr04 meter( 23, 24 ); | |
// Distance from the sensor to an object or surface | |
double distance; | |
// Main loop | |
for ( ;; ) { | |
// Calculate the distance | |
distance = meter.ranging(); | |
// Clear the display | |
lcd.clear(); | |
// Write the distance | |
if ( distance > 100 ) | |
lcd.write( utils::format( "Distance %.2fm", distance / 100 ) ); | |
else | |
lcd.write( utils::format( "Distance %.0fcm", distance ) ); | |
// Wait some time | |
time::sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment