Accelerating a timer for a fixed amount of seconds

Hi everyone!

I'm going to do my best to explain this through text...

I have a timer that I want to count down 1 second every second (regular 1x speed). But, if a logical boolean is true (button pressed in this case), I want to have time go 2 seconds every second (2x speed, or twice as fast) for a period of 15 real-world seconds.

Does anyone have any experience or advice that I could get to accomplish this? Thanks!

How precisely do you want to measure that time?
When you talk about timer what you really mean?

jojoguy10:
Hi everyone!

I'm going to do my best to explain this through text...

I have a timer that I want to count down 1 second every second (regular 1x speed). But, if a logical boolean is true (button pressed in this case), I want to have time go 2 seconds every second (2x speed, or twice as fast) for a period of 15 real-world seconds.

Does anyone have any experience or advice that I could get to accomplish this? Thanks!

show us your present countdown code.

I did the following code based in the examples BlinkWithouDelay and Debounce:

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin
const int buttonPin = 5;    // the number of the pushbutton pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// Generally, you shuould use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers


bool myBool = false;

unsigned long mySeconds = 0;

int timeBool = 0;

const int MAX_BOOL_TIME = 15;


bool readMyButton(int thisButtonPin) {

  // read the state of the switch into a local variable:
  int reading = digitalRead(thisButtonPin);
 
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // button is only pressed if the new button state is LOW
       if (buttonState == LOW) {
        Serial.println(">");
        return true;
      }
    }
  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;   
  
  return false;
}

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
   
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
}

void loop()
{
   if (readMyButton(buttonPin) == true ) {
      myBool = true;
      timeBool = MAX_BOOL_TIME;
   }

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    if ( (myBool == true && timeBool%2!=0) || myBool == false) {
       mySeconds++;
       
       // if the LED is off turn it on and vice-versa:
       if (ledState == LOW)
         ledState = HIGH;
       else
         ledState = LOW;
   
       // set the LED with the ledState of the variable:
       digitalWrite(ledPin, ledState);
    }
    
    if (myBool == true) {
       if (--timeBool==0) {
         myBool = false;       
       }
    }    
    
    Serial.println(mySeconds);
    ///////////////////////////////////////////////////////////////
  }
}

This code is blinking the LED of the board 1/2Hz. If you press the button on digital input 5, you change the frequency of blinking to 1/4Hz during 15 seconds. Is is counting up (you didn't say from where you need to count down), and is printing the result of this count to the serial line.

The only part that I really add to the examples was:

    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    if ( (myBool == true && timeBool%2!=0) || myBool == false) {
       mySeconds++;
       
       // if the LED is off turn it on and vice-versa:
       if (ledState == LOW)
         ledState = HIGH;
       else
         ledState = LOW;
   
       // set the LED with the ledState of the variable:
       digitalWrite(ledPin, ledState);
    }
    
    if (myBool == true) {
       if (--timeBool==0) {
         myBool = false;       
       }
    }    
    
    Serial.println(mySeconds);
    ///////////////////////////////////////////////////////////////

Do you understand this an can change it for what you really want?

luisilva:
How precisely do you want to measure that time?
When you talk about timer what you really mean?

I'm just counting down in 1 second increments.

By a "timer" I mean, setting it to (for example) 15 minutes, and have it countdown to 0 seconds in 1 second increments.

BulldogLowell:
show us your present countdown code.

I don't have it written yet. That's why I came here...

luisilva:
I did the following code based in the examples BlinkWithouDelay and Debounce:

// constants won't change. Used here to set a pin number :

const int ledPin =  13;      // the number of the LED pin
const int buttonPin = 5;    // the number of the pushbutton pin

// Variables will change :
int ledState = LOW;            // ledState used to set the LED
int buttonState;            // the current reading from the input pin
int lastButtonState = LOW;  // the previous reading from the input pin

// Generally, you shuould use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;          // interval at which to blink (milliseconds)

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

bool myBool = false;

unsigned long mySeconds = 0;

int timeBool = 0;

const int MAX_BOOL_TIME = 15;

bool readMyButton(int thisButtonPin) {

// read the state of the switch into a local variable:
  int reading = digitalRead(thisButtonPin);

// check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

// if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

// button is only pressed if the new button state is LOW
      if (buttonState == LOW) {
        Serial.println(">");
        return true;
      }
    }
  }

// save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading; 
 
  return false;
}

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
 
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
 
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
}

void loop()
{
  if (readMyButton(buttonPin) == true ) {
      myBool = true;
      timeBool = MAX_BOOL_TIME;
  }

// check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis; 
   
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    if ( (myBool == true && timeBool%2!=0) || myBool == false) {
      mySeconds++;
     
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
 
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
   
    if (myBool == true) {
      if (--timeBool==0) {
        myBool = false;     
      }
    }   
   
    Serial.println(mySeconds);
    ///////////////////////////////////////////////////////////////
  }
}




This code is blinking the LED of the board 1/2Hz. If you press the button on digital input 5, you change the frequency of blinking to 1/4Hz during 15 seconds. Is is counting up (you didn't say from where you need to count down), and is printing the result of this count to the serial line.

The only part that I really add to the examples was:


//////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    if ( (myBool == true && timeBool%2!=0) || myBool == false) {
      mySeconds++;
     
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
 
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
   
    if (myBool == true) {
      if (--timeBool==0) {
        myBool = false;     
      }
    }   
   
    Serial.println(mySeconds);
    ///////////////////////////////////////////////////////////////




Do you understand this an can change it for what you really want?

Thanks for the help! I will study the code and reply back with any other questions.