Multiple loop operation question

I am having an issue running multiple loops.
Each loop, when run individually will function correctly but when the loops are run together, only one loop will work.
I tested the operation of each loop by using comments (//) in front of each loop.

Test one
void loop(void) {
// loop1();
loop2(); (//This loop worked)
}

Test two
void loop(void) {
loop1(); (//This loop worked)
//loop2();
}

Any help would be greatly appreciated.
Brian Krupicka

My Code:

/*
Fire_Truck_Flasher_without_delay.ino
BMK
9-2-2021
*/

// constants won't change. Used here to set a pin number:
const int ledPin3 = A0;// the number of the LED pin
const int ledPin4 = A1;// the number of the LED pin
const int ledPin5 = A2;// the number of the LED pin

// Variables will change:
int ledState3 = LOW; // ledState used to set the LED
int ledState4 = HIGH;
int ledState5 = LOW;

// Generally, you should 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

void setup() {
// set the digital pin as output:
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
}

void loop() {

loop1();
loop2();
}
void loop1() {

// constants won't change:
const long interval = 250; // interval at which to blink (milliseconds)
// here is where you'd put code that needs to be running all the 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 the LED is off turn it on and vice-versa:
if (ledState3 == LOW) {
  ledState3 = HIGH;
  ledState4 = LOW;
} else {
  ledState3 = LOW;
  ledState4 = HIGH;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin3, ledState3);
digitalWrite(ledPin4, ledState4);

}
}

void loop2() {
// constants won't change:
const long interval = 500; // interval at which to blink (milliseconds)
// here is where you'd put code that needs to be running all the 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 the LED is off turn it on and vice-versa:
if (ledState5 == LOW) {
  ledState5 = HIGH;

} else {
  ledState5 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin5, ledState5);

}
}

The most obvious problem is that you only have one previousMillis variable

Please use Code Tags </> to present code.

Do you realize that both loops use the same previousMillis?

Hello
Either ou may specify a variable previousMillis for each loop,
or declare this variable as static in each loop.

Using 'static' rather than global variables to keep the two timers separate:

/*
  Fire_Truck_Flasher_without_delay.ino
  BMK
  9-2-2021
*/

// constants won't change. Used here to set a pin number:
const int ledPin3 = A0;// the number of the LED pin
const int ledPin4 = A1;// the number of the LED pin
const int ledPin5 = A2;// the number of the LED pin

// Variables will change:
int ledState3 = LOW; // ledState used to set the LED
int ledState4 = HIGH;
int ledState5 = LOW;

void setup()
{
  // set the digital pin as output:
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
}

void loop()
{

  loop1();
  loop2();
}

void loop1()
{
  unsigned long currentMillis = millis();
  static unsigned long previousMillis = 0;
  
  // constants won't change:
  const long interval = 250; // interval at which to blink (milliseconds)
  // here is where you'd put code that needs to be running all the time.

  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState3 == LOW)
    {
      ledState3 = HIGH;
      ledState4 = LOW;
    }
    else
    {
      ledState3 = LOW;
      ledState4 = HIGH;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledState3);
    digitalWrite(ledPin4, ledState4);
  }
}

void loop2()
{
  unsigned long currentMillis = millis();
  static unsigned long previousMillis = 0;

  // constants won't change:
  const long interval = 500; // interval at which to blink (milliseconds)
  // here is where you'd put code that needs to be running all the time.

  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState5 == LOW)
    {
      ledState5 = HIGH;
    }
    else
    {
      ledState5 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin5, ledState5);
  }
}
2 Likes

This should give a very similar effect with less code:

// constants won't change. Used here to set a pin number:
const int led3Pin = A0;
const int led4Pin = A1;
const int led5Pin = A2;

void setup()
{
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long previousMillis = 0;

  unsigned elapsedTime; // Can hold up to 65.535 seconds
  const unsigned fullCycleInterval = 1000; // two* 500, four * 250

  elapsedTime = currentMillis - previousMillis;

  if (elapsedTime >= fullCycleInterval)
  {
    previousMillis = currentMillis;
    elapsedTime = 0;
  }

  // "& 1" means "is an odd number"
  digitalWrite(led3Pin, (elapsedTime / 250) & 1);
  digitalWrite(led4Pin, !((elapsedTime / 250) & 1)); 
  digitalWrite(led5Pin, (elapsedTime / 500) & 1);
}

Thank to all that replied
Thank you John wasser for the update on the sketch. It work great!!!!
Brian Krupicka

Thank you for the extra step of reducing the code.

Brian K

John

Attached is a video of the completed project you assisted with.

It is the first of three fire trucks which will be displayed on my Model Railroad.

Thank you for your expertise.

Brian Krupicka

(Attachment IMG_0156_MOV(1).mp4 is missing)

https://youtu.be/OwzFUnpIIeM

Thank you all for your comments.
I took a little from each response and all is working
THANK YOU
Brian K