I would like to create the following:
Two led that can blink within in sync at a given interval but also to be out of small sync (around 50ms) once in few blinks/few seconds.
here is a behaviour I try to achive:
Both led are blinknig for 50ms ON state every 2000 ms and after a some time - lets say 10 blinks led1 will blink out of phase of 40 ms from led2 and both led will now be in an interval of 3000 ms between blinks, than, after another x number of seconds (or after a x number of blinks) both LED will be in sync again and will blink at the same time for the same interval duration..
I am looking to have the control both on the on Time duration and on the interval duration as well on the 'out of phase' length between the two leds.
here is the code I got:
const uint8_t led1 = 2;
const uint8_t led2 = 3;
uint32_t previousMillis1 = 0;
uint32_t previousMillis2 = 0;
const uint16_t interval_led1 = 1000;
const uint16_t interval_led2 = 1000;
const uint16_t onTime_led1 = 50;
const uint16_t onTime_led2 = 50;
bool ledState1 = LOW;
bool ledState2 = LOW;
void setup()
{
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
}
void loop()
{
if (ledState1 == HIGH && millis() - previousMillis1 >= onTime_led1) // interval time has passed
{
previousMillis1 = millis(); //store the time now to be previous time
ledState1 = LOW;
digitalWrite(led1, ledState1);
}
if (ledState1 == LOW && millis() - previousMillis1 >= interval_led1) // interval time has passed
{
previousMillis1 = millis(); //store the time now to be previous time
ledState1 = HIGH; // make LED HIGH
digitalWrite(led1, ledState1);
}
if (ledState2 == HIGH && millis() - previousMillis2 >= onTime_led2) // interval time has passed
{
previousMillis1 = millis(); //store the time now to be previous time
ledState2 = LOW;
digitalWrite(led2, ledState2);
}
if (ledState2 == LOW && millis() - previousMillis2 >= interval_led2) // interval time has passed
{
previousMillis2 = millis(); //store the time now to be previous time
ledState2 = HIGH; // make LED HIGH
digitalWrite(led2, ledState2);
}
}
while that code was working just fine for 1 LED I got the following problems with two LEDs:
- why I am looking for is not the code I have.
- although both led have the same onTime and interval duration they are not blinking in sync all.
- I have no control over a change in time - for example - to change the interval from 2000ms to 5000ms after 10 blinks and two get out of synch in 50ms between LEDs
- I repeat to many times the same code which I understand it is not good programming.
Any help will be greatful!
Thanks

