(aside: I think something went south on the first attempt of this post so this may be a duplicate)
I'm writing unit/feature tests of the serial protocol API and I'm working on the analog read command (Ra<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 changed 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 value into a float
bool lowBattery() {
//...
float voltage = analogRead(VOLTAGE);
and compares the value with a precalculated low_voltage value:
//...
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. So, 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.
:
On the BiBoard, there is no detect pin connected to the battery. You may see that the pin definition for BiBoard2 has not been released.
The ESP32's pins can only take 3.3V input. So, the resistor divides the voltage to ensure the pins are safe. The conversion factor is the ratio of the resisters of a voltage divider. The calculated voltage 2813 is used to compare with 4096. It's precalculated to reduce repeating calculations.