Countdown Timer issues

Still learning so do hit me to hard with any hard objects please. :slight_smile:

I repurposed some code I used for another countdown timer project and wanted to adapt it into a game for a kids group I run. The Object of the game is to stop the clock before it hits zero. There are five buttons involved. one button starts the countdown, one stops the countdown, one brings the time to zero, one speeds up the countdown and the last one would add seconds to the countdown.

I have two issues with the last two buttons.

One, When the button to speed up the countdown is pressed the stop button will no longer stop the countdown. It works fine when the speed is not increased.
Two, When the button to add seconds to countdown is pressed it just rapidly goes up. I want it to just go up by 10 seconds and not be able to be pressed again.

I do have code to randomize the switch pins so when the game is reset it is not the same but I took it out to trouble shoot.

Thanks for any help on this.

KB

#include <LiquidCrystal.h> // import the lcd library
LiquidCrystal lcd(8, 9, 10, 11, 12, 13); // data and control pins

int r1Switch = 2;
int r2Switch = 3;
int r3Switch = 4; 
int r4Switch = 5;
int startSwitch = 15; //start switch pin
int buzzer = 6;
int led = 7; // red led light that blinks with the countdown


int minute = 0; // the count of minutes upon which we can make the mathematical operations
int second = 90; // the count of seconds upon which we can make the mathematical operations.
String minutestring = ""; //the output variable component for minutes
String secondstring = ""; //the output variable component for seconds
long timethen = 0; //the variable to hold a sample of the time in seconds
boolean runbutton = false; //the variable to control the run button
boolean alarmon = false; // the switch for the alarm
boolean pause = false; // the track for the pause - this wins the game
boolean fast = false; // the variable to control faster countdown


void setup()
{
  pinMode(startSwitch, INPUT_PULLUP); //Using the internal pullup resister (high = off, low = on)
  pinMode(r1Switch, INPUT_PULLUP); //Using the internal pullup resister (high = off, low = on)
  pinMode(r2Switch, INPUT_PULLUP); //Using the internal pullup resister (high = off, low = on)
  pinMode(r3Switch, INPUT_PULLUP); //Using the internal pullup resister (high = off, low = on)
  pinMode(r4Switch, INPUT_PULLUP); //Using the internal pullup resister (high = off, low = on)
  pinMode(buzzer, OUTPUT);



  lcd.begin(16, 2); //start the LCD module
  //display welcome message
  lcd.clear(); // clear just incase there is any residual text
  lcd.setCursor(1, 0);
  lcd.print("Stop The Clock");
  delay(1000);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("Press Start");
}

void loop()
{
  int timezero = minute + second;
  
  if (runbutton == false && digitalRead(startSwitch) == LOW && timezero > 0)
  {
    runbutton = true;  //sets condition for running the timer on buttonpress
    timethen = millis();
  }

  if (runbutton == true && pause == false && digitalRead(r1Switch) == LOW) {
    pause = true;  //switch pause on
    delay(500);
    timethen = timethen + 1000;
    lcd.setCursor(0, 0);
    lcd.print ("You Stopped It! ");
  }

   if (runbutton == true && digitalRead(r2Switch) == LOW) {
    minute = 0;  //clears the countdown and resets to zero
    second = 0;
    runbutton = false;
    lcd.setCursor(0, 0);
    lcd.print (" Sorry You Lost ");
  }

  if (runbutton == true && pause == false)

  if (fast == false && millis() - timethen >= 1000) {
      lcd.setCursor(0, 0);
      lcd.print ("Can You Stop It?");
      timethen = millis();
      second = second - 1; //as a second passes drop down seconds and make tick sound
      tone(buzzer, 1000, 40);
      digitalWrite(led, HIGH);
      delay(40);
      digitalWrite(led, LOW);
    }
    
  if (runbutton == true && fast == false && digitalRead(r3Switch) == LOW) {
    fast = true;
  }
  
  if (runbutton == true && fast == true) {
    millis() - timethen >= 1000;
    lcd.setCursor(0, 0);
    lcd.print ("     HURRY!     ");
    timethen = millis();
    second = second - 1;
    tone(buzzer, 2000, 20);//as a second passes drop down seconds and make tick sound
    digitalWrite(led, HIGH);
    delay(20);
    digitalWrite(led, LOW);
  }
 
  if (runbutton == true && digitalRead(r4Switch) == LOW) {
    second = second + 1; //add seconds
  }

  if (runbutton == true && minute == 0 && second == 0) {
    alarmon = true;
    runbutton = false;
  }
  
  if (minute < 10) {
    minutestring = "0";  //setup for minute output
    minutestring = minutestring + minute;
  } else {
    minutestring = minute;
  }
  if (second < 10) {
    secondstring = "0";  //setup for second output
    secondstring = secondstring + second;
  } else {
    secondstring = second;
  }

  String outstring = minutestring + ":" + secondstring; //compound string for output
  lcd.setCursor(5, 1); lcd.print(outstring); //set position and print compound string


  if (alarmon == true)
  { // activates at end of run time
    tone(buzzer, 1000, 500); //outputs tone, tone (pin, frequency(31-65535), duration
    digitalWrite(led, HIGH);
    lcd.setCursor(0, 0); lcd.print ("***TIME IS UP***");
    delay(500);
    noTone(buzzer);
    lcd.clear(); // clear just incase there is any residual text
    delay(500);

    if (digitalRead(startSwitch) == LOW) {
      alarmon = false; pause = false; digitalWrite(led, LOW); noTone(buzzer); //stop led and buzzer;
    }
  }
}

Can't contribute any helpful code at the moment, but I ran into a similar issue and switched to using while statements instead/in addition to the If's. Just a thought.

Bill

Thanks Bill,

I will look into using while loops and see what I come up with.

KB