Newbie just tsrating, need help with a sketch

At first glance, you would think that the 5 second timer, once it reaches 5000 and with no flag, the if() would always evaluate.

However, since previousMillis is shared with the 80ms TIMER, where it is constantly reset, the 5 second TIMER will not evaluate.


It is always best to disable code that should not run, which adding a flag can accomplishes.


IMO, always use separate variables for separate TIMERS not like what was done above in the last sketch offered you. (one exception might be if a common TIMER is used in a State Machine).




Software is NEVER finished. Below are a few changes to the code that might be a better way of doing things.

https://forum.arduino.cc/t/newbie-just-tsrating-need-help-with-a-sketch/908165?u=larryd

#define LEDon                 HIGH
#define LEDoff                LOW

#define ENABLED               true
#define DISABLED              false

boolean _5secFlag           = ENABLED; 
boolean _80msFlag           = DISABLED;
boolean _30secFlag          = DISABLED;

boolean powerUpFlag         = ENABLED;  //At power up we want want to start the LED sequence immediately

const byte maximum          = 5;

const byte LedPins[maximum] = {8, 9, 10, 11, 12};

byte index;                             //used to access the element in our array

char updown                 = 1;        //+1 or -1 for going left and right

//timing stuff
unsigned long _5secMillis;
unsigned long _30secMillis;
unsigned long _80msMillis;
unsigned long currentMillis;


//*******************************************************************************
void setup()
{
  for (byte x = 0; x < maximum; x++)
  {
    pinMode(LedPins[x], OUTPUT);

    digitalWrite(LedPins[x], LEDoff);
  }

} //END of setup()


//*******************************************************************************
void loop ()
{
  //save the current time 
  currentMillis = millis();

  //******************************                        5  s e c o n d   T I M E R
  //have we just powered on the Arduino OR if the 5 second TIMER is enabled, has it expired ?
  if (powerUpFlag == ENABLED || (_5secFlag == ENABLED && currentMillis - _5secMillis >= 5000))
  {
    //go to normal running mode
    powerUpFlag = DISABLED;

    _5secFlag = DISABLED;  
    _80msFlag = ENABLED;
    _30secFlag = ENABLED;

    //restart the 80ms TIMER
    _80msMillis = millis();

    //restart the 30 second TIMER
    _30secMillis = millis();
  }

  //******************************                        8 0  m s   T I M E R
  //if the 80ms TIMER is enabled, has it expired ?
  if (_80msFlag == ENABLED && currentMillis - _80msMillis >= 80)
  {
    //restart the 80ms TIMER
    _80msMillis = currentMillis;

    digitalWrite(LedPins[index], LEDoff);    
    index += updown;

    digitalWrite(LedPins[index], LEDon);     

    //have we reached the limit ?
    if ( index <= 0 || index >= 4)
    {
      updown = -updown;
    }
  }

  //******************************                        3 0  s e c o n d   T I M E R
  //if the 30 second TIMER is enabled, has it expired ?
  if (_30secFlag == ENABLED && currentMillis - _30secMillis > 30000)
  {
    //LEDs OFF
    for (byte x = 0; x < maximum; x++)
    {
      digitalWrite(LedPins[x], LEDoff);
    }

    //finished with this sequence, we are now starting over
    _5secFlag = ENABLED;
    //restart the 5 second TIMER
    _5secMillis = millis();

    _80msFlag = DISABLED;
    _30secFlag = DISABLED;
  }

} //END of loop()