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);
}
}