Arduino/nudelmaschine/main/main.ino

142 lines
3.1 KiB
C++

#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
//temp sensor
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0x3F,20,4); //https://github.com/marcoschwartz/LiquidCrystal_I2C
OneWire oneWire(4); //OneWire Library (with Tim Stud)
DallasTemperature tsensor(&oneWire); //DallasTempratureSensor Library
SoftwareSerial wlan(6, 5); //RX, TX
long lastLCDupdate = millis();
int temp = 0;
byte cancelcause = 0; //why is the heater off? -> 0= None, 1 = canceled, 2= temp, 3=to long on
long heating = -1;//millis() value when heater started
char* status = "--------------------";
void setup() {
pinMode(2, OUTPUT);
pinMode(3, INPUT_PULLUP);
digitalWrite(2, LOW);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(6,0);
lcd.print("Autokocher");
lcd.setCursor(3,1);
lcd.print("Yannis Gerlach");
delay(1000);
tsensor.begin();
wlan.begin(9600);
updateLCD();
}
int getTemp() {
tsensor.requestTemperatures();
return (tsensor.getTempCByIndex(0)*100);
}
void updateLCD() {
lcd.clear();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Herd:");
lcd.setCursor(15,0);
lcd.print(heating > 0 ? "An" : "Aus");
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.setCursor(15,1);
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0,2);
if(cancelcause != 0) {
lcd.print("Abbruch Grund:");
lcd.setCursor(15,2);
switch (cancelcause) {
case 1: lcd.print("Abbr."); break;
case 2: lcd.print("Temp"); break;
case 3: lcd.print("Zeit"); break;
default: lcd.print("undef.");
}
} else if(heating > 0) {
lcd.print("An seit:");
lcd.setCursor(15,2);
lcd.print((millis()-heating)/1000);
lcd.print("s");
}
lcd.setCursor(0,3);
lcd.print(status);
lastLCDupdate = millis();
}
void handleWLAN() {
if(wlan.available()) {
char first = wlan.read();
if(first == 's') {
//status update
bool ava = true;
for(unsigned char i = 0; i < 20; i++) {
status[i] = ' ';
if(wlan.available() && ava) {
status[i] = wlan.read();
if(status[i] == '\0') {
ava = false;
status[i] = ' ';
}
}
}
}
}
}
bool presed = false;
void loop() {
delay(25);
handleWLAN();
temp = getTemp()/100;
if((millis() - lastLCDupdate) > 500) {//reprint LCD every 2 seconds
updateLCD();
}
if(digitalRead(3) == LOW) {
if(!presed) {
presed = true;
//toggle
if(heating > 0) {
cancelcause = 1;
}
setheater(heating < 0);
updateLCD();
}
} else {
presed = false;
}
if(heating > 0 && temp > 97) {//stop
setheater(false);
cancelcause = 2;
}
if(heating > 0 && millis() - heating > 600000) {//longer on tha 10min
cancelcause = 3;
setheater(false);
}
}
void setheater(bool on) {
heating = on ? millis() : -1;
if(on)
cancelcause = 0;
digitalWrite(2, on);
digitalWrite(13, on);
}