send analog info from MaxBotix range finder with xbee. Download onto the End Device with the sensor.
Dependencies: C12832_lcd mbed xbee_lib
main.cpp@0:8aac1b3be648, 2015-02-16 (annotated)
- Committer:
- dannellyz
- Date:
- Mon Feb 16 02:27:53 2015 +0000
- Revision:
- 0:8aac1b3be648
round1;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dannellyz | 0:8aac1b3be648 | 1 | #include "mbed.h" |
dannellyz | 0:8aac1b3be648 | 2 | #include "C12832_lcd.h" |
dannellyz | 0:8aac1b3be648 | 3 | #include "xbee.h" |
dannellyz | 0:8aac1b3be648 | 4 | C12832_LCD lcd; |
dannellyz | 0:8aac1b3be648 | 5 | AnalogIn ain(p17); |
dannellyz | 0:8aac1b3be648 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
dannellyz | 0:8aac1b3be648 | 7 | xbee xbee1(p9,p10,p30); //Initalise xbee_lib |
dannellyz | 0:8aac1b3be648 | 8 | |
dannellyz | 0:8aac1b3be648 | 9 | int main() { |
dannellyz | 0:8aac1b3be648 | 10 | float adc, volts, inches; |
dannellyz | 0:8aac1b3be648 | 11 | int feet, in,out; |
dannellyz | 0:8aac1b3be648 | 12 | char send_data[2]; //Xbee buffer size is 202 bytes |
dannellyz | 0:8aac1b3be648 | 13 | |
dannellyz | 0:8aac1b3be648 | 14 | lcd.cls(); |
dannellyz | 0:8aac1b3be648 | 15 | lcd.locate(0,2); |
dannellyz | 0:8aac1b3be648 | 16 | |
dannellyz | 0:8aac1b3be648 | 17 | while (1){ |
dannellyz | 0:8aac1b3be648 | 18 | adc = ain.read(); // read analog as a float |
dannellyz | 0:8aac1b3be648 | 19 | volts = adc * 3.3; // convert to volts |
dannellyz | 0:8aac1b3be648 | 20 | inches = (volts / 0.0064) ; // 3.3V supply: ~6.4mV per inch |
dannellyz | 0:8aac1b3be648 | 21 | feet = (int) inches / 12; // inches to feet (trunc) |
dannellyz | 0:8aac1b3be648 | 22 | in = (int) inches % 12; // remainder: in(ches) |
dannellyz | 0:8aac1b3be648 | 23 | out = (int) inches; |
dannellyz | 0:8aac1b3be648 | 24 | pc.printf("adc:%8.2f %8.2fV in:%8.2f %d'%d\"\n", |
dannellyz | 0:8aac1b3be648 | 25 | adc, volts, inches, feet, in); |
dannellyz | 0:8aac1b3be648 | 26 | |
dannellyz | 0:8aac1b3be648 | 27 | lcd.printf("%.2fV in: %.2f send: %d\n", |
dannellyz | 0:8aac1b3be648 | 28 | volts, inches,out); |
dannellyz | 0:8aac1b3be648 | 29 | if(out < 10){sprintf (send_data, "0%d", out);} |
dannellyz | 0:8aac1b3be648 | 30 | else{sprintf (send_data, "%d", out);} |
dannellyz | 0:8aac1b3be648 | 31 | xbee1.SendData(send_data); //Send data to XBee |
dannellyz | 0:8aac1b3be648 | 32 | wait(0.05); // ~20Hz update rate ; note we aren't |
dannellyz | 0:8aac1b3be648 | 33 | // truly synchronized ... |
dannellyz | 0:8aac1b3be648 | 34 | } |
dannellyz | 0:8aac1b3be648 | 35 | } |