Range Finder (Version 1) - Indicator LEDs at 50cm and 70cm

Dependencies:   mbed

Fork of MaxSonar_EZ1_Analog by Michael Shimniok

Committer:
shimniok
Date:
Tue Jan 25 00:05:16 2011 +0000
Revision:
0:9dfac5da16a9
Child:
1:b3590ab2a8ae
Initial Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:9dfac5da16a9 1 #include "mbed.h"
shimniok 0:9dfac5da16a9 2
shimniok 0:9dfac5da16a9 3 // MaxSonar EZ1 test program
shimniok 0:9dfac5da16a9 4 // by Michael Shimniok http://www.bot-thoughts.com/
shimniok 0:9dfac5da16a9 5 //
shimniok 0:9dfac5da16a9 6 // Based on datasheet here: http://www.maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf
shimniok 0:9dfac5da16a9 7 // Reads from AN (analog) pin connected to mbed p20, assumes 3.3V supply to EZ1 module.
shimniok 0:9dfac5da16a9 8 //
shimniok 0:9dfac5da16a9 9 // mbed -> EZ1
shimniok 0:9dfac5da16a9 10 // -----------
shimniok 0:9dfac5da16a9 11 // VOUT -> +5
shimniok 0:9dfac5da16a9 12 // GND -> GND
shimniok 0:9dfac5da16a9 13 // p20 -> AN
shimniok 0:9dfac5da16a9 14 //
shimniok 0:9dfac5da16a9 15
shimniok 0:9dfac5da16a9 16 AnalogIn ain(p20);
shimniok 0:9dfac5da16a9 17 Serial pc(USBTX, USBRX); // tx, rx
shimniok 0:9dfac5da16a9 18
shimniok 0:9dfac5da16a9 19 int main() {
shimniok 0:9dfac5da16a9 20 float adc, volts, inches;
shimniok 0:9dfac5da16a9 21 int feet, in;
shimniok 0:9dfac5da16a9 22
shimniok 0:9dfac5da16a9 23 pc.baud(115200);
shimniok 0:9dfac5da16a9 24
shimniok 0:9dfac5da16a9 25 while (1){
shimniok 0:9dfac5da16a9 26 adc = ain.read(); // read analog as a float
shimniok 0:9dfac5da16a9 27 volts = adc * 3.3; // convert to volts
shimniok 0:9dfac5da16a9 28 inches = volts / 0.0064; // 3.3V supply: 6.4mV per inch
shimniok 0:9dfac5da16a9 29 feet = (int) inches / 12; // inches to feet (trunc)
shimniok 0:9dfac5da16a9 30 in = (int) inches % 12; // remainder -> in(ches)
shimniok 0:9dfac5da16a9 31
shimniok 0:9dfac5da16a9 32 pc.printf("%8.2f adc %8.2fV %8.2f in %d'%d\"\n", adc, volts, inches, feet, in);
shimniok 0:9dfac5da16a9 33
shimniok 0:9dfac5da16a9 34 wait(0.05); // 20Hz update rate ; note we aren't truly synchronized to the device or anything...
shimniok 0:9dfac5da16a9 35 }
shimniok 0:9dfac5da16a9 36 }
shimniok 0:9dfac5da16a9 37
shimniok 0:9dfac5da16a9 38