Countdown Timer switch case

i am new in arduino.. i want to mke a countdown timer by using two push buttons and lcd 16*2 . how can i use switch case and break...

Countdownt timer function::

Push button 1 : Start, stop, reset by pressing twice

Push Button 2: Time count increases by 1 minute each time by press button 2

Flow::

① Set the time with push button 2.

② Start timer with push button 1

③ push button 1 to stop the timer

④ Start the timer again with push button 1

⑤ The buzzer will sounds in when it will be 0 seconds.

⑥ push button 1 will stop the buzzer

⑦ Return to ①

sketch::

#include <Debounce.h>
#include <LiquidCrystal.h>
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

unsigned long int timeNow = 0;
unsigned long int elapsedTime = 0;
int button1State = 0;
int button2State = 0;

unsigned long int setTime = 0;

String readableTime;
byte button1 = 4;
byte button2 = 5;
byte Buzzer = 3;

int timerStatus = 0;
int tPause = 0;
int timeUp = 0;

unsigned long int seconds;
unsigned long int minutes;
unsigned long int timePassed;
unsigned long int remainingTime = 0;

unsigned long int pausedTime = 0;
unsigned long int resumeTime = 0;
unsigned long int startTime = 0;
unsigned long int totalPausedTime = 0;
unsigned long int timePaused = 0;

Debounce Button1(button1); // Button1 debounced, with default 50ms debounce time.
Debounce Button2(button2, 50, true); // Button2 debounced, with custom debounce time.

void setup() {
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT); // Also works with INPUT_PULLUP
  pinMode(Buzzer, OUTPUT);

  //getReadableTime(readableTime);
  lcd.begin(16,2);
  lcd.clear();
  //lcd.backlight();      // Turning on the backlight
  lcd.setCursor(0, 0);  //Set LCD cursor to character 3 on line 0
  lcd.print("TIMER");
  lcd.setCursor(0, 1);  //Move LCD cursor to character 0 on line 1
  lcd.print("00:00");
}

void loop() {
lcd.clear();
    lcd.setCursor(0, 0);  //Set LCD cursor to character 3 on line 0
    lcd.print("TIMER");
    lcd.setCursor(2, 1);  //Move LCD cursor to character 0 on line 1
    lcd.print(":");
    lcd.setCursor(3, 1);  //Move LCD cursor to character 0 on line 1
    lcd.print("00");
    lcd.setCursor(0, 1);  //Move LCD cursor to character 0 on line 1
    lcd.print("00");
    
  if (Button1.read() == HIGH && button1State == 1) {
    Serial.println("Butten 1!");
    startTime = millis();
    timerStatus = 1;
    timePassed = millis() - startTime - totalPausedTime;
    lcd.clear();
    lcd.setCursor(0, 0);  //Set LCD cursor to character 3 on line 0
    lcd.print("TIMER");
    lcd.setCursor(4, 1);  //Move LCD cursor to character 0 on line 1
    lcd.print("00");
    lcd.setCursor(0, 1);  //Move LCD cursor to character 0 on line 1
    lcd.print("00");
    button1State = 0;
    delay(800);
    while (timerStatus != 0) {
      //button1Action();
      timePassed = millis() - startTime - totalPausedTime;
      remainingTime = setTime * 1000 * 60 - timePassed;

      seconds = remainingTime / 1000;
      minutes = seconds / 60;

      lcd.clear();
      lcd.setCursor(0, 0);  //Set LCD cursor to character 3 on line 0
      lcd.print("TIMER");
      lcd.setCursor(4, 1);  //Move LCD cursor to character 0 on line 1
      lcd.print(seconds);
      lcd.setCursor(0, 1);  //Move LCD cursor to character 0 on line 1
      lcd.print(minutes);

      Serial.print("Time Passed:  ");
      Serial.println(timePassed/1000);
      //paused
      if (Button1.read() == HIGH && tPause == 0 && button1State == 1) {
        Serial.println("Paused ");
        tPause = 1;
        pausedTime = millis();
        button1State = 0;
        while (Button1.read()) {
          Serial.print(".");
        }
        delay(800);
      }
      // start
      else if (Button1.read() == HIGH && tPause == 1 && button1State == 1) {
        Serial.println("Started ");
        tPause = 0;
        resumeTime = millis();
        totalPausedTime = resumeTime - pausedTime;
        button1State = 0;
        delay(800);
      }
      // Time Out
      else if (tPause == 0 && timePassed >= setTime * 1000 * 60) {
        Serial.println("Time Out ");
        
        ////buzz 0n///
        tone(Buzzer, 1000); // Send 1KHz sound signal...
        delay(1000);        // ...for 1 sec
        seconds = 0;
        minutes = 0;
        timeUp = 1;
        timerStatus = 0;
        setTime = 0;
        remainingTime = 0;
        pausedTime = 0;
        resumeTime = 0;
        startTime = 0;
        totalPausedTime = 0;
        timePaused = 0;
        break;
      }
      else {
        button1State = 1;
        button2State = 1;
        delay(100);
      }
      delay(500);
    }
  }

  else if ( Button2.read() == HIGH && button2State == 1) {
    Serial.println("Butten 2!");
    button2State = 0;
    //button2Action();
    setTime++;
    button2State = 0;
    lcd.clear();
    lcd.setCursor(0, 0);  //Set LCD cursor to character 3 on line 0
    lcd.print("TIMER");
    lcd.setCursor(0, 4);  //Move LCD cursor to character 0 on line 1
    lcd.print(setTime);
    delay(800);

  }
  else {
    button1State = 1;
    button2State = 1;
    delay(100);
  }
  if (timeUp = 1 && Button1.read() == HIGH) {
    // Buzz off
    Serial.println("Buzzer Off");
    digitalWrite(Buzzer, LOW);
    timeUp = 0;
  }
}

Welcome to the forums. Thanks for posting your code correctly!

What is your program doing now that does not match your description?

A common technique is to use a state machine (or finite state machine). Your sketch is in a certain state (Setting time, paused, counting down, etc.) and depending on what state it is in, it does what needs to get done.

hi and welcome.

What does your code do now, and how is that different to what you want?

Using one button for multiple purposes can get tricky, particularly things like press twice to reset.

Buttons are cheap, you’ve got input pins to spare: I suggest you to just add a few buttons and code for them as individuals.

Later as a refinement, get the multipurpose stuff going.

Or don’t. There are ppl who have some trouble with short and long presses being two things, and managing a two presses that works consistently for them leading to frustration experience by the user.

a7

I've moved your question, it seems to be about programming more than anything else.

Buzzer Off
Butten 1!
Time Passed:  0
Time Out 
Buzzer Off
Butten 2!
Buzzer Off
Buzzer Off
Butten 1!
Time Passed:  0
Time Passed:  1
Paused 
..................................................................................................................................................................

thanks for your comment..
it's automatically reapting again and again..

want to use switch case .
as:
switch()
case1:
timerstart()
case2:
time setup()
case3:
time pause()
case4:
time resume()
case5:
stop buzzer()

thank you for your reply sir,
it's loop automatically reapting again and again..
as below:

Buzzer Off
Butten 1!
Time Passed:  0
Time Out 
Buzzer Off
Butten 2!
Buzzer Off
Buzzer Off
Butten 1!
Time Passed:  0
Time Passed:  1
Paused 
...............................................................................

i am learning arduino by myself..
i want to make it by using two button..
thank you very much...

thanks for your help..
i really appreciate your contribution..
it will be very helpfull if you try it more..
i am also trying..

thanks for your comment sir .
i am learning arduino.
actually i need sugestion how i can complete this...

I hope this work all correctly

#include <Debounce.h>
#include <LiquidCrystal.h>
const byte rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define b1 4
#define b2 5
#define Buzzer 3

#define doublePressPeriod 200
#define isDoublePress ((millis()-lastPress)<doublePressPeriod)

bool timerStatus = false;

unsigned long  StartMillis = 0;
unsigned long  remainingTime = 0;
unsigned long  lastPress = 0;

Debounce Button1(b1); //  debounced, with default 50ms debounce time.
Debounce Button2(b2); //

void setup() {
  Serial.begin(9600);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(Buzzer, OUTPUT);

  lcd.begin(16, 2);
  lcd.clear();
  //lcd.backlight();      // Turning on the backlight
  lcd.setCursor(0, 0);  //Set LCD cursor to character 3 on line 0
  lcd.print("TIMER");
  lcd.setCursor(0, 1);  //Move LCD cursor to character 0 on line 1
  lcd.print("00:00");
  lastPress =  millis() ;
}

void loop() {
  if (timerStatus == false && Button2.read() == HIGH) {
    remainingTime = remainingTime + 60000UL;
    if (remainingTime < 5940000UL) remainingTime = 5940000UL;
    showTime();
  }
  if (timerStatus == false && Button1.read() == HIGH) {
    if (isDoublePress)resett();
    else {
      if (remainingTime > 0) {
        timerStatus = true;
        lastPress =  millis();
        StartMillis = millis();
      }
    }
  }
  if (timerStatus == true && Button1.read() == HIGH) {
    if (isDoublePress)resett();
    else {
      timerStatus = false;
      lastPress =  millis();
      remainingTime = remainingTime - (millis() - StartMillis);
    }
  }
  if (timerStatus == true && (millis() - StartMillis) % 1000) {
    remainingTime = remainingTime - (millis() - StartMillis);
    showTime();
  }
  if (timerStatus == true && (millis() - StartMillis > remainingTime)) buzz();
}
void resett() {
  timerStatus = false;
  remainingTime = 0;
  lastPress = millis();
  showTime();
}
void buzz() {
  Serial.println("Time Out ");
  timerStatus = false;
  remainingTime = 0;
  bool x = true;
  while (x) {
    if (Button1.read() == HIGH) {
      x = !x;
    }
    tone(Buzzer, 1000, 1000); // Send 1KHz sound signal for 1 sec
    delay(1000);
  }
  lastPress =  millis();
  Serial.println("Buzzer Off");
  digitalWrite(Buzzer, LOW);  //for sure
}
void showTime() {
  int Seconds = floor(remainingTime / 1000UL);
  int Minute = floor(Seconds / 60);
  Seconds = Seconds - Minute * 60;
  lcd.setCursor(0, 1);
  if (Minute < 10)lcd.print('0');
  lcd.print(Minute);
  lcd.print(':');
  if (Seconds < 10)lcd.print('0');
  lcd.print(Seconds);
  delay(50);
}

Sir kolaha,
thanks for your hard work.
but the code is not working. I tried in many ways.
LCD only shows backlight, no text and buttons are also not working.
I will be grateful if you help me a little more.

if you not see text, how do you know that "button not working" ?
anyway, show pictures what you got

Sir kolaha,
Thanks for your continuous support.
Sorry, the LCD was defective. I checked it with another LCD.
button and lcd are working properly.(by using Serial.println)
but now the problem is it's repeating so fast from 0 to 99 automatically.
is it possible to use switch case and break;

Now that you have had some success, show us your current sketch.

What buzzer are you using ?

Eh, yes, my sketch is wrong in a couple of places. I tried to solve but not done yet.

Passive buzzer

Thanks for your support sir...

I found a reference website: https://www.sys-link.jp/it/electronic-kit/arduino/arduino-011/

difference is four 7 segment display and lcd 16*2.

here is the code:

const int pinSwCnt = A1; // Input switch
const int pinSwSet = A0; // Input switch

///7 Segpin
const int pin7segA  = 6;
const int pin7segB  = 7;
const int pin7segC  = 8;
const int pin7segD  = 9;
const int pin7segE  = 10;
const int pin7segF  = 12;
const int pin7segG  = 13;

/// 7-segment digit control
const int pin7segDig1 = A3;
const int pin7segDig2 = 2;
const int pin7segDig3 = 3;
const int pin7segDig4 = 4;

// Time operation switch related
int swCntPrevState = 0; // Previous switch state
int swCntCurrState = 0; // Current switch state
int swCntUpEdge = 0; // Rising edge detection
int pushSingle = 0; // Press once
int pushDouble = 0; // Press twice

// Time setting switch related
int swSetPrevState = 0; // Previous switch state
int swSetCurrState = 0; // Current switch state
long timeStartSet; //time setting
long timePrevCtUp;

//count
long timeNow; //now time
long timeStart; // Timer start time
long timeStop; //Temporary stop time
long timeUpEdge; // Time when the rising edge was detected
int numUpEdge = 0; // Number of rising edges detected
int setTime = 60; // Set time (in seconds)
int timeSet = setTime; // Switch set time
int timeDisp = setTime; //represents the time
int numDisp [4]; // Number displayed on the LED

//control
int i;
int timeState = 0; // Timer state 0: Initial state 1: Operation 2: Complete 3: Stop

void setup()
{
  // Initialize input / output
  pinMode( pinSwCnt, INPUT );
  pinMode( pinSwSet, INPUT );

  pinMode( pin7segA,  OUTPUT );
  pinMode( pin7segB,  OUTPUT );
  pinMode( pin7segC,  OUTPUT );
  pinMode( pin7segD,  OUTPUT );
  pinMode( pin7segE,  OUTPUT );
  pinMode( pin7segF,  OUTPUT );
  pinMode( pin7segG,  OUTPUT );

  pinMode( pin7segDig1,  OUTPUT );
  pinMode( pin7segDig2,  OUTPUT );
  pinMode( pin7segDig3,  OUTPUT );
  pinMode( pin7segDig4,  OUTPUT );
  
  // Debug serial
  Serial.begin(9600);

}

void loop()
{

  timeNow = millis (); // Get the current time

  // Time operation switch rising edge detection
  swCntCurrState = digitalRead (pinSwCnt); // Current switch state
  if (swCntCurrState == HIGH && swCntCurrState! = SwCntPrevState) {// Compare the current switch state with the previous switch state
    swCntUpEdge = 1;
    if( numUpEdge == 0 )
      timeUpEdge = timeNow;
    numUpEdge ++;
  }
  else
    swCntUpEdge = 0;
  swCntPrevState = swCntCurrState; // Previous switch state
  
  // Judgment that the number of switches is 1 or 2
  if( numUpEdge >= 1 ){
    if( numUpEdge >= 2 ){
      pushDouble = 1;
      numUpEdge = 0;
    }
    else if( timeNow-timeUpEdge > 600 ){
      pushSingle = 1;
      numUpEdge = 0;
    }
  }

  // Judgment of operating status
  switch(timeState){
    case 1: //Action
      if( pushSingle == 1 ){
        timeState = 3;
        timeStop = timeNow;
  
      }
      else if( (timeNow-timeStart)/1000>=(timeSet) ){
        timeState = 2;
      }
      break;
  
    case 2: //finished
      if( pushSingle==1 || pushDouble==1 ){
        timeState = 0;
      timeDisp = setTime;
      }
      break;
  
    case 3: //stop
      if( pushSingle==1 ){
        timeState = 1;
        timeStart = timeNow - (timeStop-timeStart);
 
      }
      else if( pushDouble == 1 ){
        timeState = 0;
        timeDisp = setTime;
      }
      break;
  
   default : //Initial
      if( pushSingle == 1 ){
        timeState = 1;
        timeSet   = timeDisp;
        timeStart = timeNow;
 
      }
      break;
  }
  pushSingle = 0;
  pushDouble = 0;


  // Calculation of display time
  switch(timeState){
    case 1: //Action
      timeDisp = timeSet-((timeNow-timeStart) / 1000); // The difference between the start time and the current time is the display time
      break;
  
    case 2: //finished
      timeDisp = 0;
      break;
  
    case 3: //stop
      timeDisp = timeSet-((timeStop-timeStart) / 1000); // The difference between the start time and the stop time is the display time
      break;
  
    default : //Initial
      break;
  }

  // Time setting switch Rising edge detection
  swSetCurrState = digitalRead (pinSwSet); // Current switch state
  if (timeState == 0 && swSetCurrState == HIGH && swSetCurrState! = SwSetPrevState) {// Compare the current switch state with the previous switch state
    timeStartSet = timeNow;
    timePrevCtUp = timeNow;
    up_time_disp();
  }
  swSetPrevState = swSetCurrState; // Previous switch state

  if( swSetCurrState==HIGH && timeState==0   ){
    if ((timeNow-timePrevCtUp)> = 1000) {// Count up every second within 10 seconds after pressing
      show_param();
      up_time_disp();
      timePrevCtUp = timeNow;
    }
    else if ((timeNow-timeStartSet)> 5000 && (timeNow-timePrevCtUp)> = 333) {// Count up every 1/3 second within 20 seconds after pressing
      show_param();
      up_time_disp();
      timePrevCtUp = timeNow;
    }
    else if ((timeNow-timeStartSet)> 10000 && (timeNow-timePrevCtUp)> = 100) {// Count up every 0.1 seconds after 20 seconds after pressing
      show_param();
      up_time_disp();
      timePrevCtUp = timeNow;
    }
    else if ((timeNow-timeStartSet)> 15000 && (timeNow-timePrevCtUp)> = 33) {// Count up every 0.1 seconds after 20 seconds after pressing
      show_param();
      up_time_disp();
      timePrevCtUp = timeNow;
    }
  }

  // Convert display time for 7-segment LED
  numDisp [0] = timeDisp% 10; // 1 second unit
  numDisp [1] = (timeDisp% 60) / 10; // in seconds
  numDisp [2] = (timeDisp / 60)% 10; // 1 minute unit
  numDisp [3] = (timeDisp / 600)% 10; // 10 minutes
  
  /// 7-segment LED control
  if( i ==3 )
    i = 0;
  else
    i++;
    
  switch(i){
    case 1:
      digitalWrite(pin7segDig4  , HIGH  );
      digitalWrite(pin7segDig3  , LOW   );
      digitalWrite(pin7segDig2  , HIGH  );
      digitalWrite(pin7segDig1  , HIGH  );
      break;
  
    case 2:
      digitalWrite(pin7segDig4  , HIGH  );
      digitalWrite(pin7segDig3  , HIGH  );
      digitalWrite(pin7segDig2  , LOW   );
      digitalWrite(pin7segDig1  , HIGH  );
      break;
  
    case 3:
      digitalWrite(pin7segDig4  , HIGH  );
      digitalWrite(pin7segDig3  , HIGH  );
      digitalWrite(pin7segDig2  , HIGH  );
      digitalWrite(pin7segDig1  , LOW   );
      break;
  
   default :
      digitalWrite(pin7segDig4  , LOW   );
      digitalWrite(pin7segDig3  , HIGH  );
      digitalWrite(pin7segDig2  , HIGH  );
      digitalWrite(pin7segDig1  , HIGH  );
      break;
  }
  disp_7seg( numDisp[i] );


  delay(5);

}

void up_time_disp()
{
  if( timeDisp == 3600 )
    timeDisp = 60;
  else
   timeDisp += 60;
   
  Serial.print(timeDisp);
  Serial.print("\n");

}


  
void show_param(  )
{
       Serial.print(timeNow);
       Serial.print("\t");
       Serial.print(timeStartSet);
       Serial.print("\t");
       Serial.print(timeNow-timeStartSet);
       Serial.print("\t");
       Serial.print("\n");
}

void disp_7seg( int numDisp )
{
      switch(numDisp){
      case 1:
        digitalWrite(pin7segA  , LOW  );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , LOW  );
        digitalWrite(pin7segE  , LOW  );
        digitalWrite(pin7segF  , LOW  );
        digitalWrite(pin7segG  , LOW  );
        break;
    
      case 2:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , LOW  );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , HIGH );
        digitalWrite(pin7segF  , LOW  );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      case 3:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , LOW  );
        digitalWrite(pin7segF  , LOW  );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      case 4:
        digitalWrite(pin7segA  , LOW  );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , LOW  );
        digitalWrite(pin7segE  , LOW  );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      case 5:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , LOW  );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , LOW  );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      case 6:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , LOW  );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , HIGH );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      case 7:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , LOW  );
        digitalWrite(pin7segE  , LOW  );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , LOW  );
        break;
    
      case 8:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , HIGH );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      case 9:
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , LOW  );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , HIGH );
        break;
    
      default :  //0
        digitalWrite(pin7segA  , HIGH );
        digitalWrite(pin7segB  , HIGH );
        digitalWrite(pin7segC  , HIGH );
        digitalWrite(pin7segD  , HIGH );
        digitalWrite(pin7segE  , HIGH );
        digitalWrite(pin7segF  , HIGH );
        digitalWrite(pin7segG  , LOW  );
        break;
    }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.