Code to use a MaxSonar for range detection.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 /** 00004 This source code is used to connect a MaxSonar detector. It is written to allow multiple sonar's. 00005 If you want the sonar to constantly poll constantly either: don't attach the pin, or delete the code relating to 'Enable' 00006 00007 This code is public domain. 00008 **/ 00009 00010 AnalogIn Sensor(p20); //Analog port that is connected to the MaxSonar range detector 00011 DigitalOut Enable(p18); //Remove this if you want a constant poll 00012 DigitalOut led(LED1); //led, useful to see when the sonar is polling 00013 Serial serialPort(USBTX, USBRX); //To talk back to your computer 00014 00015 int main() { 00016 float adc, volts, in; 00017 00018 while (1) { 00019 Enable = 1; //Enable the sonar 00020 led = 1; //Turn the LED on to show let the user know its working 00021 wait_us(25);//Wait 20uS + 5uS safety margin 00022 adc = Sensor.read(); //Read the sensor value 00023 Enable = 0; //Disable the sonar 00024 led = 0; //Turn the LED off 00025 volts = ( adc * 3.3 ); // convert to volts 00026 in = volts / 0.0032 * 0.3937; // 3.3V supply: 3.2mV per cm * CM -> IN conversion factor 00027 00028 serialPort.printf("%8.5f adc %8.5fV %8.2f\"\n", adc, volts, in); 00029 00030 wait(0.5); 00031 } 00032 }
Generated on Mon Jul 18 2022 03:53:34 by
1.7.2