For testing battery life on the harp project.

Dependencies:   Servo mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 
00004 DigitalOut myled(LED1);
00005 AnalogIn battery(p19);
00006 DigitalOut battery_warning(p24);
00007 Serial pc(USBTX, USBRX);
00008 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00009 Timer t;
00010 
00011 void warning_light_check(float);
00012 
00013 int main() {
00014     pc.baud(9600);
00015     const float BAT_MUL = 10.26;
00016     const float BAT_LIM = 7.0;
00017     float sample;
00018     float voltage;
00019     
00020     Servo left_s(p21);
00021     Servo right_s(p22);
00022     
00023     left_s.calibrate_max(0.0007);
00024     left_s.calibrate_min(-0.0014);
00025     right_s.calibrate(0.0009);
00026     
00027     FILE *fp = fopen("/local/battery.csv", "w");
00028     fputs("Time,Battery\r\n", fp);
00029     
00030     t.start();
00031 
00032     while(1) {
00033         sample = battery.read();
00034         voltage = sample*BAT_MUL;
00035         fprintf(fp, "%f,%f\r\n", t.read(), voltage);
00036         pc.printf("VBat: %4.3f, ADC: %4.3f, Vadc: %4.3f\r\n", voltage, sample, sample*3.3);
00037         left_s = 1.0;
00038         warning_light_check(voltage);
00039         
00040         wait(0.5);
00041         
00042         sample = battery.read();
00043         voltage = sample*BAT_MUL;
00044         fprintf(fp, "%f,%f\r\n", t.read(), voltage);
00045         pc.printf("VBat: %4.3f, ADC: %4.3f, Vadc: %4.3f\r\n", voltage, sample, sample*3.3);
00046         left_s = 0.0;
00047         warning_light_check(voltage);
00048         
00049         wait(0.5);
00050         
00051         if(voltage < BAT_LIM)
00052             break;
00053     }
00054     fclose(fp);
00055     myled = 1;   
00056 }
00057 
00058 void warning_light_check(float voltage)
00059 {
00060     if(voltage < 6.4)
00061         battery_warning = 0;
00062     else
00063         battery_warning = 1;
00064     wait(1);
00065 }