/*
อัพเดทล่าสุด 8 nov 23
-ฟังก์ชั่นที่อยากทำ-status
-แยกโปรแกรมไฟกระพริบ->Done
-แจ้งเตือนเมื่อยกงานออกก่อนเวลา->wait
-แสดงเวลาของแต่ล่ะช่องในจอเดียว->done 80%
-มีเสียงแจ้งเตือนเมื่อหมดเวลา->Done
*/
//#include <Wire.h> //ของจริง
//#include <LiquidCrystal_I2C.h> //ของจริง
//LiquidCrystal_I2C lcd(0x27, 16, 2); //ของจริง
#include <LiquidCrystal_I2C.h> //ของ wokwi
#define I2C_ADDR 0x27 //ของ wokwi
#define LCD_COLUMNS 16 //ของ wokwi
#define LCD_LINES 2 //ของ wokwi
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES); //ของ wokwi
unsigned long currentMillis1 ;
unsigned long seconds1;
unsigned long minutes1;
unsigned long hours1;
unsigned long currentMillis2 ;
unsigned long seconds2;
unsigned long minutes2;
unsigned long hours2;
unsigned long currentMillis3 ;
unsigned long seconds3;
unsigned long minutes3;
unsigned long hours3;
int led1=5; // Led หลอดที่ 1
int led2=4; // Led หลอดที่ 2
int led3=3; // Led หลอดที่ 3
int led4=2; // Led หลอดที่ 4
int buzzer=6; // buzzer
int btn1=12; // ปุ่มกดที่ 1
int btn2=11; // ปุ่มกดที่ 2
int btn3=10; // ปุ่มกดที่ 3
int btn4=9; // ปุ่มกดที่ 4
int buzzerState = LOW;
int alarmState1 = LOW;
int alarmState2 = LOW;
int alarmState3 = LOW;
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
long previousMillis1 = 0; // will store last time LED was updated
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillisAlm = 0;
long interval = 300;
double ledblink=160; //ตั้งเวลา led กระพริบ
bool btnsta1=0;
bool btnprevsta1=0;
bool ledsta1=0;
bool btnsta2=0;
bool btnprevsta2=0;
bool ledsta2=0;
bool btnsta3=0;
bool btnprevsta3=0;
bool ledsta3=0;
unsigned long settime=60000; //ตั้งเวลา coolling
unsigned long starttime1=0;
unsigned long elapetime1=0;
unsigned long starttime2=0;
unsigned long elapetime2=0;
unsigned long starttime3=0;
unsigned long elapetime3=0;
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
//lcd.begin(); //ของจริง
lcd.init();//ของ wokwi
// Print a message to the LCD.
lcd.setCursor(3,0);
lcd.print("MIE BARLAP");
lcd.setCursor(5,1);
lcd.print("TEAM B");
delay(3000);
lcd.setCursor(0,0);
lcd.print("1>00:00 2>00:00");
lcd.setCursor(0,1);
lcd.print("3>00:00 4>00:00");
delay(200);
}
void loop() {
btnsta1=digitalRead(btn1);
btnsta2=digitalRead(btn2);
btnsta3=digitalRead(btn3);
if(btnsta1==1){
Serial.println("button1 on");
checkled1();
}
else{
digitalWrite(led1, LOW);
alarmState1=0;
btnprevsta1=0;
starttime1=millis();
// lcd.setCursor(2, 0);
//lcd.print("WAIT ");
}
if(btnsta2==1){
checkled2();
}
else{
digitalWrite(led2, LOW);
alarmState2=0;
btnprevsta2=0;
starttime2=millis();
}
if(btnsta3==1){
checkled3();
}
else{
digitalWrite(led3, LOW);
alarmState3=0;
btnprevsta3=0;
starttime3=millis();
}
if(elapetime1<=settime){
tray1time();
}
else{
lcd.setCursor(2, 0);
lcd.print("OVER ");
}
if(elapetime2<=settime){
tray2time();
}
else{
lcd.setCursor(10, 0);
lcd.print("OVER ");
}
if(elapetime3<=settime){
tray3time();
}
else{
lcd.setCursor(2, 1);
lcd.print("OVER ");
}
if(alarmState1==1 || alarmState2==1 ||alarmState3==1){
digitalWrite(buzzer, HIGH);
}
else{
digitalWrite(buzzer, LOW);
}
}
//----------------------------------------Check LED------------------------------------//
void checkled1(){
if(btnprevsta1=0){
starttime1=millis();
//digitalWrite(led1, HIGH);
}
else{
elapetime1=millis()-starttime1;
if(elapetime1>=settime){
ledblink1();
//digitalWrite(buzzer, HIGH);
alarmState1=1;
}
else{
digitalWrite(led1, HIGH);
alarmState1=0;
}
}
}
void checkled2(){
if(btnprevsta2=0){
starttime2=millis();
}
else{
elapetime2=millis()-starttime2;
if(elapetime2>=settime){
ledblink2();
alarmState2=1;
//digitalWrite(buzzer, HIGH);
//Serial.println("Tray_2 Overtime");
}
else{
digitalWrite(led2, HIGH);
alarmState2=0;
}
}
}
void checkled3(){
if(btnprevsta3=0){
starttime3=millis();
}
else{
elapetime3=millis()-starttime3;
if(elapetime3>=settime){
ledblink3();
alarmState3=1;
}
else{
//Serial.println("Tray_3 Cooling start");
digitalWrite(led3, HIGH);
alarmState3=0;
}
}
}
void ledblink1(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 > interval) {
// save the last time you blinked the LED
previousMillis1 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led1, ledState1);
}
}
void ledblink2(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis2 > interval) {
// save the last time you blinked the LED
previousMillis2 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led2, ledState2);
}
}
void ledblink3(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis3 > interval) {
// save the last time you blinked the LED
previousMillis3 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState3 == LOW)
ledState3 = HIGH;
else
ledState3 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led3, ledState3);
}
}
void tray1time(){
currentMillis1 = elapetime1;
seconds1 = currentMillis1 / 1000;
minutes1 = seconds1 / 60;
hours1 = minutes1 / 60;
//unsigned long days = hours / 24;
currentMillis1 %= 1000;
seconds1 %= 60;
minutes1 %= 60;
hours1 %= 24;
lcd.setCursor(2, 0);
if (minutes1 < 10) {
lcd.print("0");
}
lcd.print(minutes1);
lcd.print(":");
if (seconds1 < 10) {
lcd.print("0");
}
lcd.print(seconds1);
}
void tray2time(){
currentMillis2 = elapetime2;
seconds2 = currentMillis2 / 1000;
minutes2 = seconds2 / 60;
hours2 = minutes2 / 60;
//unsigned long days = hours / 24;
currentMillis2 %= 1000;
seconds2 %= 60;
minutes2 %= 60;
hours2 %= 24;
lcd.setCursor(10, 0);
if (minutes2 < 10) {
lcd.print("0");
}
lcd.print(minutes2);
lcd.print(":");
if (seconds2 < 10) {
lcd.print("0");
}
lcd.print(seconds2);
}
void tray3time(){
currentMillis3 = elapetime3;
seconds3 = currentMillis3 / 1000;
minutes3 = seconds3 / 60;
hours3 = minutes3 / 60;
//unsigned long days = hours / 24;
currentMillis3 %= 1000;
seconds3 %= 60;
minutes3 %= 60;
hours3 %= 24;
lcd.setCursor(2, 1);
if (minutes3 < 10) {
lcd.print("0");
}
lcd.print(minutes3);
lcd.print(":");
if (seconds3 < 10) {
lcd.print("0");
}
lcd.print(seconds3);
}
void overblink(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis3 > interval) {
// save the last time you blinked the LED
previousMillis3 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState3 == LOW)
ledState3 = HIGH;
else
ledState3 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led3, ledState3);
}
}