Test program for MaxSonar EZ1 sonar ranger using analog input.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 // MaxSonar EZ1 test program
00004 // by Michael Shimniok http://www.bot-thoughts.com/
00005 //
00006 // Based on datasheet here: http://www.maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf
00007 // Reads from AN (analog) pin connected to mbed p20, assumes 3.3V supply to EZ1 module.
00008 //
00009 // mbed -> EZ1
00010 // -----------
00011 // VOUT -> +5
00012 // GND  -> GND
00013 // p20  -> AN
00014 //
00015 
00016 AnalogIn ain(p20);
00017 Serial pc(USBTX, USBRX); // tx, rx
00018 
00019 int main() {
00020     float adc, volts, inches;
00021     int feet, in;
00022     
00023     pc.baud(115200);
00024     
00025     while (1){
00026         adc = ain.read();           // read analog as a float
00027         volts = adc * 3.3;          // convert to volts
00028         inches = volts / 0.0064;    // 3.3V supply: 6.4mV per inch
00029         feet = (int) inches / 12;   // inches to feet (trunc)
00030         in = (int) inches % 12;     // remainder -> in(ches)
00031         
00032         pc.printf("%8.2f adc %8.2fV %8.2f in %d'%d\"\n", adc, volts, inches, feet, in);
00033 
00034         wait(0.05);                 // 20Hz update rate ; note we aren't truly synchronized to the device or anything...   
00035     }
00036 }
00037 
00038