Skip to content

Commit

Permalink
Merge pull request #49 from rlogiacco/feature/esp32
Browse files Browse the repository at this point in the history
Configurable ADC resolution
  • Loading branch information
rlogiacco authored Jan 25, 2024
2 parents 1474173 + 5f3cbc6 commit 58f7b0f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 34 deletions.
17 changes: 7 additions & 10 deletions Battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "Battery.h"
#include <Arduino.h>

Battery::Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin) {
Battery::Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin, uint8_t adcBits) : adc(0x01 << adcBits) {
this->sensePin = sensePin;
this->activationPin = 0xFF;
this->minVoltage = minVoltage;
Expand All @@ -30,15 +30,12 @@ void Battery::begin(uint16_t refVoltage, float dividerRatio, mapFn_t mapFunction
this->refVoltage = refVoltage;
this->dividerRatio = dividerRatio;
pinMode(this->sensePin, INPUT);
if (this->activationPin < 0xFF) {
pinMode(this->activationPin, OUTPUT);
}
this->mapFunction = mapFunction ? mapFunction : &linear;
}

void Battery::onDemand(uint8_t activationPin, uint8_t activationMode) {
if (activationPin < 0xFF) {
this->activationPin = activationPin;
this->activationPin = activationPin;
if (activationPin < 0xFF) {
this->activationMode = activationMode;
pinMode(this->activationPin, OUTPUT);
digitalWrite(activationPin, !activationMode);
Expand All @@ -59,16 +56,16 @@ uint8_t Battery::level(uint16_t voltage) {
}
}

uint16_t Battery::voltage() {
uint16_t Battery::voltage(uint8_t msDelay) {
if (activationPin != 0xFF) {
digitalWrite(activationPin, activationMode);
delayMicroseconds(10); // copes with slow switching activation circuits
}
analogRead(sensePin);
delay(2); // allow the ADC to stabilize
uint16_t reading = analogRead(sensePin) * dividerRatio * refVoltage / 1024;
delay(msDelay); // allow the ADC to stabilize
uint16_t reading = (analogRead(sensePin) * dividerRatio * refVoltage) / adc;
if (activationPin != 0xFF) {
digitalWrite(activationPin, !activationMode);
}
return reading;
}
}
8 changes: 6 additions & 2 deletions Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class Battery {
* @param minVoltage is the voltage, expressed in millivolts, corresponding to an empty battery
* @param maxVoltage is the voltage, expressed in millivolts, corresponding to a full battery
* @param sensePin is the analog pin used for sensing the battery voltage
* @param adcBits is the number of bits the ADC uses (defaults to 10)
*/
Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin);
Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin, uint8_t adcBits = 10);

/**
* Initializes the library by optionally setting additional parameters.
Expand Down Expand Up @@ -69,8 +70,9 @@ class Battery {

/**
* Returns the current battery voltage in millivolts.
* @param delay is the amount of milliseconds to wait to allow the ADC input voltage to stabilize (defaults to 2ms)
*/
uint16_t voltage();
uint16_t voltage(uint8_t delay = 2);

private:
uint16_t refVoltage;
Expand All @@ -81,6 +83,8 @@ class Battery {
uint8_t activationPin;
uint8_t activationMode;
mapFn_t mapFunction;

const uint16_t adc;
};

//
Expand Down
Loading

0 comments on commit 58f7b0f

Please sign in to comment.