สวัสดีค่ะ วงจรนี้เป็นวงจรนับเวลาถอยหลังอยู่ในคำสั่ง "While พอดีสร้างสวิตซ์ขึ้นมาตัวนึงให้กดแล้วหยุดทำงาน (Break) แต่มันกดแล้วไม่หยุดเลยต้องกดค้างไว้ อยากรู้ว่าถ้าจะกดแล้วหยุดนับเวลาทันทีทำยังไงคะ
โค้ด Arduino
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <LCD.h>
#include<EEPROM.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
LiquidCrystal_I2C lcd(I2C_ADDR,2,1,0,4,5,6, 7);
const int stsp = A0; // รับสัญญาณ Analog
const int inc = A1;
const int dec = A2;
const int set = A3;
const int buzz = 9;
const int relay = 8;
const int ledR = 7;
const int ledW = 5;
const int buttonPin =2;
int buttonState =0;
int hrs = 0; // ตั้งค่าตัวแปร 0= ตัวแปร Varied ค่าได้
int Min = 0;
int Sec = 0;
unsigned int check_val = 50;
int add_chk = 0;
int add_sec = 1;
int add_min = 2;
bool RUN = true;
bool min_flag = true;
bool sec_flag = true;
void setup()
{
lcd.begin(20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // ไปที่ตัวอักษรที่ 0 แถวที่ 1
lcd.setCursor(0, 0);
lcd.print(" RT'39 ELECTRONIC");
lcd.setCursor(6, 1);
lcd.print("UV TIMER");
lcd.setCursor(5,3);
lcd.print("Gritsnus");
lcd.setCursor(8,2);
lcd.print("by");
pinMode(buttonPin,INPUT);
pinMode(stsp, INPUT_PULLUP);
pinMode(inc, INPUT_PULLUP);
pinMode(dec, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
pinMode(buzz, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(5,OUTPUT);
digitalWrite(relay, LOW);
digitalWrite(buzz, LOW);
digitalWrite(5,HIGH);
buttonState = digitalRead(buttonPin);
if (EEPROM.read(add_chk) != check_val)
{
EEPROM.write(add_chk, check_val);
EEPROM.write(add_sec, 0);
EEPROM.write(add_min, 2);
}
else
{
Sec = EEPROM.read(add_sec);
Min = EEPROM.read(add_min);
}
INIT();
}
void loop()
{
if (digitalRead(stsp) == LOW)
{
lcd.clear();
RUN = true;
while (RUN)
{
digitalWrite(7,HIGH);
digitalWrite(5,LOW);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite(relay, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" TIMER STOPPED");
lcd.setCursor(0, 1);
lcd.print("----------------");
delay(2000);
digitalWrite(7,LOW);
RUN = false;
digitalWrite(5,HIGH);
INIT();
break;
} else {
digitalWrite(relay, HIGH);
}
digitalWrite(relay, HIGH);
Sec = Sec - 1;
delay(1000);
if (Sec == -1)
{
Sec = 59;
Min = Min - 1;
}
if (Min == -1)
{
Min = 59;
hrs = hrs - 1;
}
if (hrs == -1) hrs = 0;
lcd.setCursor(0, 1);
lcd.print("****************");
lcd.setCursor(4, 0);
if (hrs <= 9)
{
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9)
{
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (Sec <= 9)
{
lcd.print('0');
}
lcd.print(Sec);
if (hrs == 0 && Min == 0 && Sec == 0)
{
digitalWrite(relay, LOW);
digitalWrite(7,LOW);
lcd.setCursor(4, 0);
RUN = false;
for (int i = 0; i < 5; i++)
{
digitalWrite(buzz, HIGH);
delay(100);
digitalWrite(buzz, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
}
INIT1();
}
}
}
if (digitalRead(set) == LOW)
{
delay(500);
while (min_flag)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("------------------- ");
lcd.setCursor(0, 3);
lcd.print("------------------- ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(0, 2);
lcd.print(">");
lcd.setCursor(19, 1);
lcd.print("<");
lcd.setCursor(19, 2);
lcd.print("<");
lcd.setCursor(4, 1);
lcd.print("SET MINUTE: ");
lcd.setCursor(9,2);
lcd.print(Min);
delay(100);
if (digitalRead(inc) == LOW)
{
Min = Min + 1;
if (Min >= 60) Min = 0;
delay(100);
}
if (digitalRead(dec) == LOW)
{
Min = Min - 1;
if (Min <= -1) Min = 0;
delay(100);
}
if (digitalRead(set) == LOW)
{
min_flag = false;
delay(250);
}
}
while (sec_flag)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("------------------- ");
lcd.setCursor(0, 3);
lcd.print("------------------- ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(0, 2);
lcd.print(">");
lcd.setCursor(19, 1);
lcd.print("<");
lcd.setCursor(19, 2);
lcd.print("<");
lcd.setCursor(4, 1);
lcd.print("SET SECOND: ");
lcd.setCursor(9,2);
lcd.print(Sec);
delay(100);
if (digitalRead(inc) == LOW)
{
Sec = Sec + 1;
if (Sec >= 60) Sec = 0;
delay(100);
}
if (digitalRead(dec) == LOW)
{
Sec = Sec - 1;
if (Sec <= -1) Sec = 0;
delay(100);
}
if (digitalRead(set) == LOW)
{
sec_flag = false;
delay(250);
}
}
if (Sec == 0 && Min == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" INVAID TIME");
delay(2000);
}
else
{
EEPROM.write(add_sec, Sec);
EEPROM.write(add_min, Min);
}
INIT1();
}
}
void INIT()
{
Sec = 0;
Min = 10;
hrs = 0;
digitalWrite(5,HIGH);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Start / Stop Time");
lcd.setCursor(6, 2);
if (hrs <= 9)
{
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9)
{
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (Sec <= 9)
{
lcd.print('0');
}
lcd.print(Sec);
min_flag = true;
sec_flag = true;
delay(500);
}
void INIT1()
{
Sec = EEPROM.read(add_sec);
Min = EEPROM.read(add_min);
hrs = 0;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Start / Set Time");
lcd.setCursor(6, 2);
if (hrs <= 9)
{
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9)
{
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (Sec <= 9)
{
lcd.print('0');
}
lcd.print(Sec);
min_flag = true;
sec_flag = true;
delay(500);
}
ต้องการออกจากคำสั่ง While ทันทีทำยังไง???
โค้ด Arduino
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <LCD.h>
#include<EEPROM.h>
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
LiquidCrystal_I2C lcd(I2C_ADDR,2,1,0,4,5,6, 7);
const int stsp = A0; // รับสัญญาณ Analog
const int inc = A1;
const int dec = A2;
const int set = A3;
const int buzz = 9;
const int relay = 8;
const int ledR = 7;
const int ledW = 5;
const int buttonPin =2;
int buttonState =0;
int hrs = 0; // ตั้งค่าตัวแปร 0= ตัวแปร Varied ค่าได้
int Min = 0;
int Sec = 0;
unsigned int check_val = 50;
int add_chk = 0;
int add_sec = 1;
int add_min = 2;
bool RUN = true;
bool min_flag = true;
bool sec_flag = true;
void setup()
{
lcd.begin(20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // ไปที่ตัวอักษรที่ 0 แถวที่ 1
lcd.setCursor(0, 0);
lcd.print(" RT'39 ELECTRONIC");
lcd.setCursor(6, 1);
lcd.print("UV TIMER");
lcd.setCursor(5,3);
lcd.print("Gritsnus");
lcd.setCursor(8,2);
lcd.print("by");
pinMode(buttonPin,INPUT);
pinMode(stsp, INPUT_PULLUP);
pinMode(inc, INPUT_PULLUP);
pinMode(dec, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
pinMode(buzz, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(5,OUTPUT);
digitalWrite(relay, LOW);
digitalWrite(buzz, LOW);
digitalWrite(5,HIGH);
buttonState = digitalRead(buttonPin);
if (EEPROM.read(add_chk) != check_val)
{
EEPROM.write(add_chk, check_val);
EEPROM.write(add_sec, 0);
EEPROM.write(add_min, 2);
}
else
{
Sec = EEPROM.read(add_sec);
Min = EEPROM.read(add_min);
}
INIT();
}
void loop()
{
if (digitalRead(stsp) == LOW)
{
lcd.clear();
RUN = true;
while (RUN)
{
digitalWrite(7,HIGH);
digitalWrite(5,LOW);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite(relay, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" TIMER STOPPED");
lcd.setCursor(0, 1);
lcd.print("----------------");
delay(2000);
digitalWrite(7,LOW);
RUN = false;
digitalWrite(5,HIGH);
INIT();
break;
} else {
digitalWrite(relay, HIGH);
}
digitalWrite(relay, HIGH);
Sec = Sec - 1;
delay(1000);
if (Sec == -1)
{
Sec = 59;
Min = Min - 1;
}
if (Min == -1)
{
Min = 59;
hrs = hrs - 1;
}
if (hrs == -1) hrs = 0;
lcd.setCursor(0, 1);
lcd.print("****************");
lcd.setCursor(4, 0);
if (hrs <= 9)
{
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9)
{
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (Sec <= 9)
{
lcd.print('0');
}
lcd.print(Sec);
if (hrs == 0 && Min == 0 && Sec == 0)
{
digitalWrite(relay, LOW);
digitalWrite(7,LOW);
lcd.setCursor(4, 0);
RUN = false;
for (int i = 0; i < 5; i++)
{
digitalWrite(buzz, HIGH);
delay(100);
digitalWrite(buzz, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
}
INIT1();
}
}
}
if (digitalRead(set) == LOW)
{
delay(500);
while (min_flag)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("------------------- ");
lcd.setCursor(0, 3);
lcd.print("------------------- ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(0, 2);
lcd.print(">");
lcd.setCursor(19, 1);
lcd.print("<");
lcd.setCursor(19, 2);
lcd.print("<");
lcd.setCursor(4, 1);
lcd.print("SET MINUTE: ");
lcd.setCursor(9,2);
lcd.print(Min);
delay(100);
if (digitalRead(inc) == LOW)
{
Min = Min + 1;
if (Min >= 60) Min = 0;
delay(100);
}
if (digitalRead(dec) == LOW)
{
Min = Min - 1;
if (Min <= -1) Min = 0;
delay(100);
}
if (digitalRead(set) == LOW)
{
min_flag = false;
delay(250);
}
}
while (sec_flag)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("------------------- ");
lcd.setCursor(0, 3);
lcd.print("------------------- ");
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(0, 2);
lcd.print(">");
lcd.setCursor(19, 1);
lcd.print("<");
lcd.setCursor(19, 2);
lcd.print("<");
lcd.setCursor(4, 1);
lcd.print("SET SECOND: ");
lcd.setCursor(9,2);
lcd.print(Sec);
delay(100);
if (digitalRead(inc) == LOW)
{
Sec = Sec + 1;
if (Sec >= 60) Sec = 0;
delay(100);
}
if (digitalRead(dec) == LOW)
{
Sec = Sec - 1;
if (Sec <= -1) Sec = 0;
delay(100);
}
if (digitalRead(set) == LOW)
{
sec_flag = false;
delay(250);
}
}
if (Sec == 0 && Min == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" INVAID TIME");
delay(2000);
}
else
{
EEPROM.write(add_sec, Sec);
EEPROM.write(add_min, Min);
}
INIT1();
}
}
void INIT()
{
Sec = 0;
Min = 10;
hrs = 0;
digitalWrite(5,HIGH);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Start / Stop Time");
lcd.setCursor(6, 2);
if (hrs <= 9)
{
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9)
{
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (Sec <= 9)
{
lcd.print('0');
}
lcd.print(Sec);
min_flag = true;
sec_flag = true;
delay(500);
}
void INIT1()
{
Sec = EEPROM.read(add_sec);
Min = EEPROM.read(add_min);
hrs = 0;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Start / Set Time");
lcd.setCursor(6, 2);
if (hrs <= 9)
{
lcd.print('0');
}
lcd.print(hrs);
lcd.print(':');
if (Min <= 9)
{
lcd.print('0');
}
lcd.print(Min);
lcd.print(':');
if (Sec <= 9)
{
lcd.print('0');
}
lcd.print(Sec);
min_flag = true;
sec_flag = true;
delay(500);
}