The root of this behavior is that when the program runs delay() function - nothing else could run (digitalwrites, checks, conditions, etc.) until the delay() time is passed.
The effective solution for this is avioding the delay() function, and getting the same results (without the side-effect) using timers (like millis()) or other ways.
Googling "How to avoid delay()" will give you lot of information about how-to do it, one example can be found here: Arduino Playground - How and Why to avoid delay() function.
Good luck with that, and enjoy your programming experience!
Answer from Offer on Stack Exchangeled - How can an Arduino do multiple actions in parallel without interfering with each other? - Arduino Stack Exchange
How to call multiples functions within the loop{ } and have them execute in parallel
How to run multiple for loop parallelly in arduino?
Parallel I/O
Videos
The root of this behavior is that when the program runs delay() function - nothing else could run (digitalwrites, checks, conditions, etc.) until the delay() time is passed.
The effective solution for this is avioding the delay() function, and getting the same results (without the side-effect) using timers (like millis()) or other ways.
Googling "How to avoid delay()" will give you lot of information about how-to do it, one example can be found here: Arduino Playground - How and Why to avoid delay() function.
Good luck with that, and enjoy your programming experience!
we found some other code that helped us avoid the delay() functions. Now we are able to use the timestamp "Millis" to run our blinking lights AND have another if statement running the photocell & third LED.
Making progress! Thanks for the suggestions! Here is the code and it works great to turn the LED on and off based on the photocell, and have the other LED's flashing on their own.
const int photoCell =0;
const int ledPin = 13;
const int led2Pin = 12;
const int led3Pin = 11;
int lightCal;
int lightVal;
int ledState = LOW;
int led2State = HIGH;
long previousMillis = 0;
long interval = 250;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
lightCal=analogRead(photoCell);
}
void loop() {
unsigned long currentMillis = millis();
lightVal = analogRead(photoCell);
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
if (ledState == LOW)
led2State = HIGH;
else
led2State = LOW;
digitalWrite(ledPin, ledState);
digitalWrite(led2Pin, led2State);
}
if (lightVal < lightCal - 50)
{
digitalWrite(led3Pin, HIGH);
}
else {
digitalWrite(led3Pin, LOW);
}
}