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

Dependencies:   mbed

main.cpp

Committer:
tylerjw
Date:
2012-10-18
Revision:
0:c674d8c52a7e

File content as of revision 0:c674d8c52a7e:

#include "mbed.h"

DigitalOut myled(LED1);
AnalogIn battery(p19);
DigitalOut battery_warning(p24);
Serial pc(USBTX, USBRX);

int main() {
    pc.baud(9600);
    const float BAT_MUL = 10.26;
    float sample;

    while(1) {
        sample = battery.read();
        pc.printf("VBat: %4.3f, ADC: %4.3f, Vadc: %4.3f\r\n", sample*BAT_MUL, sample, sample*3.3);
        if(sample*BAT_MUL < 6.4)
            battery_warning = 0;
        else
            battery_warning = 1;
        wait(1);
    }
}