Range Finder (Version 1) - Indicator LEDs at 50cm and 70cm
Dependencies: mbed
Fork of MaxSonar_EZ1_Analog by
Diff: main.cpp
- Revision:
- 1:b3590ab2a8ae
- Parent:
- 0:9dfac5da16a9
--- a/main.cpp Tue Jan 25 00:05:16 2011 +0000 +++ b/main.cpp Tue Jun 14 11:12:17 2016 +0000 @@ -1,38 +1,34 @@ +/* Arthur Spencer 14/06/16 +LV-MaxSonar-EZ1 Range Finder +Indicator LED at D0 (always on), Range indicators that come on at 70cm and 50cm + +*/ #include "mbed.h" -// MaxSonar EZ1 test program -// by Michael Shimniok http://www.bot-thoughts.com/ -// -// Based on datasheet here: http://www.maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf -// Reads from AN (analog) pin connected to mbed p20, assumes 3.3V supply to EZ1 module. -// -// mbed -> EZ1 -// ----------- -// VOUT -> +5 -// GND -> GND -// p20 -> AN -// +AnalogIn ain(A0); //Receives analogue signal from range finder +//Serial pc(USBTX, USBRX); -AnalogIn ain(p20); -Serial pc(USBTX, USBRX); // tx, rx +DigitalOut far(D0); +DigitalOut mid(D1); +DigitalOut near(D2); + +int main() +{ + float volts, distance; -int main() { - float adc, volts, inches; - int feet, in; - - pc.baud(115200); - - while (1){ - adc = ain.read(); // read analog as a float - volts = adc * 3.3; // convert to volts - inches = volts / 0.0064; // 3.3V supply: 6.4mV per inch - feet = (int) inches / 12; // inches to feet (trunc) - in = (int) inches % 12; // remainder -> in(ches) - - pc.printf("%8.2f adc %8.2fV %8.2f in %d'%d\"\n", adc, volts, inches, feet, in); - - wait(0.05); // 20Hz update rate ; note we aren't truly synchronized to the device or anything... + while (1) { + volts = ain.read(); + distance = volts*600; //cm +// pc.printf("V = %f Distance = %f\n\n", volts, distance); //Prints voltage and distance +// wait(0.5); + if (distance<30) { + near = 1, mid = 1, far = 1; + } else { + if (distance<50) { + near = 0, mid = 1, far = 1; + } else { + near = 0, mid = 0, far = 1; + } } } - - +} \ No newline at end of file