#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 20,4);
#define T_OFF_STATE 0
#define T_ON_STATE 1
int state = T_OFF_STATE; // set initial state
int Relay = 4; // set output solinoid
int t_on_show,t_off_show;
long tt_on,tt_off;
long Time_on , Time_off;
unsigned long previousTime_Scan = millis();
long timeInterval_Scan = 1000;
unsigned long timer1 = millis();
unsigned long currentTime = millis();
void setup() {
Serial.begin(9600); // set broad rate
lcd.begin(20,4); // set lcd
lcd.backlight(); // on blacklight
pinMode(Relay,OUTPUT); // set pin output
lcd.setCursor(0,3);
lcd.print("Loop Time ");
}
void loop() {
unsigned long currentTime = millis();
//----------------------- task 1 -----------------------
if(currentTime - previousTime_Scan > timeInterval_Scan) { // เมื่อถึงเวลา 1 วินาที จะทำการอ่านค่าใหม่ตลอด
previousTime_Scan = currentTime;
tt_on = analogRead(A0);
tt_on = map(tt_on,0,1023,0,60);
Time_on = tt_on * 60000;
t_on_show = map(Time_on,0,360000,0,60)/10;
Serial.println(Time_on);
tt_off = analogRead(A1);
tt_off = map(tt_off,0,1023,0,60);
Time_off = tt_off * 60000;
t_off_show = map(Time_off,0,360000,0,60)/10;
Serial.println(Time_off);
}
switch (state){
case T_OFF_STATE:
Serial.println("T_OFF");
digitalWrite(Relay,LOW);
lcd.setCursor(14,2);
lcd.print(" OFF ");
if (millis() - timer1 >= Time_off){
timer1 = millis();
state = T_ON_STATE;
}
break;
case T_ON_STATE:
Serial.println("T_ON");
digitalWrite(Relay,HIGH);
lcd.setCursor(14,2);
lcd.print(" ON ");
if (millis() - timer1 >= Time_on){
timer1 = millis();
state = T_OFF_STATE;
}
break;
}
show();
}
void show()
{
lcd.setCursor(0,0);
lcd.print("Time On = ");
lcd.print(t_on_show);
lcd.print(" Min ");
lcd.setCursor(0,1);
lcd.print("Time Off = ");
lcd.print(t_off_show);
lcd.println(" Min ");
lcd.setCursor(0,2);
lcd.print("Status : Water");
lcd.setCursor(0,3);
lcd.print("Loop Time ");
lcd.print(t_on_show + t_off_show);
lcd.print(" Min ");
}