/*
Wokwi | questions
Multiple definition that doesn't exist
TEOVR — 10/29/24 at 7:00 AM
Project Link: https://wokwi.com/projects/412783365768497153?gh=1
NOTE:
In an actual circuit you must use resistors on the segments
and transistor drivers on the digits (grey wires).
*/
#include <DHT.h>
#include <SevSeg.h>
#include "SevSegConstants.h"
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
const int RELAY_PIN = A0;
const int DHT_PIN = A1;
const int SET_BTN_PIN = A2;
const int SLIDER_PIN = A3;
const unsigned long SENSOR_DELAY = 2000;
int targetHum = 0;
int actualHum = 0;
unsigned long prevTime = 0;
DHT dht(DHT_PIN, DHTTYPE);
SevSeg sevseg;
int getSetHumidity() {
int target = 0;
Serial.println("Set target humidity, press button to store\n");
while (digitalRead(SET_BTN_PIN) == true) {
target = map(analogRead(SLIDER_PIN), 0, 1023, 0, 100);
//Serial.println(target);
sevseg.setNumber(target);
sevseg.refreshDisplay();
}
return target;
}
int getHumidity() {
float h = dht.readHumidity();
if (!isnan(h)) { // if valid reading
Serial.print("Humidity: ");
Serial.print((int)h);
Serial.println("%");
return (int)h;
}
Serial.println("Sensor error");
return 9999;
}
void setup() {
Serial.begin(115200);
dht.begin();
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
pinMode(SET_BTN_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
int target = getSetHumidity();
Serial.print("Target humidity: ");
Serial.print(target);
Serial.println("%");
targetHum = target;
}
void loop() {
if (millis() - prevTime >= SENSOR_DELAY) {
prevTime = millis();
actualHum = getHumidity();
}
sevseg.setNumber(actualHum);
if (actualHum < targetHum) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
sevseg.refreshDisplay(); // Must run repeatedly
}