top of page

Software

Public·127 members

Reading the battery voltage

I'm writing unit/feature tests of the serial protocol API and I'm working on the digital read command (Rd<pin>). The pin  I  use (VOLTAGE, pin 4) comes from the lowBattery function in reaction.h and is defined in opencat.h.


The test is performed on a fully charged battery. I'm having difficulty understanding the response:


=
383
R            

I expected to get a value between 0 - 4096 but at the high end of the range. The lowBattery code reads the valiue into a float.


bool lowBattery() {
    //...
    float voltage = analogRead(VOLTAGE);

and compares the value with a precalculated low_voltage value:


#define LOW_VOLTAGE 6.8
//...
float vFactor = 4096 / 3.3 / 3;
float low_voltage = LOW_VOLTAGE * vFactor;
//...
if (voltage == 0 || voltage < low_voltage ...)

The value of vFactor should be 413.73..., and thus the value of low_voltage is 2813.41...


This doesn't look right. I'm missing something here. Is the command response (383) valid? I expect it to be an integer. I don't understand the calculation in lowBattery.


15 Views

About

Learn programming by teaching Nybble new tricks!

bottom of page