Skip to content

Commit 078bdad

Browse files
authored
Added setAnalogReference() to NodeManager
1 parent 3cadf71 commit 078bdad

File tree

7 files changed

+37
-21
lines changed

7 files changed

+37
-21
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ You can interact with each class provided by NodeManager through a set of API fu
277277
Sensor* getSensorWithChild(uint8_t child_id);
278278
// sleep between send()
279279
void sleepBetweenSend();
280+
// set the analog reference to the given value and optionally perform some fake reading on the given pin
281+
void setAnalogReference(uint8_t value, uint8_t pin = -1);
280282
#if NODEMANAGER_SLEEP == ON
281283
// [3] set the duration (in seconds) of a sleep cycle
282284
void setSleepSeconds(unsigned long value);

nodemanager/Node.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,23 @@ void NodeManager::sleepBetweenSend() {
721721
// if we sleep here ack management will not work
722722
if (_sleep_between_send > 0) wait(_sleep_between_send);
723723
}
724+
725+
// set the analog reference
726+
void NodeManager::setAnalogReference(uint8_t value, uint8_t pin) {
727+
#ifdef CHIP_AVR
728+
// nothing to do
729+
if (value == _analog_reference) return;
730+
// change the analog reference
731+
analogReference(value);
732+
// wait a bit
733+
wait(200);
734+
// perform some reading before actually reading the value
735+
if (pin > -1) {
736+
for (int i = 0; i < 5; i++) {
737+
analogRead(pin);
738+
wait(50);
739+
}
740+
}
741+
#endif
742+
}
743+

nodemanager/Node.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class NodeManager {
8383
Sensor* getSensorWithChild(uint8_t child_id);
8484
// sleep between send()
8585
void sleepBetweenSend();
86+
// set the analog reference to the given value and optionally perform some fake reading on the given pin
87+
void setAnalogReference(uint8_t value, uint8_t pin = -1);
8688
#if NODEMANAGER_SLEEP == ON
8789
// [3] set the duration (in seconds) of a sleep cycle
8890
void setSleepSeconds(unsigned long value);
@@ -179,6 +181,7 @@ class NodeManager {
179181
uint8_t _reboot_pin = -1;
180182
void _present(uint8_t child_id, uint8_t type);
181183
List<Timer*> _timers;
184+
uint8_t _analog_reference = DEFAULT;
182185
#if NODEMANAGER_INTERRUPTS == ON
183186
uint8_t _interrupt_1_mode = MODE_NOT_DEFINED;
184187
uint8_t _interrupt_2_mode = MODE_NOT_DEFINED;

sensors/SensorAnalogInput.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ class SensorAnalogInput: public Sensor {
111111
int _getAnalogRead() {
112112
#ifdef CHIP_AVR
113113
// set the reference
114-
if (_reference != -1) {
115-
analogReference(_reference);
116-
wait(100);
117-
}
114+
if (_reference != -1) nodeManager.setAnalogReference(_reference);
118115
#endif
119116
// read and return the value
120117
int value = analogRead(_pin);

sensors/SensorBattery.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,21 @@ class SensorBattery: public Sensor {
7474
_battery_adj_factor = value;
7575
};
7676

77-
// define what to do during setup
78-
void onSetup() {
79-
#ifdef CHIP_AVR
80-
// when measuring the battery from a pin, analog reference must be internal
81-
if (! _battery_internal_vcc && _battery_pin > -1)
82-
#ifdef CHIP_MEGA
83-
analogReference(INTERNAL1V1);
84-
#else
85-
analogReference(INTERNAL);
86-
#endif
87-
#endif
88-
};
89-
9077
// define what to do during loop
9178
void onLoop(Child* child) {
92-
// measure the board vcc
79+
// measure the battery
9380
float volt = 0;
9481
if (_battery_internal_vcc || _battery_pin == -1) volt = (float)hwCPUVoltage()/1000;
95-
else volt = analogRead(_battery_pin) * _battery_volts_per_bit;
82+
else {
83+
// when measuring the battery from a pin, analog reference must be internal
84+
#ifdef CHIP_AVR
85+
nodeManager.setAnalogReference(INTERNAL);
86+
#endif
87+
#ifdef CHIP_MEGA
88+
nodeManager.setAnalogReference(INTERNAL1V1);
89+
#endif
90+
volt = analogRead(_battery_pin) * _battery_volts_per_bit;
91+
}
9692
volt = volt * _battery_adj_factor;
9793
child->setValue(volt);
9894
// calculate the percentage

sensors/SensorRain.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class SensorRain: public SensorAnalogInput {
3232
children.get()->setPresentation(S_RAIN);
3333
children.get()->setType(V_RAINRATE);
3434
children.get()->setDescription(_name);
35-
setReference(DEFAULT);
3635
setOutputPercentage(true);
3736
setReverse(true);
3837
setRangeMin(100);

sensors/SensorSoilMoisture.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class SensorSoilMoisture: public SensorAnalogInput {
3535
setReverse(true);
3636
setRangeMin(100);
3737
setOutputPercentage(true);
38-
setReference(DEFAULT);
3938
};
4039

4140
};

0 commit comments

Comments
 (0)