#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define UP 2
#define DOWN 3
#define START 4
#define RELAY 5
#define LDR A1
#define NTC A0
LiquidCrystal_I2C lcd(0x27, 16,2);
int time=0;
int ttime=0;
int btime=0;
int tset=0;
int tchange=0;
int tcur=0;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
bool tup=0;
bool Cup=0;
bool tdown=0;
bool Cdown=0;
bool start=0;
bool sv=false;
void setup() {
Serial.begin(9600);
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(START, INPUT);
pinMode(13, OUTPUT);
pinMode(RELAY, OUTPUT);
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("MADE IN");
lcd.setCursor(3,1);
lcd.print("TEXNOLAB");
delay(2000);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.print("UP- ");
//Serial.print(digitalRead(UP));
//Serial.print(" DOWN- ");
//Serial.print(digitalRead(DOWN));
//Serial.print(" START- ");
//Serial.println(digitalRead(START));
Cup=digitalRead(UP);
Cdown=digitalRead(DOWN);
if(!Cup && Cdown)
{
if(Cup!=tup)
{
tchange++;
btime=millis();
}
else if(Cup==tup && millis()-btime>2000 && (millis()-btime)%200<100) tchange++;
}
tup=Cup;
if(!Cdown && Cup)
{
if(Cdown!=tdown)
{
tchange--;
btime=millis();
}
else if(Cdown==tdown && millis()-btime>2000 && (millis()-btime)%200<100) tchange--;
}
tdown=Cdown;
if(!digitalRead(START))
{
tset=tchange;
ttime=millis();
start=true;
}
if(tset>tcur )
{
digitalWrite(13, HIGH);
digitalWrite(RELAY, HIGH);
sv=true;
}
else
{
digitalWrite(13, LOW);
digitalWrite(RELAY, LOW);
sv=false;
}
//Serial.print("Tcur- ");
//Serial.print(tcur);
//Serial.print(" Tset- ");
//Serial.print(tset);
//Serial.print(" Tchange- ");
//Serial.print(tchange);
int tAnalog=analogRead(NTC);
tcur = 1 / (log(1 / (1023. / tAnalog - 1)) / BETA + 1.0 / 298.15) - 273.15;
if(start) time=(millis()-ttime)/1000;
//Serial.print(" Time- ");
//Serial.print(time);
//Serial.print(" Time- ");
//Serial.print(millis());
//Serial.print(" LDR- ");
//Serial.println(analogRead(LDR));
lcd.setCursor(0,0);
lcd.print("TEM IDT SV TM I");
lcd.setCursor(0,1);
if(0<=tcur && tcur<10) lcd.print(' ');
lcd.print(tcur);
lcd.print(char(223));
if(tcur>-10) lcd.print(' ');
lcd.setCursor(4,1);
if(tset!=tchange)
{
if(millis()%300<50) lcd.print(" ");
else
{
if(0<=tchange && tchange<10) lcd.print(' ');
lcd.print(tchange);
lcd.print(char(223));
if(tchange>-10) lcd.print(' ');
}
}
else
{
if(0<=tset && tset<10) lcd.print(' ');
lcd.print(tset);
lcd.print(char(223));
if(tset>-10) lcd.print(' ');
}
lcd.setCursor(8,1);
if(sv) lcd.print("AK");
else lcd.print("PS");
lcd.setCursor(11,1);
lcd.print(time);
if(time<10) lcd.print(" ");
else if(time<100) lcd.print(" ");
lcd.setCursor(15,1);
if(analogRead(LDR)<300) lcd.print('G');
else lcd.print('Q');
}