Example code for monitoring battery voltage and lighting a LED when battery voltage is below a threshold.

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 DigitalOut myled(LED1);
00004 AnalogIn battery(p19);
00005 DigitalOut battery_warning(p24);
00006 Serial pc(USBTX, USBRX);
00007 
00008 int main() {
00009     pc.baud(9600);
00010     const float BAT_MUL = 10.26;
00011     float sample;
00012 
00013     while(1) {
00014         sample = battery.read();
00015         pc.printf("VBat: %4.3f, ADC: %4.3f, Vadc: %4.3f\r\n", sample*BAT_MUL, sample, sample*3.3);
00016         if(sample*BAT_MUL < 6.4)
00017             battery_warning = 0;
00018         else
00019             battery_warning = 1;
00020         wait(1);
00021     }
00022 }