Need help with Time in Arduino

Hello Forum,

I'm in need of help from more experienced Arduino Programmers. I can't seem to figure out how to use Time in the arduino code so that after a specific amount of seconds elapsed, it will perform an action. If anyone could explain or help me, I would be very appreciate.

thank you

if ( (trigger_event == 1) && (timeRunning == 0) ){
// trigger_event ==1 such as "digitalRead(pinX) == 0" from a button press
startTime = millis();
timeRunning = 1;
}
if ( (timeRunning ==1) && ((millis() - startTime) >= eventDuration) ){
//perform action
timeRunning = 0;
}

all time related variable are unsigned long

thank you so much will keep you updated with the code

CrossRoads: if ( (trigger_event == 1) && (timeRunning == 0) ){ // trigger_event ==1 such as "digitalRead(pinX) == 0" from a button press startTime = millis(); timeRunning = 1; } if ( (timeRunning ==1) && ((millis() - startTime) >= eventDuration) ){ //perform action timeRunning = 0; }

all time related variable are unsigned long

How would I incorporate this into my code so that after 50 or some minutes it would turn on just one led, and so it would restart after?

You don't show your code so how can we possibly say something that refers to your code? CrossRoads's code has a comment stating 'perform action'. Replace that with whatever you want to do.

sterretje: You don't show your code so how can we possibly say something that refers to your code? CrossRoads's code has a comment stating 'perform action'. Replace that with whatever you want to do.

const int RED = 13;
const int YELLOW = 12;
const int GREEN = 11;

unsigned long startTime = 0;

void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);



}

void loop() {




digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH); 
digitalWrite(YELLOW, HIGH);
delay(5000);
digitalWrite(RED, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, LOW);
delay(1000);
                    

}

This is just a very very early iteration of it, but what if you can't see what I'm trying to do is, I'm trying to have all the LEDS (GREEN, YELLOW, AND RED being LEDS) to be on at first. As time goes on I want the green to turn off, then yellow, and then finally the red. I would appreciate help being as I'm still very new to ardiunos. I've been stubborn trying to figure it out myself but I just can't figure it out.

if ( (trigger_event == 1) && (timeRunning == 0) ){

// trigger_event ==1 such as "digitalRead(pinX) == 0" from a button press

startTime = millis();

timeRunning = 1;

//turn on 3 LEDs

}

if ( (timeRunning ==1) && ((millis() - startTime) >= 5000UL) ){

//perform action

// turn off LED 1

}

if ( (timeRunning ==1) && ((millis() - startTime) >= 10000UL) ){

//perform action

// turn off LED 2

}

if ( (timeRunning ==1) && ((millis() - startTime) >= 15000UL) ){

//perform action

// turn off LED 3

timeRunning = 0;

}

Starting to see the pattern here?

You have a couple of steps (all for a given duration). 1) All on 2) Yellow and red on 3) Red on 4) All off

You can keep a counter to indicate which step needs to be executed.

// global variable to remember the step to execute
int stepcounter = 0;

void loop()
{
  if(stepcounter == 0)
  {
    digitalWrite(PINGREEN, HIGH);
    digitalWrite(PINYELLOW, HIGH);
    digitalWrite(PINRED, HIGH);
    delay(5000);
    stepcounter=1;
  }
  else if(stepcounter==1)
  {
    digitalWrite(PINGREEN, LOW);
    delay(5000);
    stepcounter=2;
  }
  else if(stepcounter==2)
  {
    digitalWrite(PINYELLOW, LOW);
    delay(5000);
    stepcounter=3;
  }
  else if(stepcounter==3)
  {
    digitalWrite(PINRED, LOW);
    delay(5000);
    stepcounter=0;
  }
}

Instead of using if / else if / ... you can use a switch/case statement

In the next step, you can write functions that perform the actions for each step; shown for all leds on, you can implement the other functions (GreenOff, YellowOff and RedOff).

void AllOn()
{
  // green on
  digitalWrite(PINGREEN, HIGH);
  // yellow on
  digitalWrite(PINYELLOW, HIGH);
  // red on
  digitalWrite(PINRED, HIGH);
  // wait a while
  delay(5000);
  // indicate the next step to do
  stepcounter=1;
}

And call them from the loop().

void loop()
{
  if(stepcounter == 0)
  {
    AllOn();
  }
  else if(stepcounter==1)
  {
    GreenOff();
  }
  else if(stepcounter==2)
  {
    YellowOff();
  }
  else if(stepcounter==3)
  {
    RedOff();
  }
}

The disadvantage of delay() that during that period nothing can be done by the processor. If you want e.g. to force a stop at the moment that a button is pressed, you have to wait for at least the delay period before the button will be read.

To solve that, you can use CrossRoads' code as a start. My implementation would look like

void AllOn(unsigned long duration)
{
  // rememeber start time of delay
  static unsigned long starttime = 0;
  if(starttime == 0)
  {
    // green on
    digitalWrite(PINGREEN, HIGH);
    // yellow on
    digitalWrite(PINYELLOW, HIGH);
    // red on
    digitalWrite(PINRED, HIGH);
    // set the start time
    starttime = millis();
  }
  // if duration milliseconds has passed
  if(millis -starttime >= duration)
  {
    // reset the starttime
    starttime = 0;
    // indicate the next step to execute
    stepcounter=1;
  }
}

For the above code, the loop() does not change. Below a full implementation that also demonstrates that the code does not block. A button connected between pin 4 and ground; if it's pressed, a message is displayed continuously on the SerialMonitor while the LEDs keep on changing.

#define PINRED 9
#define PINYELLOW 11
#define PINGREEN 10

#define PINBUTTON 4

void setup()
{
  Serial.begin(9600);
  pinMode(PINGREEN, OUTPUT);
  pinMode(PINYELLOW, OUTPUT);
  pinMode(PINRED, OUTPUT);
  pinMode(PINBUTTON, INPUT_PULLUP);

  delay(1); // Arduino is too fast 
}

// global variable to remember the step to execute
int stepcounter = 0;

void loop()
{
  // to demonstrate that the code does not block
  if (digitalRead(PINBUTTON) == LOW)
    Serial.println("Button pressed");

  switch (stepcounter)
  {
    case 0:
      AllOn(2000UL);
      break;
    case 1:
      GreenOff(2000UL);
      break;
    case 2:
      YellowOff(2000UL);
      break;
    case 3:
      RedOff(2000UL);
      break;
    default:
      Serial.println("Unsupported step number; reset to 0");
      stepcounter = 0;
      break;
  }
}

/*
  AllOn()
  switch all leds on; uses non-blocking delay of duration milliseconds
*/
void AllOn(unsigned long duration)
{
  // rememeber start time of delay
  static unsigned long starttime = 0;
  if (starttime == 0)
  {
    // green on
    digitalWrite(PINGREEN, HIGH);
    // yellow on
    digitalWrite(PINYELLOW, HIGH);
    // red on
    digitalWrite(PINRED, HIGH);
    // set the start time
    starttime = millis();
    // debug message
    Serial.println("DEBUG: all leds on, delay started");
  }
  // if duration milliseconds has passed
  if (millis() - starttime >= duration)
  {
    // reset the starttime
    starttime = 0;
    // indicate the next step to execute
    stepcounter = 1;
    // debug message
    Serial.println("DEBUG: all leds on, delay finished");
  }
}


/*
  GreenOff
  switch green led off; uses non-blocking delay of duration milliseconds
*/
void GreenOff(unsigned long duration)
{
  // rememeber start time of delay
  static unsigned long starttime = 0;
  if (starttime == 0)
  {
    // green off
    digitalWrite(PINGREEN, LOW);
    // set the start time
    starttime = millis();
    // debug message
    Serial.println("DEBUG: green led off, delay started");
  }
  // if duration milliseconds has passed
  if (millis() - starttime >= duration)
  {
    // reset the starttime
    starttime = 0;
    // indicate the next step to execute
    stepcounter = 2;
    // debug message
    Serial.println("DEBUG: green led off, delay finished");
  }
}

/*
  YellowOff
  switch yellow led off; uses non-blocking delay of duration milliseconds
*/
void YellowOff(unsigned long duration)
{
  // rememeber start time of delay
  static unsigned long starttime = 0;
  if (starttime == 0)
  {
    // yellow off
    digitalWrite(PINYELLOW, LOW);
    // set the start time
    starttime = millis();
    // debug message
    Serial.println("DEBUG: yellow led off, delay started");
  }
  // if duration milliseconds has passed
  if (millis() - starttime >= duration)
  {
    // reset the starttime
    starttime = 0;
    // indicate the next step to execute
    stepcounter = 3;
    // debug message
    Serial.println("DEBUG: yellow led off, delay finished");
  }
}

/*
  RedOff
  switch red led off; uses non-blocking delay of duration milliseconds
*/
void RedOff(unsigned long duration)
{
  // rememeber start time of delay
  static unsigned long starttime = 0;
  if (starttime == 0)
  {
    // red off
    digitalWrite(PINRED, LOW);
    // set the start time
    starttime = millis();
    // debug message
    Serial.println("DEBUG: red led off, delay started");
  }
  // if duration milliseconds has passed
  if (millis() - starttime >= duration)
  {
    // reset the starttime
    starttime = 0;
    // indicate the next step to execute
    stepcounter = 0;
    // debug message
    Serial.println("DEBUG: red led off, delay finished");
  }
}

This timer library might also be useful - https://github.com/prampec/arduino-softtimer