ABELI / Mbed 2 deprecated Ultrasonic

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     float inch;
00023     float cm;
00024     pc.baud(115200);
00025     
00026     while (1){
00027         adc = ain.read();           // read analog as a float
00028         volts = adc * 3.3;          // convert to volts
00029         //inches = volts / 0.0064;    // 3.3V supply: 6.4mV per inch
00030         inches = volts / 0.0098;    // 5V supply: 9.8mV per inch
00031         feet = (int) inches / 12;   // inches to feet (trunc)
00032         in = (int) inches % 12;     // remainder -> in(ches)
00033         cm=in*2.54;
00034         pc.printf("%8.2f adc %8.2fV %8.2f in %d'%d\   %8.2fcm ""\n", adc, volts, inches, feet, in,cm);
00035         //pc.printf("%8.2f cm""\r",cm);
00036  
00037         //wait(0.05);
00038         wait(0.5);                 // 20Hz update rate ; note we aren't truly synchronized to the device or anything...   
00039     }
00040 }
00041