How to pause loop?

Hello,

I’m working on countdown timer.

Timer should work while ‘‘button1’’ is HIGH. But somehow the loop runs even if ‘‘button1’’ is LOW

Any sugestions where I’m making mistakes with code?

Thanks in advance!

Code is added bellow.

#define numberOfSeconds(_time_) ((_time_ / 1000) % 60)  
#define numberOfMinutes(_time_) (((_time_ / 1000) / 60) % 60) 

// INCLUDES
// https://github.com/avishorp/TM1637
#include <TM1637Display.h>

// CONSTANTS
const uint8_t OFF[] = {0, 0, 0, 0};
// In this library, the byte order is .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01011111, B01101110};


byte oneChanOut = 5; // Channel output pin

int brightness1 = 0; // Initial length of tube 1
int fadeAmount1 = 1; // Increment between refreshes
int buttonPin = 9;
int Button1;

// GLOBALS
// Create a display object, specifying parameters (Clock pin, Data pin)
TM1637Display display(2, 3);

// 1000ms in 1sec, 60secs in 1min, 60mins in 1hr. So, 1000x60x60 = 3600000ms = 1hr
unsigned long timeLimit = 3600000;





void setup(){
  Serial.begin(9600);
  // Set brightness
  display.setBrightness(0x0c);
  // Clear the display
  display.setSegments(OFF);

  pinMode(buttonPin, INPUT);
}
void loop () {



int button1 = digitalRead(buttonPin);
while (button1 == HIGH){
  countdown();


}

if (button1 == LOW){  

  int seconds = numberOfSeconds(timeLimit);
    int minutes = numberOfMinutes(timeLimit); 
  
    // This displays the seconds in the last two places
    display.showNumberDecEx(seconds, 0, true, 2, 2);
    // Display the minutes in the first two places, with colon
    display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);


}

}


void countdown() {




  // Calculate the time remaining 
  unsigned long timeRemaining = timeLimit - millis();

 // get the button  state on the input

  while(timeRemaining > 0) {
    // To display the countdown in mm:ss format, separate the parts
    int seconds = numberOfSeconds(timeRemaining);
    int minutes = numberOfMinutes(timeRemaining); 
  
    // This displays the seconds in the last two places
    display.showNumberDecEx(seconds, 0, true, 2, 2);
    // Display the minutes in the first two places, with colon
    display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);

    // Update the time remaining
    if(millis() < timeLimit)
    timeRemaining = timeLimit - millis();

    fadeAmount1 = ((timeLimit - timeRemaining)/140062);
   brightness1 = ((timeLimit - timeRemaining)/14062);
     analogWrite(oneChanOut, brightness1);
  }

}
void displayText() {
  display.setSegments(PLAY);
  delay(2000);
}
1 Like

I think you wil be stuck in that loop if button1 is read only once and it is high

Try this

while ( (button1 = digitalRead(buttonPin))== HIGH)
         countdown();

Have you tried a basic test sketch to verify that button1 hardware works?

Get rid of all those random blank lines and reformat using ctrl-T! It's horrible to read.

Normally the pins internal pull up resistor enabled and when you push the switch it grounds the input making
it a LOW.

How do you have the switch configured ?

You should not try to pause loop(). Instead you should write your program so that loop() can continue without affecting the position your code has reached.

...R