move turn on/off logic inside sensor

This commit is contained in:
Nicu Hodos 2022-01-01 16:45:57 +01:00
parent 1beeb485c9
commit bb638a8d70
3 changed files with 17 additions and 19 deletions

View File

@ -42,4 +42,15 @@ public:
delay(100);
sendHumidity(values.humidity);
}
void turnOnSensor() override {
pinMode(DHT_PIN, INPUT_PULLUP);
digitalWrite(TEMP_POSITIVE, HIGH);
}
void turnOffSensor() override {
digitalWrite(TEMP_POSITIVE, LOW);
pinMode(DHT_PIN, OUTPUT);
digitalWrite(DHT_PIN, LOW);
}
};

View File

@ -11,4 +11,6 @@ public:
virtual void setup() = 0;
virtual bool readTemp() = 0;
virtual void sendValues(bool voltage = false) = 0;
virtual void turnOnSensor() {}
virtual void turnOffSensor() {}
};

View File

@ -8,11 +8,7 @@
// Pins
#define SENDER PIN_B2
DhtValues readTemp();
void turnOnDht();
void turnOffDht();
TempSensor &tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
TempSensor& tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
volatile int counter = 0;
@ -38,10 +34,10 @@ void loop() {
tempSensor.sendValues();
}
turnOffDht();
tempSensor.turnOffSensor();
}
if (retry && !success) {
turnOffDht();
tempSensor.turnOffSensor();
}
retry = !retry && !success;
}
@ -51,17 +47,6 @@ void loop() {
ISR(WDT_vect) {
counter++;
if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) {
turnOnDht();
tempSensor.turnOnSensor();
}
}
void turnOnDht() {
pinMode(DHT_PIN, INPUT_PULLUP);
digitalWrite(TEMP_POSITIVE, HIGH);
}
void turnOffDht() {
digitalWrite(TEMP_POSITIVE, LOW);
pinMode(DHT_PIN, OUTPUT);
digitalWrite(DHT_PIN, LOW);
}