// include the LiquidCrystal.h library
#include <LiquidCrystal.h>
//#include <LcdKeypad.h>
#include <Bounce2.h> // Eingäne entprellen
/*
* initialize the library with the interface pins values
* First create the variables and assign the values
* because LiquidCrystal lcd(7, 6, 12, 10, 9, 8);
* will not tell you what wire goes where.
*
* TIP: Do the GND and POWER wires after you have made
* all other connections and you have checked your
* schematic several times.
*
* Connect the LCD wires to the pins on the Arduino
*
* Pin LCD Descriptions:
* Pin 1 --- Ground
* Pin 2 --- VDD Power for the LCD
* Pin 3 --- Contrast Adjust
* Pin 4 --- Register Select (RS)
* Pin 5 --- Read / Write select (R/W)
* Pin 6 --- Enable (E)
* Pin 7 --- not used
* Pin 8 --- not used
* Pin 9 --- not used
* Pin 10 -- not used
* Pin 11 -- Data line (D4) 4-bits at a time
* Pin 12 -- Data line (D5) 4-bits at a time
* Pin 13 -- Data line (D6) 4-bits at a time
* Pin 14 -- Data line (D7) 4-bits at a time
* Pin 15 -- Backlight Power
* Pin 16 -- Backlight Ground (GND) remember 220 ohms resistor
*
*/
// Double slash for adding single line comments
const int rs = 7; // Pin 7 on Arduino to pin 4 (RS) on LCD
const int en = 6; // Pin 6 on Arduino to pin 6 (E) on LCD
const int d4 = 12; // Pin 12 on Arduino to pin 11 (D4) on LCD
const int d5 = 10; // Pin 10 on Arduino to pin 12 (D5) on LCD
const int d6 = 9; // Pin 9 on Arduino to pin 13 (D6) on LCD
const int d7 = 8; // Pin 8 on Arduino to pin 14 (D7) on LCD
/*
10k potentiometer
Connect swipe pin of the potentiometer to the VO pin on the 16 x 2 LCD
Lastly the Ground (Black) and power wires (Red)
from the Arduino to the following pins on the LCD display
GND = VSS, RW, and (K with 220 ohms)
POWER = VDD, A
*/
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// ###################################################################
#define ADC_VREF_mV 5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35 A0
// TEST
const int RELAY_PIN = 2; // Pin, an dem das Relais angeschlossen ist
const int TEMP_SENSOR_PIN = A0; // Pin, an dem der Temperatursensor angeschlossen ist
const float SET_POINT = 75.0; // Sollwert in Grad Celsius
const float SET_POINT_HYST = 3.5; // Sollwert Hysterese in Grad Celsius
const unsigned long IMPULSE_TIME = 5000; // Impulszeit in Millisekunden (5 Sekunden)
const unsigned long PAUSE_TIME = 10000; // Pausenzeit in Millisekunden (10 Sekunden)
const unsigned long Intervall_Show_TIME = 1000; // Anzeigezeit in Millisekunden (1 Sekunden)
unsigned long previousMillis = 0;
unsigned long previousMillisAnzeige = 0;
bool isHeating = false;
float readTemperature() {
int sensorValue = analogRead(TEMP_SENSOR_PIN);
float temp = sensorValue /(307.0-4.0)*150.0; // * (5.0 / 1023.0);
return temp;
}
// Test
#define KEYPAD_NONE -1
#define KEYPAD_SELECT 4
int PressedKey = KEYPAD_NONE;
bool KeyTriggered = false;
//Bounce2::Button iSaunaTuerSwitch = Bounce2::Button();
Bounce iSaunaTuerSwitch = Bounce();
enum iSaunaDoor {cNULL, cOPEN, cCLOSE};
int iSaunaDoor = cNULL;
// Sauna Step
enum iSaunaStep {sNULL, sOPEN, sCLOSE, sREMOTE, sHEAT};
int iSaunaStep = sNULL;
// Sauna Timer
long SaunaTimer = 120; // timer of 120 minutes
long lastSaunaTimer = 0;
// Lese Status Tür
void LCDReadKey() {
if (analogRead(A1) < 700 && !KeyTriggered) {
PressedKey = KEYPAD_SELECT;
KeyTriggered = true;
Serial.println("Key Select");
}
if (analogRead(A1) > 900 && KeyTriggered) {
PressedKey = KEYPAD_NONE;
KeyTriggered = false;
Serial.println("Key NONE");
}
}
// Status Sauna Door
void readSaunaDoor() {
//Update Inputs
iSaunaTuerSwitch.update();
if (iSaunaTuerSwitch.read() == HIGH) {
iSaunaDoor = cOPEN;
//Serial.println("Door Open");
}
if (iSaunaTuerSwitch.read() == LOW && iSaunaDoor == cOPEN) {
iSaunaDoor = cCLOSE;
//Serial.println("Door Closed");
}
}
void setup() {
pinMode(A0, INPUT);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print Hello World! to the LCD.
lcd.print("Hello World!");
// Test
// BUTTON SETUP old
pinMode(3, INPUT); // Eigang Sauna-Tür
// Entprellt SELECT ONE OF THE FOLLOWING :
iSaunaTuerSwitch.attach( 3, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
iSaunaTuerSwitch.interval(20);
}
void loop() {
unsigned long currentMillis = millis();
float temperature = readTemperature();
if (iSaunaStep >= sHEAT && temperature+SET_POINT_HYST < SET_POINT && !isHeating && currentMillis - previousMillis >= PAUSE_TIME) {
digitalWrite(RELAY_PIN, HIGH);
previousMillis = currentMillis;
isHeating = true;
}
if ((iSaunaStep < sHEAT && isHeating)|| (temperature >= SET_POINT && isHeating && currentMillis - previousMillis >= IMPULSE_TIME)) {
digitalWrite(RELAY_PIN, LOW);
previousMillis = currentMillis;
isHeating = false;
} // else if (!isHeating && currentMillis - previousMillis >= PAUSE_TIME) {
// Pausenzeit abgelaufen
//}
// Door
readSaunaDoor();
// Key action
LCDReadKey();
//Serial.print("KEY "); Serial.print(analogRead(A1)); Serial.print(" : "); Serial.println(PressedKey);
switch (iSaunaStep)
{
case sOPEN:{
lcd.setCursor(0,1);
//lcd.print("sOPEN ");
if (iSaunaDoor == cCLOSE) {iSaunaStep=sCLOSE;}
break;
}
case sCLOSE:{
lcd.setCursor(0,1);
//lcd.print("sCLOSE ");
if (PressedKey == KEYPAD_SELECT) {iSaunaStep=sREMOTE;}
PressedKey = KEYPAD_NONE;
if (iSaunaDoor == cOPEN) {iSaunaStep=sOPEN;}
break;
}
case sREMOTE:{
lcd.setCursor(0,1);
//lcd.print("sREMOTE ");
if (PressedKey == KEYPAD_SELECT) {iSaunaStep=sHEAT;}
PressedKey = KEYPAD_NONE;
if (iSaunaDoor < cCLOSE) {iSaunaStep=sOPEN;}
break;
}
case sHEAT:{
lcd.setCursor(0,1);
//lcd.print("sHEAT ");
if (PressedKey == KEYPAD_SELECT) {iSaunaStep=sREMOTE;}
PressedKey = KEYPAD_NONE;
if (iSaunaDoor < cCLOSE) {Serial.print("Offen ");}
break;
}
case sNULL:{
// alles aus
lcd.setCursor(0,1);
//lcd.print("sNULL ");
if (iSaunaDoor == cOPEN) {iSaunaStep=sOPEN;}
break;
}
default:
Serial.println("Default");
iSaunaStep = cNULL;
break;
}
delay(100);
if (iSaunaStep != sHEAT) {
lastSaunaTimer = currentMillis;
}
lcd.setCursor(0,0);
int RestTime = (SaunaTimer - ((currentMillis - lastSaunaTimer) / 1000 / 60)) ;
if (currentMillis - previousMillisAnzeige >= Intervall_Show_TIME) {
// print the temperature in the Serial Monitor:
previousMillisAnzeige = currentMillis;
Serial.print("stDoor:"); Serial.print(iSaunaDoor);
Serial.print(" stStep:"); Serial.print(iSaunaStep);
Serial.print(" SaunaTimer:"); Serial.print(SaunaTimer); Serial.print(" TimerRest:"); Serial.println(RestTime);
lcd.setCursor(0,0);
lcd.print("AktTemp: C");
lcd.setCursor(8,0);
lcd.print(temperature);
lcd.setCursor(0,1);
lcd.print("Millis : ms");
lcd.setCursor(8,1);
lcd.print(currentMillis - previousMillis);
}
}