30 secs countdown that can be interrupted anytime?

here i declare the values:

    unsigned long prevMillis;
    int count = 30;

void setup here:

prevMillis = millis();  // get the time at the start

void loop here: i insert this code after a button is pressed..

    unsigned long curMillis = millis();
  //  if one second has passed
  if(curMillis - prevMillis >= 1000){
    count -= 1;
    Serial.println(count);
    lcd.setCursor(1, 3);
    lcd.print(count);
    if (count <= 9) {
    lcd.setCursor(1, 3);
    lcd.print("0");
    lcd.setCursor(2, 3);
    lcd.print(count);
    }
    prevMillis += 1000;
 
  }
  if(count == 0){
 
    Serial.println("Countdown ended");
    
  }
}

i am trying to display a 30 seconds countdown on a i2c lcd.. but the problem is the countdown stops at "29" after it started..

then once i press the button again to show the countdown again on the lcd.. it starts and stops at "28"
then "27" on another press.. then 26 on another press.. it does not perform a complete 30 seconds countdown as i wanted to..

i try to put the countdown code alone in a new sketch without the button code and other codes in my program.. and it work fine.. im guessing theres something in my program that preventing the countdown to continue..

my goal is to display a 30 seconds countdown on lcd together with an item/product that im selling.. on a button press.. and also i want a countdown that can be interrupted anytime by a button press.. so it dont have to finish the 30 secs countdown all the time. so the program can do other things..

  count -= 1;
  Serial.println(count);
  lcd.setCursor(1, 3);
  lcd.print(count);
  if (count <= 9)

Under what circumstances is count not going to be less than 9 ?

if (count == 0)

You never change the value of count so will it ever be zero ?

 Under what circumstances is count not going to be less than 9

when the count is <= 9 the space it occupy from the lcd becomes 1 digit.. it gives wrong values.. 9 becomes 90.... 8 becomes 80... 7 becomes 70.... thats why i re position the lcd.setCursor to coverup the zero..

 You never change the value of count so will it ever be zero ?

from what i understand the code deduct 1 second from 30.. and eventually the value will be "0"

Sorry, my mistake reading your code. Ignore my question

Sorry, my mistake reading your code. Ignore my question

thats alright

Please post a complete program that demonstrates the problem

Once this is all sorted, of course the next question will be "ok, how do I fit this in to the SD card code in the other thread"?

Good thing I had the foresight there to make that non-blocking so that millis() stuff can be slotted in. (To the extent that I even put a millis()-based proof-of-life led in there....)

But may I make a suggestion. Sure, diving into a project is a good way to learn sometimes, and the theories of adult ed are all to do with learning what you need, when you need.

But if you wish this:

projectdimpreza:
so the program can do other things..

... it behooves you to pause I think, and have a proper study of:

  • Robin2's thread on planning a program
  • UKHeliBob's thread on using millis()
  • Robin2's thread on doing many things at once

... before you paint yourself into a corner.

UKHeliBob's thread on using millis()

i read that before i post here.. i know the countdown alone is working.. because i compile it and upload it without my program sketch.. im just asking for suggestion what could be causing it to not work with my program..

im trying my best to understand what each function of programming does.. im not american so asides from me new to programming... theres a language barrier.. i can only digest so much.. but im willing to learn..

projectdimpreza:
im not american so ... theres a language barrier....

Nor am I, and I'm sure going by his name UKHeliBob isn't either; I was born about 50km from London.

So let's stick to English not American :wink:

I don't think you actually explained what this countdown is supposed to do in your main program, nor what's going wrong when you try to slot it in.

So let's stick to English not American :wink:

yea english, my bad :frowning:

I don't think you actually explained what this countdown is supposed to do in your main program, nor what's going wrong when you try to slot it in.

you asked for it, then ill give u explanation.
the purpose of the 30 secs countdown is to warn the user that the item they choose will be cleared on the lcd screen after 30 secs .but they can also manually clear the lcd screen on a button press.. so they dont have to wait for the 30 secs to end..

problem is.. the countdown starts at 29.. then stops there.. after testing it again.. with another button press.. it starts at 28 then stops at 28 also.. then i press again.. starts 27 then stop 27.. seems like it just counting down 1 then stops.. it doesnt do the complete 30 secs countdown..

Here is a Serial print version of a countdown timer with reset.

I have use the Bounce2 library to debounce the button which I found to be required to avoid interaction between the stop/start button presses. Sketch is written for INPUT_PULLUP on the button pin.

#include <Bounce2.h>
Bounce startButton = Bounce();

unsigned long startTime;
unsigned long currentTime;
unsigned long period = 1000;
const byte buttonPin = 4;
byte count = 30;
boolean timing = false;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.println("Countdown Timer Demo");

  startButton.attach(buttonPin);
  startButton.interval(25); // debounce interval in ms
}

void loop()
{
  currentTime = millis();
  startButton.update();

  if(startButton.fell() && ! timing)
  {
    startTime = currentTime;
    timing = true;
    Serial.println("Button Press Begin Countdown");
    Serial.println(count);
  }

 if (startButton.fell() && timing && millis()- startTime >=50) //50 ms to ensure a unique startButton.update() reading
  {
    timing = false;
    count = 30;
    Serial.println("Button Press Stop Countdown");
  }


  if (timing && (currentTime - startTime >= period))
  {
    startTime += period;
    count -= 1;
    Serial.println(count);
  }

  if (timing && count == 0)
  {
    Serial.println("TimeOut Stop Countdown");
    timing = false;
    count = 30;
  }
}
 void count30() {

    unsigned long curMillis = millis();
  //  if one second has passed
  if(curMillis - prevMillis >= 1000){
    count -= 1;
    Serial.println(count);
    lcd.setCursor(1, 3);
    lcd.print(count);
    if (count <= 9) {
    lcd.setCursor(1, 3);
    lcd.print("0");
    lcd.setCursor(2, 3);
    lcd.print(count);
    }
    prevMillis += 1000;
  }
  if(count == 0){
 
    Serial.println("Countdown ended");
    
  }
}

i assume that because my countdown code is a void it just run once.. it will only run again if its called again in the program.. which in this case the "void count30()" and thats why its just deducting only "1" from 30 everytime it executes..

        // buttonCount is the button state.. to let the program know how many time it has been press
        if (buttonCount == 0) { 
       // start is my variable for button
        if (start == LOW) { 
                open_1hr_counter();
                write_1hr_counter();
                get_1hr_code();
                showCode();  
                count30(); // this part is where the countdown starts displaying on lcd.

like what i said before.. the countdown start at 29.. then stop at 29 to... then start on 28 then stop on 28 on the 2nd call or second time i press the button.. then so on.. it doesnt complete the whole 30 secs countdown.. it just deducting 1 everytime the void count30() is called.

im thinking maybe a "for loop" could fix this? but im not sure if a for loop can be interrupted anytime incase the user decide to manually clear or stop the countdown on the lcd by a button press..

projectdimpreza:
the purpose of the 30 secs countdown is to warn the user that the item they choose will be cleared on the lcd screen after 30 secs .but they can also manually clear the lcd screen on a button press.. so they dont have to wait for the 30 secs to end..

What do you mean: " the item they choose"? Does that mean the item currently on the screen due to the button click that walks through the file line by line? And if so what does "will be cleared on the lcd screen after 30 secs" mean? The way your main program stands, to clear an item off the screen has no meaning: you either click to the next item (with a rewind from the end) or decide to print an item, which is also a rewind.

So right now I have no idea what this timer will do, back in the main program.

I wonder if you do?

So right now I have no idea what this timer will do, back in the main program.
I wonder if you do?

here you go again asking full details of what the program will do.. but when i answer.. you will came up with another question digging further the program.. i explained it many times.. i create this post to ask for suggestion and guidance..and i appreciate people whos willing to help..

let me tell you this.. maybe u know more in programming than me.. but it doesn't make u a better person or a higher kind than others.. your making it sound like im talking non sense at all.. well im not the kind thats going to be bullied..

the problem is clear.. 30 secs countdown stops and does not complete the countdown and thats it.

projectdimpreza:
let me tell you this.. maybe u know more in programming than me.. but it doesn't make u a better person or a higher kind than others.. your making it sound like im talking non sense at all.. well im not the kind thats going to be bullied..

Ok well you're welcome to the code I supplied in the other thread, that works according to what you eventually explained there. It took ages and loads of posts to get to that understanding, and I happily provided a full working solution for that requirement, once I understood it.

I'm trying to do the same here: what you have explained so far of this counter thing, while making sense on its own, does not seem to me to have a role in the main program in the other thread. If you could have explained exactly how it would integrate into that code, guess what, I'd happily work with you to do that, same as I did in the main thread.

If you think it's bullying for someone who's trying to help to get at the nitty-gritty of the requirement so that that requirement can be met, then you need to get your paranoia seen to.

I'm out: I hope you get it working, I really do, and if others are willing to work with you to help, good luck to them to.

But I'm done here, and in the other thread.