bla

Dependencies:   mbed

Committer:
thesteira
Date:
Wed Jan 13 16:42:10 2016 +0000
Revision:
0:c98fab4c30c9
Ultraschall Test

Who changed what in which revision?

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