Extract moisture reading logic into separate function
This commit is contained in:
+18
-14
@@ -1,11 +1,12 @@
|
|||||||
|
#include <stdint.h>
|
||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
#define MOISTURE_SENSOR_A0 A0
|
#define MOISTURE_SENSOR_A0 A0
|
||||||
|
|
||||||
volatile bool readSensors = false;
|
volatile bool readSensors = false;
|
||||||
uint32_t moistureReadingSum = 0;
|
int16_t moistureReadingSum = 0;
|
||||||
uint8_t moistureReadingCount = 0;
|
int8_t moistureReadingCount = 0;
|
||||||
const uint8_t SENSOR_READINGS = 3;
|
const uint8_t SENSOR_READINGS = 3;
|
||||||
|
|
||||||
ISR(WDT_vect) {
|
ISR(WDT_vect) {
|
||||||
@@ -20,6 +21,20 @@ void watchdogs() {
|
|||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int16_t readMoisture() {
|
||||||
|
if (readSensors) {
|
||||||
|
moistureReadingSum += analogRead(MOISTURE_SENSOR_A0);
|
||||||
|
moistureReadingCount++;
|
||||||
|
|
||||||
|
if (moistureReadingCount >= SENSOR_READINGS) {
|
||||||
|
Serial.println(moistureReadingSum / SENSOR_READINGS);
|
||||||
|
moistureReadingSum = 0;
|
||||||
|
moistureReadingCount = 0;
|
||||||
|
} else readSensors = true;
|
||||||
|
}
|
||||||
|
readSensors = false;
|
||||||
|
}
|
||||||
|
|
||||||
void enterSleep() {
|
void enterSleep() {
|
||||||
// Arduino disables USB when sleeping
|
// Arduino disables USB when sleeping
|
||||||
// if using Serial, use flag : SLEEP_MODE_IDLE
|
// if using Serial, use flag : SLEEP_MODE_IDLE
|
||||||
@@ -38,17 +53,6 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (readSensors) {
|
int16_t moisture = readMoisture();
|
||||||
moistureReadingSum += analogRead(MOISTURE_SENSOR_A0);
|
|
||||||
moistureReadingCount++;
|
|
||||||
|
|
||||||
if (moistureReadingCount >= SENSOR_READINGS) {
|
|
||||||
Serial.println(moistureReadingSum / SENSOR_READINGS);
|
|
||||||
moistureReadingSum = 0;
|
|
||||||
moistureReadingCount = 0;
|
|
||||||
} else readSensors = true;
|
|
||||||
}
|
|
||||||
readSensors = false;
|
|
||||||
}
|
|
||||||
enterSleep();
|
enterSleep();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user