|
|
|
@ -9,8 +9,44 @@
|
|
|
|
|
|
|
|
|
|
AsyncWebServer server(80); |
|
|
|
|
|
|
|
|
|
float averageBatteryVoltage = 0.0f; |
|
|
|
|
int16_t batteryVoltage = -1; //in mV
|
|
|
|
|
|
|
|
|
|
struct ADC_CalibrationValue |
|
|
|
|
{ |
|
|
|
|
float Measure; |
|
|
|
|
int16_t ADC_Value; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const ADC_CalibrationValue calibration[] = { |
|
|
|
|
{ 0.118f, 1 }, |
|
|
|
|
{ 0.343f, 253 }, |
|
|
|
|
{ 0.791f, 801 }, |
|
|
|
|
{ 1.512f, 1705 }, |
|
|
|
|
{ 2.013f, 2306 }, |
|
|
|
|
{ 2.406f, 2796 }, |
|
|
|
|
{ 2.606f, 3058 }, |
|
|
|
|
{ 2.839f, 3423 }, |
|
|
|
|
{ 2.996f, 3726 }, |
|
|
|
|
{ 3.16f, 4094 } |
|
|
|
|
}; |
|
|
|
|
const int8_t calibrationNumValues = sizeof(calibration)/sizeof(calibration[0]); |
|
|
|
|
|
|
|
|
|
float getCalibratedVoltage(int16_t adcOutput) |
|
|
|
|
{ |
|
|
|
|
for(int8_t i = 1; i < calibrationNumValues; ++i) |
|
|
|
|
{ |
|
|
|
|
if(i == calibrationNumValues - 1 || calibration[i].ADC_Value >= adcOutput) |
|
|
|
|
{ |
|
|
|
|
const auto& p = calibration[i - 1]; |
|
|
|
|
const auto& n = calibration[i]; |
|
|
|
|
return (float)(adcOutput - p.ADC_Value) / (float)(n.ADC_Value - p.ADC_Value) * (n.Measure - p.Measure) + p.Measure; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return -1.0f; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void setup() |
|
|
|
|
{ |
|
|
|
|
Serial.begin(115200); |
|
|
|
@ -50,13 +86,22 @@ void setup()
|
|
|
|
|
Serial.println("HTTP server started"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
float avgAnalogV = 0.0f; |
|
|
|
|
|
|
|
|
|
void loop() |
|
|
|
|
{ |
|
|
|
|
const int potPin = 34; |
|
|
|
|
|
|
|
|
|
delay(1000); |
|
|
|
|
const float minV = 0.14f; // 1
|
|
|
|
|
const float maxV = 3.16f; // 4094
|
|
|
|
|
|
|
|
|
|
delay(10); |
|
|
|
|
int16_t analogV = analogRead(potPin); |
|
|
|
|
float v = (float)analogV / 4096.0f * 3.3f; |
|
|
|
|
batteryVoltage = (int16_t)(v * 1000.0f + 0.5f); |
|
|
|
|
//Serial.println(potValue);
|
|
|
|
|
float v = getCalibratedVoltage(analogV); |
|
|
|
|
|
|
|
|
|
averageBatteryVoltage = averageBatteryVoltage * 0.95f + v * 0.05f; |
|
|
|
|
batteryVoltage = (int16_t)(averageBatteryVoltage * 1000.0f + 0.5f); |
|
|
|
|
|
|
|
|
|
//avgAnalogV = avgAnalogV * 0.9f + (float)analogV * 0.1f;
|
|
|
|
|
//batteryVoltage = (int16_t)(avgAnalogV + 0.5f);
|
|
|
|
|
} |
|
|
|
|