Compare commits

...

2 commits

Author SHA1 Message Date
0b8e4d3a5c
Add PID. 2024-04-24 12:32:34 +02:00
f2b82a6abd
Set temprange from 18 to 89 degrees.
There is no need for temperatures under 18 and above 89 degrees. Now it's easier to set the temp.
2024-04-23 17:40:04 +02:00

View file

@ -2,28 +2,67 @@
#include <LiquidCrystal_I2C.h> // Support for PCF8574 --> LCD #include <LiquidCrystal_I2C.h> // Support for PCF8574 --> LCD
#include <OneWire.h> // 1-Wiresupport for DS18B20 #include <OneWire.h> // 1-Wiresupport for DS18B20
#include <DallasTemperature.h> // Easy reading of DS18B20 #include <DallasTemperature.h> // Easy reading of DS18B20
#include <PID_v1.h> // PID-Controller
#define ONE_WIRE_BUS 10 // Enable 1-Wirebus on digital 10 (PB2 - Pin16) #define ONE_WIRE_BUS 10 // Enable 1-Wirebus on digital 10 (PB2 - Pin16)
#define fan 8
#define wecktopf 9
LiquidCrystal_I2C lcd(0x3F,20,4); // declare LCD LiquidCrystal_I2C lcd(0x3F,20,4); // declare LCD
OneWire oneWire(ONE_WIRE_BUS); // declare 1-Wirebus OneWire oneWire(ONE_WIRE_BUS); // declare 1-Wirebus
DallasTemperature sensors(&oneWire); // declare sensors DallasTemperature sensors(&oneWire); // declare sensors
float tempset; // Define variables PID is connected to
float watertemp; double pidset, watertemp, output;
// PID adjustment
double Kp=2, Ki=5, Kd=1;
PID myPID(&watertemp, &output, &pidset, Kp, Ki, Kd, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
// Temp adjustment
int tempset;
int poti; int poti;
// time
int onesecond = 1000;
unsigned long previousMillis;
// °C for LCD
byte customChar[8] = {
0b01110,
0b01010,
0b01010,
0b01110,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup() { void setup() {
// Setup GPIO // Setup GPIO
pinMode(9, OUTPUT); // Output "Wecktopf" pinMode(wecktopf, OUTPUT); // Output "Wecktopf"
pinMode(8, OUTPUT); // Fan pinMode(fan, OUTPUT); // Fan
// Setup sensors // Setup sensors
sensors.begin(); sensors.begin();
// Setup I2C-LCD // Setup I2C-LCD
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
lcd.clear(); lcd.clear();
lcd.createChar(0, customChar);
lcd.setCursor(1, 0); lcd.setCursor(1, 0);
// Setup PID
windowStartTime = millis();
myPID.SetOutputLimits(0, WindowSize); //tell the PID to range between 0 and the full window size
myPID.SetMode(AUTOMATIC); //turn the PID on
// Greetings // Greetings
lcd.print("Weckkubator 0.0.1"); lcd.print("Weckkubator 0.0.1");
delay(3000); delay(3000);
@ -32,23 +71,34 @@ void setup() {
} }
void loop() { void loop() {
//
// run pid
pid();
// update LCD
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= onesecond) {
settemp(); settemp();
readtemp(); readtemp();
lcdoutput(); lcdoutput();
delay(100);
if (tempset >= watertemp) {
digitalWrite(9, HIGH);
} }
else {
digitalWrite(9, LOW);
} }
//------------------------------------------
// PID-Controller
void pid() {
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (millis() - windowStartTime > WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if (output < millis() - windowStartTime) digitalWrite(wecktopf, LOW);
else digitalWrite(wecktopf, HIGH);
} }
//------------------------------------------ //------------------------------------------
@ -68,18 +118,32 @@ void readtemp() {
void lcdoutput() { void lcdoutput() {
// Output temperatureset // Output temperatureset
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print("s:"); lcd.print("set: ");
lcd.print(tempset); lcd.print(tempset);
lcd.write((byte)0);
lcd.print("C");
// Output watertemp // Output watertemp
lcd.setCursor(9, 1); lcd.setCursor(0, 2);
lcd.print("t:"); lcd.print("temp: ");
lcd.print(watertemp); lcd.print(watertemp);
lcd.write((byte)0);
lcd.print("C");
lcd.print(" ");
// output PID
lcd.setCursor (0, 3);
lcd.print("output: ");
lcd.print(output);
lcd.print(" ");
} }
//------------------------------------------ //------------------------------------------
// Read Poti // Read Poti
void settemp() { void settemp() {
poti = analogRead(A3); poti = analogRead(A3);
tempset = (poti*0.1); tempset = (18+(poti*0.07)); // range between 18°C and 18+1024*0,07 (~89°C)
pidset = tempset;
} }