I am trying my hand at my very first Arduino program. It is meant to be a firefly simulator but I am having the following problem:
I want to have 4 sets of 2 lights each on their own blink loop. However I want them to blink concurrently to each other instead of each set of 2 having to wait until the previous 2 complete their loop.
Do I need multi-threading for this? Can I do this with Arduino? Any other suggestions?
Code below (I apologize in advance for messy formatting):
int ledPin[] = {8, 9, 10, 11}; // pwm pins only
int value = 0; // variable to keep the actual value
int fadeStart; // random number for start of fade sequence
int inSpeed; // how fast the light fades in
int outSpeed; // how fast the light fades out
int inDelay; // how long before next flash starts
int outDelay; // how long before fade
//int pin; // randomizes which LED fires
void setup() {
// nothing to set up
}
void loop()
{
for (int i=0; i<=3; i++)
{
outDelay = random(5,30);
inDelay = random(1000,5500);
fadeStart = random(25,50);
inSpeed = random(25,50);
for(value = 0 ; value <= 255; value+=inSpeed) // fade in (from min to max)
{
analogWrite(ledPin[i], value); // sets the value (range from 0 to 255)
delay(fadeStart); // waits for X milli seconds to see the dimming effect
}
outSpeed = random(25, 150);
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledPin[i], value);
delay(outDelay);
}
delay(100); // the delay between changes
}
}