#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include "DHT.h"
// 핀 설정
// I2C OLED
// SDA - A4, SCL - A5
#define PIN_BTN 3 // 시작, 중지 버튼
#define PIN_S1 7 // S1 릴레이
#define PIN_S2 8 // S2 릴레이
#define PIN_POT A0 // 가변 저항
#define PIN_FAN 2 // 송풍팬
#define DHTPIN 5 // DHT22 온도센서
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
SSD1306AsciiWire oled;
int prevPot = 0;
int prevKey = HIGH;
int led = HIGH;
float set_temp = 0.0;
bool state_run;
bool state_on;
bool state_heater;
bool safe_control;
#define ON_DELAY 5 * 60000UL // 5분 딜레이
uint32_t prev_off_time;
bool onDelay;
float temp;
void setup() {
Serial.begin(9600);
Serial.println();
pinMode(PIN_BTN, INPUT_PULLUP);
digitalWrite(PIN_S1, HIGH);
pinMode(PIN_S1, OUTPUT);
digitalWrite(PIN_S2, HIGH);
pinMode(PIN_S2, OUTPUT);
pinMode(PIN_FAN, INPUT);
dht.begin();
Wire.begin();
Wire.setClock(400000L);
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.setFont(font5x7);
oled.clear();
oled.setCursor(2, 0);
oled.println(F("Press Button To Start"));
oled.set2X();
oled.setCursor(5, 2);
oled.println("NOW");
oled.setCol(5);
oled.println("SET");
}
void loop() {
check_button();
read_temp();
control();
}
void relay_control()
{
digitalWrite(PIN_S1, LOW);
delay(1000);
digitalWrite(PIN_S1, HIGH);
delay(1000);
digitalWrite(PIN_S2, LOW);
delay(1000);
digitalWrite(PIN_S2, HIGH);
}
void check_button()
{
static uint32_t prevTime = 0;
uint32_t t = millis();
if (t - prevTime >= 100) {
prevTime = t;
int key = digitalRead(PIN_BTN);
if (key != prevKey) {
if (key == LOW) {
state_run = !state_run;
onDelay = false;
state_heater = check_fan();
Serial.print(F("heater "));
Serial.println(state_heater);
oled.clear(0, 127, 0, 1);
oled.set2X();
if (state_run) {
oled.setCursor(35, 0);
oled.print(F("Run..."));
state_on = false;
oled.setCursor(63, 6);
oled.print(state_on ? F("a:on") : F("a:--"));
}
if (!state_run) {
if (state_on) {
oled.setCursor(39, 0);
oled.print(F("Stop..."));
relay_control();
state_on = false;
oled.setCursor(63, 6);
oled.print(F("a:--"));
}
oled.clear(0, 127, 0, 1);
oled.set1X();
oled.setCursor(2, 0);
oled.print(F("Press Button To Start"));
oled.set2X();
}
}
prevKey = key;
}
//if (!state_run) {
int val = analogRead(PIN_POT);
val = map(val, 0, 1023, 200, 360);
val = constrain(val, 200, 350);
if (val != prevPot) {
prevPot = val;
set_temp = (float)val/10;
oled.setCursor(53, 4);
oled.print(set_temp, 1);
oled.print(F("'C"));
}
//}
}
}
void read_temp()
{
static uint32_t prevTime = millis() - 2000;
uint32_t tt = millis();
if (tt - prevTime < 2000) {
return;
}
prevTime = tt;
// float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
temp = t;
// Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t) || isnan(f)) {
if (isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// // Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// // Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
oled.setCursor(41, 2);
if (temp > -10) {
oled.print(F(" "));
}
if (temp >= 0 && temp <= 10) {
oled.print(F(" "));
}
oled.print(temp, 1);
oled.print(F("'C"));
}
void control()
{
bool heater = check_fan();;
if (heater != state_heater) {
state_heater = heater;
oled.setCursor(5, 6);
oled.print(F("h:"));
oled.print(heater ? F("on") : F("--"));
}
if (state_run) {
if (temp >= (set_temp)) {
if (state_on && state_heater) {
state_on = false;
oled.setCursor(63, 6);
oled.print(F("a:--"));
relay_control();
onDelay = true;
prev_off_time = millis();
Serial.println("heater off");
}
} else if (temp < (set_temp -1)) {
if (!state_on && !state_heater) {
if (onDelay) {
if (millis() - prev_off_time < ON_DELAY) {
return;
}
}
state_on = true;
oled.setCursor(63, 6);
oled.print(F("a:on"));
relay_control();
onDelay = false;
Serial.println("heater on");
}
}
}
}
bool check_fan()
{
int count = 0;
int i = 20;
while(i--) {
count += !digitalRead(PIN_FAN);
delayMicroseconds(100);
}
return (count > 10);
}