Timer Countdown Programming

Hello all,

I’ve got a simple (for someone else) logic problem. I’m using the large seven segment display driver to drive 3 digits as a countdown timer. After trial and error, it works. Now I’m trying to complicate things, and have the time countdown faster by a factor of 2 when a button is pressed, and normal when not pressed. I’ve scoured the internet looking for some code to modify for my purpose, and haven’t found what I’m looking for.

I’m fairly new to programming, and any assistance you can give would be greatly appreciated.

Thanks in advance!

How have you currently programmed your timing? Are you using an Arduino and “millis()”? You will have some code that causes your program to wait, since it can count down a lot faster than it currently does - so in order to double the speed you would need to halve the wait times.

Thanks SV-Zanshin,

I can’t believe I left out that I am using an Arduino. My apologies. I am also using ‘millis()’ as well.

I do understand that part, but feel there is a bit more to it. In a 5 minute countdown, I need the timer to count faster/normal depending on the status of a button, which can change repeatedly before my maximum time constant is exceeded. Which must mean I need to keep track of the current button status, the last button status, the last time it changed status, accumulated time in each status etc.

My current working code has a set ‘MaxTime’, my variable that captures when it all begins ‘StartTime’(not shown below), my variable ‘RunTime’ that is converted to a ‘number’ for posting to my digits using my ‘showTime’ function. When I post the ‘number’ I break the value of seconds into ‘minutes’, ‘secondstens’, and ‘secondsones’. That all works fine. Somewhere in this small three lines of code, I need to change

Runtime = millis() - StartTime;

to

Runtime = millis() - StartTime - TimeButtonClosed*2;

My current code:

Runtime = millis() - StartTime;

number = (MaxTime - Runtime) / 1000;

showTimer(number);

Thanks again for any help on this.

I think I might have figured it out, but I can’t test until later. Does this make sense to anyone else? Is there a better way?

  if (Runtime <= MaxTime)
  {
    if (ButtonStatus != LastButtonStatus)                    //if status has changed
    {
      LastTimePeriod = millis() - StateChange;            //length of the last period of time
      if (ButtonStatus != 0)                                                     //was closed, now open
      {
        TotalOnTime = TotalOnTime + LastTimePeriod;    
      }
      if (ButtonStatus == 0)                                                    //was open, now closed
      {
        TotalOffTime = TotalOffTime + LastTimePeriod; 
      }
      StateChange = millis();                                                 //saves the last change time
      LastButtonStatus = ButtonStatus;                          //saves the last change
    }
    if (ButtonStatus != 0)                                                       //if the button is open
    {
      OffTime = millis() - StateChange;                              //this counts the off time, this time period
    }
    if (ButtonStatus == 0)                                                        //if the button is closed
    {
      OnTime = millis() - StateChange;                                //this counts the on time, this time period
    }
    Runtime = TotalOnTime + OnTime + ((TotalOffTime + OffTime)*2);
    number = (MaxTime - Runtime) / 1000;
    showTimer(number);
  }