/* ------------ ประกาศตัวแปรแทนขา I/O ----------- */
int SW1 = 7;
int SW2 = 10;
int SW3 = 6;
/* ----------- ประกาศตัวแปรสำหรับ Set เวลา --------- */
bool st_SetH = 0;
bool st_SetM = 0;
bool st_SetS = 0;
byte SetH;
byte SetM;
byte SetS;
/* **************************************************** */
void setup() {
pinMode(SW1, INPUT);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
Serial.begin(9600);
};
/* ***************************************************** */
void loop() {
if(digitalRead(SW2) == 0){ // กด SW2 เพื่อเข้า Mode Set
delay(2000);
if(digitalRead(SW2) == 0){ // ตรวจสอบการกด SW2 อีกครั้ง ว่ายังกดค้างอยู่มั๊ย
Serial.println("Set Timer Mode");
Serial.println();
Serial.println("Set Hours");
st_SetH = 1;
// ----------- Set Hours ----------
while(st_SetH == 1){
if(digitalRead(SW1) == 0){ // เพิ่มค่าชั่วโมงครั้งละ 1
while(digitalRead(SW1) == 0) delay(1);
SetH = SetH + 1;
if(SetH >= 24) SetH = 24;
Serial.println("H:" + String(SetH));
delay(50);
}
if(digitalRead(SW3) == 0){ // ลดค่าชั่วโมงครั้งละ 1
while(digitalRead(SW3) == 0) delay(1);
SetH = SetH - 1;
if(SetH == 255) SetH = 0;
Serial.println("H:" + String(SetH));
delay(50);
}
if(digitalRead(SW2) == 0){ // ปุ่มยืนยันการตั้งค่า ชั่วโมง
delay(1000);
if(digitalRead(SW2) == 0){
st_SetH = 0;
st_SetM = 1;
}
}
} // while Set Hours
// ----------- Set Minute -----------
Serial.println("Set Minute");
while(st_SetM == 1){
if(digitalRead(SW1) == 0){ // เพิ่มค่าชั่วโมงครั้งละ 1
while(digitalRead(SW1) == 0) delay(1);
SetM = SetM + 5;
if(SetM >= 60) SetM = 60;
Serial.println("M:" + String(SetM));
delay(50);
}
if(digitalRead(SW3) == 0){ // ลดค่าชั่วโมงครั้งละ 1
while(digitalRead(SW3) == 0) delay(1);
SetM = SetM - 5;
if(SetM >= 250) SetM = 0;
Serial.println("M:" + String(SetM));
delay(50);
}
if(digitalRead(SW2) == 0){ // ปุ่มยืนยันการตั้งค่า ชั่วโมง
delay(1000);
if(digitalRead(SW2) == 0){
st_SetM = 0;
st_SetS = 1;
}
}
}
// ----------- Set Secound -----------
Serial.println("Set Secound");
while(st_SetS == 1){
if(digitalRead(SW1) == 0){ // เพิ่มค่าชั่วโมงครั้งละ 1
while(digitalRead(SW1) == 0) delay(1);
SetS = SetS + 5;
if(SetS >= 60) SetS = 60;
Serial.println("S:" + String(SetS));
delay(50);
}
if(digitalRead(SW3) == 0){ // ลดค่าชั่วโมงครั้งละ 1
while(digitalRead(SW3) == 0) delay(1);
SetS = SetS - 5;
if(SetS >= 250) SetS = 0;
Serial.println("M:" + String(SetS));
delay(50);
}
if(digitalRead(SW2) == 0){ // ปุ่มยืนยันการตั้งค่า ชั่วโมง
delay(1000);
if(digitalRead(SW2) == 0){
st_SetS = 0;
}
}
}
// --------------------------------
} // if กด SW2 ค้าง
} // if Mode Set
Serial.println("LooP");
delay(1000);
};