hi,
I'm practicing with millis() and got on something I can't undurstand.
I modified the
Blink Without Delay to accomodate 3 LEDs.
If I put 3 different intervals
long interval5 = 1000; // interval at which to blink (milliseconds)
long interval6 = 2000;
long interval7 = 3000;
the LEDs start blinking in sync and after about 1 minute get out of sync.
but then,
If I put 3 times the same interval
long interval5 = 1000; // interval at which to blink (milliseconds)
long interval6 = 1000;
long interval7 = 1000;
the LEDs never get out of sync!
I first thought that since the code for each LED passes through the loop turning them on or off one after the other, the small difference in the order of appearance slowly accumulates and ends up busting the pattern, but then the same would happen with equal intervals.
what am I missing?
here's the code
// constants won't change. Used here to
// set pin numbers:
const int ledPin5 = 5; // the number of the LED pin
const int ledPin6 = 6;
const int ledPin7 = 7;
// Variables will change:
int ledState5 = LOW; // ledState used to set the LED
int ledState6 = LOW;
int ledState7 = LOW;
long previousMillis5 = 0; // will store last time LED was updated
long previousMillis6 = 0;
long previousMillis7 = 0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval5 = 1000; // interval at which to blink (milliseconds)
long interval6 = 2000;
long interval7 = 3000;
void setup() {
// set the digital pin as output:
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
}
void loop()
{
// 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 currentMillis5 = millis();
unsigned long currentMillis6 = millis();
unsigned long currentMillis7 = millis();
if(currentMillis5 - previousMillis5 > interval5) {
// save the last time you blinked the LED
previousMillis5 = currentMillis5;
if (ledState5 == LOW)
ledState5 = HIGH;
else
ledState5 = LOW;
digitalWrite(ledPin5, ledState5);
}
if(currentMillis6 - previousMillis6 > interval6) {
// save the last time you blinked the LED
previousMillis6 = currentMillis6;
if (ledState6 == LOW)
ledState6 = HIGH;
else
ledState6 = LOW;
digitalWrite(ledPin6, ledState6);
}
if(currentMillis7 - previousMillis7 > interval7) {
// save the last time you blinked the LED
previousMillis7 = currentMillis7;
if (ledState7 == LOW)
ledState7 = HIGH;
else
ledState7 = LOW;
digitalWrite(ledPin7, ledState7);
}
}
thanks