Tyler Weaver
/
battery_monitor_demo
Example code for monitoring battery voltage and lighting a LED when battery voltage is below a threshold.
main.cpp@0:c674d8c52a7e, 2012-10-18 (annotated)
- Committer:
- tylerjw
- Date:
- Thu Oct 18 20:20:01 2012 +0000
- Revision:
- 0:c674d8c52a7e
Battery Monitor Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerjw | 0:c674d8c52a7e | 1 | #include "mbed.h" |
tylerjw | 0:c674d8c52a7e | 2 | |
tylerjw | 0:c674d8c52a7e | 3 | DigitalOut myled(LED1); |
tylerjw | 0:c674d8c52a7e | 4 | AnalogIn battery(p19); |
tylerjw | 0:c674d8c52a7e | 5 | DigitalOut battery_warning(p24); |
tylerjw | 0:c674d8c52a7e | 6 | Serial pc(USBTX, USBRX); |
tylerjw | 0:c674d8c52a7e | 7 | |
tylerjw | 0:c674d8c52a7e | 8 | int main() { |
tylerjw | 0:c674d8c52a7e | 9 | pc.baud(9600); |
tylerjw | 0:c674d8c52a7e | 10 | const float BAT_MUL = 10.26; |
tylerjw | 0:c674d8c52a7e | 11 | float sample; |
tylerjw | 0:c674d8c52a7e | 12 | |
tylerjw | 0:c674d8c52a7e | 13 | while(1) { |
tylerjw | 0:c674d8c52a7e | 14 | sample = battery.read(); |
tylerjw | 0:c674d8c52a7e | 15 | pc.printf("VBat: %4.3f, ADC: %4.3f, Vadc: %4.3f\r\n", sample*BAT_MUL, sample, sample*3.3); |
tylerjw | 0:c674d8c52a7e | 16 | if(sample*BAT_MUL < 6.4) |
tylerjw | 0:c674d8c52a7e | 17 | battery_warning = 0; |
tylerjw | 0:c674d8c52a7e | 18 | else |
tylerjw | 0:c674d8c52a7e | 19 | battery_warning = 1; |
tylerjw | 0:c674d8c52a7e | 20 | wait(1); |
tylerjw | 0:c674d8c52a7e | 21 | } |
tylerjw | 0:c674d8c52a7e | 22 | } |