Many hours of searching and trial/error but I need help now. I'm trying to build a countdown timer that only counts down while a button is being held down. When the button is not held down, I would like the countdown to pause. When the button is held down again, I would like the countdown to resume. I feel like this is so simple that I'm over-thinking it. My code is below, but please don't get side-tracked by how bad it is...I'm a newb. If its easier to start over fresh, I'm perfectly fine with that. Thank you
#include <EEPROM.h>
#include <LiquidCrystal.h>
int X;
int Y;
int Z;
int W;
int V;
int U;
int T;
int x = -1;
int address = 0;
int airAddress = 1;
const int airTime = EEPROM.read(airAddress) - EEPROM.read(address);
int button1 = 8;
int button2 = 9;
int button3 = 10;
int previouslyPressed1 = 0;
int previouslyPressed2 = 0;
int buzzer = 13;
int AA = 0;
int stopSecond = 0; //stopwatch seconds
int stopMinuite = 0; //stopwatch minutes
int theTime;
int Latch;
LiquidCrystal lcd(12,11,5,4,3,2);
void setup(){
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(buzzer, OUTPUT);
lcd.clear();
lcd.begin(16,2);
W = 300;
U = 0; //These are the "seconds" in Timer
T = airTime; //These are the minutes in Timer
lcd.print(" GIA Timer");
delay(500);
lcd.clear();
}
void loop()
{
while (x == -1){ //This is the start screen x==-1 is normal
lcd.setCursor(0,0);
lcd.print("Timer - 1");
//lcd.setCursor(0,1);
//lcd.print("Stopwatch - 2");
/////////////////////////////START SCREEN END//////////////////////////////////////////
/////////////////////////////OPTIONS MENU/////////////////////////////////////////////
if(digitalRead(button3) == HIGH) //this enters timer mode
{x = 0;
lcd.clear();}
/*if(digitalRead(button2) == HIGH) //this enters stopwatch mode
{
x = 5;
lcd.clear();
lcd.print("Stopwatch");
delay(1000);
lcd.clear();
}*/
}
/////////////////////////////THIS IS ENTERING THE TIMER MENU//////////////////////////
while (x == 0) //this screen is for setting the minutes for the timer (also button 3)
{
while(AA == 0){
lcd.print("Minute Hand");
delay(1000);
AA = 1;
lcd.clear();
}
if ( T < 0 )
{
T = 0;
lcd.clear();
}
/*if((digitalRead(button1) == HIGH) && (previouslyPressed1 == 0)) { //timer minute increase by 1
T = T + 1;
previouslyPressed1 = 1;
delay(50);
lcd.clear();
}
if((digitalRead(button1) == LOW) && (previouslyPressed1 == 1)) { //unknown
previouslyPressed1 = 0;
delay(50);
}
if((digitalRead(button2) == HIGH) && (previouslyPressed2 == 0)) { //timer minute decrease by 1
T = T - 1;
previouslyPressed2 = 1;
delay(50);
lcd.clear();
}
if((digitalRead(button2) == LOW) && (previouslyPressed2 == 1)) { //unknown
previouslyPressed2 = 0;
delay(50);
}*/
lcd.setCursor(0,0);
lcd.print(T);
lcd.print(":");
if (U > 9)
{ //The U portion is for, example: "12" and "4" (i.e. to eliminate zeros
lcd.print(U);
}
else{
lcd.print("0");
lcd.print(U);
}
///////////////////////////////////////MINUTES SCREEN END////////////////////////////////
/////////////////////////////////////SECONDS SCREEN BEGIN///////////////////////////////
if (digitalRead(button3) == HIGH) //this is if you press button 3 after your finished
{ //selected the minutes (enters the "seconds" screen)
x = 1;
V = (millis()/(1000));
lcd.clear();
lcd.setCursor(0,0);
lcd.clear();
lcd.print("Second Hand");
lcd.setCursor(0,1);
lcd.print("(Hold to ignore)");
lcd.setCursor(0,0);
delay(1000);
lcd.clear();
}}
while (x == 1) //this screen is for setting the "seconds" for the timer
{
if ( U > 59 ){ //this ensures that the seconds work properly
U = 0;}
if(U < 0){
U = 59;
lcd.clear();}
/*if((digitalRead(button1) == HIGH) && (previouslyPressed1 == 0)) { //timer second increase by 1
U = U + 1;
previouslyPressed1 = 1;
delay(50);
lcd.clear();
}
if((digitalRead(button1) == LOW) && (previouslyPressed1 == 1)) { //unknown
previouslyPressed1 = 0;
delay(50);
}
if((digitalRead(button2) == HIGH) && (previouslyPressed2 == 0)) { //timer second decrease by 1
U = U - 1;
previouslyPressed2 = 1;
delay(50);
lcd.clear();
}
if((digitalRead(button2) == LOW) && (previouslyPressed2 == 1)) { //unkown
previouslyPressed2 = 0;
delay(50);
}*/
lcd.setCursor(0,0);
lcd.print(T);
lcd.print(":");
if (U > 9){ //Ensures proper zeros
lcd.print(U);
}
else{
lcd.print("0");
lcd.print(U);
}
////////////////////////THIS IS WHAT HAPPENS AFTER HITTING THE "TIMER START" BUTTON////////
if (digitalRead(button3) == HIGH && digitalRead(button2)==HIGH)
{
x = 2; //while timer is started, set x = 2
if (T > 0){ //if timer minutes greater than zero
W = (T*60) + U;} //this is the total number of seconds
else{
W = U;}
lcd.clear();
lcd.setCursor(0,0);
V = (millis()/1000);
}
}
////////////////////////////THE TIMER STARTS HERE////////////////////////////////////
X = (W - (millis()/(1000))) + V;
//Here is the Second to Minute-second Conversion. X is the second input
Y = X/60; //Y is the minute output
Z = X - (60*Y); //Z is the second output
// This is the display of the minutes
lcd.setCursor(0,0);
lcd.print(Y);
lcd.print(":");
if (Z > 9)
{
lcd.print(Z);
lcd.print(" ");
}
else
{
lcd.print("0");
lcd.print(Z);
lcd.print(" ");
}
if((Y == 0) && (Z == 0))
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Timer is over");
while(digitalRead(button3) == LOW)
{
digitalWrite(buzzer, HIGH); // turn the buzzer on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(buzzer, LOW); // turn the buzzer off by making the voltage LOW
delay(100);
}
x = -1;
delay(1000);
lcd.clear();
AA = 0;
}
////////////////////////THE END OF TIMER AND ALL ASSOCIATED FUNCTIONS//////////////////////
////////////////////////START OF THE STOPWATCH AND FUNCTIONS//////////////////////////////
/*while(x == 5){
if(stopSecond > 59)
{
stopSecond = 0;
stopMinuite = stopMinuite + 1;
theTime = theTime + 60;
}
lcd.setCursor(0,0);
lcd.print(stopMinuite);
lcd.print(":");
if (stopSecond > 9){
lcd.print(stopSecond);}
else{
lcd.print("0");
lcd.print(stopSecond);}
if((digitalRead(button3) == HIGH) && (Latch == 0))
{
Latch = 1 ;
theTime = (millis()/(1000));
digitalWrite(buzzer, HIGH); // turn the buzzer on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(buzzer, LOW); // turn the buzzer off by making the voltage LOW
delay(100);
}
if( Latch == 1)
{
stopSecond = (millis()/(1000)) - theTime ;
}
//if(digitalRead(button2 == HIGH)){
//Latch = 0;}
if(digitalRead(button2) == HIGH)
{
Latch = 0;
digitalWrite(buzzer, HIGH); // turn the buzzer on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(buzzer, LOW); // turn the buzzer off by making the voltage LOW
delay(100);
// stopSecond = 0;
// stopMinuite = 0;
}
if(digitalRead(button1) == HIGH) //if x=-1 then go back to main menu
{
x = -1;
}*/
}