Simple Timer Guidance

Hello all,

This is my first time using an Arduino or doing any sort of programming. I have a project where I would like to press a button and turn on an LED for 80 seconds then immediately following I would like to blink another LED for 10 seconds with the process restarting itself unless it is paused by pressing a button. I have been looking at implementing millis() functions with buttons but I am a little overwhelmed. Any guidance would be appreciated.

You have to say what you mean by "blink" How many milliseconds ON and how many OFF?

So nothing happens when you turn on the Arduino. Push the button to get: First LED 80 seconds 0n Second LED 10 seconds 'blinking' (does the first LED turn off?) First LED 80 seconds ON Second LED 10 seconds 'blinking' . . . Until you push the same button again: light turns off and you go back to the beginning.

Start with this to get 2 LEDs to turn on in sequence after a button push. Then add code for the pause

// define all the variables
// define inputs, outputs in void setup()

// then loop will be like this - couple of timers, couple of flags to keep track of what's going on
void loop(){
currentTime = millis();  // capture current time - all time variables are unsigned  long
if ( (digitalRead(buttonPin) == LOW) && timer1Running == 0){ // button was pushed, wasn't running already
startTime1 = currentTime;
timer1Running = 1;
digitalWrite (LED1,  HIGH);
}

if (timer1Running == 1){
 if ( (currentTime - startTime1) >= 80000UL){  // time to stop 1st LED?
  timer1Running = 0;
digitalWrite(LED2, LOW);
timer2Running = 1;
startTime2 = currentTime;
digitalWrite (LED2, HIGH);
}
}

if (timer2Running == 1){
if ( (currentTime - startTime2)>=10000UL){
timer2Running = 0;
digitalWrite (LED2, LOW);
}
}

} // end loop

CrossRoads: Start with this to get 2 LEDs to turn on in sequence after a button push. Then add code for the pause

// define all the variables
// define inputs, outputs in void setup()

// then loop will be like this - couple of timers, couple of flags to keep track of what's going on void loop(){ currentTime = millis();  // capture current time - all time variables are unsigned  long if ( (digitalRead(buttonPin) == LOW) && timer1Running == 0){ // button was pushed, wasn't running already startTime1 = currentTime; timer1Running = 1; digitalWrite (LED1,  HIGH); }

if (timer1Running == 1){ if ( (currentTime - startTime1) >= 80000UL){  // time to stop 1st LED?  timer1Running = 0; digitalWrite(LED2, LOW); timer2Running = 1; startTime2 = currentTime; digitalWrite (LED2, HIGH); } }

if (timer2Running == 1){ if ( (currentTime - startTime2)>=10000UL){ timer2Running = 0; digitalWrite (LED2, LOW); } }

} // end loop

Thanks for the quick response. I am still trying to get a handle on variables and what not. I put together this code and the LED's simply turn on and don't turn off. I am sure I botched something with the time variables since I don't know what I am doing.

  const int buttonPin = 2;
const int LED1 = 13;
const int LED2 = 12;
unsigned long currentTime = 0;
unsigned long timer1Running = 0;
unsigned long timer2Running = 0;
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
// define inputs, outputs in void setup()
void setup() {
  // initialize the LED pin as an output:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}
// then loop will be like this - couple of timers, couple of flags to keep track of what's going on
void loop(){
currentTime = millis();  // capture current time - all time variables are unsigned  long
if ( (digitalRead(buttonPin) == LOW) && timer1Running == 0){ // button was pushed, wasn't running already
startTime1 = currentTime;
timer1Running = 1;
digitalWrite (LED1,  HIGH);
}

if (timer1Running == 1){
 if ( (currentTime - startTime1) >= 80000UL){  // time to stop 1st LED?
  timer1Running = 0;
digitalWrite(LED2, LOW);
timer2Running = 1;
startTime2 = currentTime;
digitalWrite (LED2, HIGH);
}
}

if (timer2Running == 1){
if ( (currentTime - startTime2)>=10000UL){
timer2Running = 0;
digitalWrite (LED2, LOW);
}
}

} // end loop
}

As for the blinking, it is not super important about the type of blinking. Half second on-half second off would be fine but the blinking isn't even that essential, it would just be nice.

Hello all,

This is my first time using an Arduino or doing any sort of programming. I have a project where I would like to press a button and turn on an LED for 80 seconds then immediately following I would like to blink another LED every half second for 10 seconds with the process restarting itself unless it is paused by pressing the same button. I have been looking at implementing millis() functions with buttons but I am a little overwhelmed. I have gotten some help with the code but I think I am messing up the variables. Here is what I have so far

 const int buttonPin = 2;
const int LED1 = 13;
const int LED2 = 12;
unsigned long currentTime = 0;
unsigned long timer1Running = 0;
unsigned long timer2Running = 0;
unsigned long startTime1 = 0;
unsigned long startTime2 = 80000;
// define inputs, outputs in void setup()
void setup() {
  // initialize the LED pin as an output:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}
// then loop will be like this - couple of timers, couple of flags to keep track of what's going on
void loop(){
currentTime = millis();  // capture current time - all time variables are unsigned  long
if ( (digitalRead(buttonPin) == LOW) && timer1Running == 0){ // button was pushed, wasn't running already
startTime1 = currentTime;
timer1Running = 1;
digitalWrite (LED1,  HIGH);
}

if (timer1Running == 1){
 if ( (currentTime - startTime1) >= 80000UL){  // time to stop 1st LED?
  timer1Running = 0;
digitalWrite(LED2, LOW);
timer2Running = 1;
startTime2 = currentTime;
digitalWrite (LED2, HIGH);
}
}

if (timer2Running == 1){
if ( (currentTime - startTime2)>=10000UL){
timer2Running = 0;
digitalWrite (LED2, LOW);
}
}

} // end loop

then immediately following

Immediately following what? Turning on the LED? Turning off the LED after the 80 second pause?

I have gotten some help with the code but I think I am messing up the variables.

It's the formatting that is messed up. Use Tools + Auto Format before posting code. Make it readable.

That code does something. What does it do? How does that differ from what you want it to do?

Here is what I have so far

Not true, here is what someone gave you to start with so far.

pinMode(buttonPin, INPUT);

Make this pinMode(buttonPin, INPUT_PULLUP); // internal pullup

or pinMode(buttonPin, INPUT); digitalWritebuttonPin, HIGH); // enable internal pullup

and wire a switch from pin to gnd. Do you have that wired up, and is it working?

Pardon my syntax, you are correct, that is what I was given to start. After some toying around and some outside help I have arrived at this:

 const int buttonPin = 2;
const int LED1 = 13;
const int LED2 = 12;
unsigned long currentTime = 0;
boolean timer1Running = false;
boolean timer2Running = false;
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
// define inputs, outputs in void setup()
void setup() {
  // initialize the LED pin as an output:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}
// then loop will be like this - couple of timers, couple of flags to keep track of what's going on
void loop(){
  
  
currentTime = millis();  // capture current time - all time variables are unsigned  long

if ( (digitalRead(buttonPin) == HIGH))  // button was pushed, wasn't running already
{ 
  //reset
  timer1Running = false;
  timer2Running = false;
  digitalWrite (LED2, LOW);
  digitalWrite (LED1, LOW);
  
  //start
  startTime1 = currentTime;
  timer1Running = true;
  digitalWrite (LED1,  HIGH);
}

if (timer1Running)
{
   if ( (currentTime - startTime1) >= 80000)
   {  // time to stop 1st LED?
    timer1Running = false;
    digitalWrite(LED1, LOW);
    timer2Running = true;
    startTime2 = currentTime;
    digitalWrite (LED2, HIGH);
  }
} else if (timer2Running)
{
  if ( (currentTime - startTime2)>=10000)
  {
    timer2Running = false;
    digitalWrite (LED2, LOW);
    
    
    startTime1 = currentTime;
    timer1Running = true;
    digitalWrite (LED1,  HIGH);
  }
}
else{}

} // end loop

it will work for now. Thanks for your help all of you.