#include <PID_v1.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTTYPE DHT22
#define DHTPIN 29 // Digital pin connected to the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//KEYPAD
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {35, 37, 39, 41}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {43, 45, 47, 49}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
String inNUM;
//long inSetpoint;
#define ssr 3
#define FANHEAT 6
//#define FANEX 7
//Variables
float temperature_read = 0.0;
float set_temperature = 0;
float PID_error = 0;
float previous_error = 0;
float elapsedTime, Time, timePrev;
int PID_value = 0;
//PID constants
int kp = 90; int ki = 0.3; int kd = 1.8;
int PID_p = 0; int PID_i = 0; int PID_d = 0;
void setup()
{
Serial.begin(9600);
dht.begin();
inNUM.reserve(10); // maximum number of digit for a number is 10, change if needed
//SetLCD
lcd.init();
lcd.backlight();
}
void loop(){
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float TempC = dht.readTemperature();
//Serial.print(F("Humidity: "));
//Serial.print(h);
lcd.setCursor(0, 3);
lcd.print("HUMI ");
lcd.print(h);
lcd.setCursor(10, 3);
lcd.print("TEMP ");
lcd.print(TempC);
//Serial.print(F("% Temperature: "));
//Serial.print(t);
lcd.setCursor(0, 1);
lcd.print("Setpoint ");
lcd.print(set_temperature);
//Inputset Section
char key = keypad.getKey();
if (key) {
//Serial.println(key);
if (key >= '0' && key <= '9') { // only act on numeric keys
inNUM += key; // append new character to input string
} else if (key == '#') {
if (inNUM.length() > 0) {
set_temperature = inNUM.toInt(); // YOU GOT AN INTEGER NUMBER
inNUM = ""; // clear input
// DO YOUR WORK HERE
}
} else if (key == '*') {
inNUM = ""; // clear input
}
lcd.clear();
}
lcd.setCursor(0, 0);
lcd.print("Enter Set ");
lcd.print(inNUM);
// First we read the real value of temperature
temperature_read = TempC;
//Next we calculate the error between the setpoint and the real value
PID_error = set_temperature - TempC;
//Calculate the P value
PID_p = kp * PID_error;
//Calculate the I value in a range on +-3
if(-3 < PID_error <3)
{
PID_i = PID_i + (ki * PID_error);
}
//For derivative we need real time to calculate speed change rate
timePrev = Time; // the previous time is stored before the actual time read
Time = millis(); // actual time read
elapsedTime = (Time - timePrev) / 1000;
//Now we can calculate the D calue
PID_d = kd*((PID_error - previous_error)/elapsedTime);
//Final total PID value is the sum of P + I + D
PID_value = PID_p + PID_i + PID_d;
//We define PWM range between 0 and 255
if(PID_value < 0)
{ PID_value = 255; }
if(PID_value > 255)
{ PID_value = 0; }
//if((set_temperature <= 30) || (set_temperature >= 61) )
//{ analogWrite(ssr,0); }
//if((set_temperature >= 31) || (set_temperature <= 60) )
//{ analogWrite(ssr,255-PID_value); }
//Now we can write the PWM signal to the mosfet on digital pin D3
analogWrite(ssr,255-PID_value);
previous_error = PID_error; //Remember to store the previous error for next loop.
Serial.print(ssr);
Serial.print(" ");
Serial.println(PID_value);
Serial.print(" ");
Serial.println(set_temperature);
//delay(200);
}