Millis question

Hello everyone. Lets say, Im a newbie in arduino but anyway i made few easy projects like a home termometr, weather station and wire cutter.

Im trying to understand a millis. I want to get two LEDs turn ON in the same moment but one is ON for example 7 seconds and the other 12 seconds - and in loop....

Code below works on different way. One led start first, and later second.

int ledPin1 =  4; 
int ledState1 = LOW; 
unsigned long previousMillis1 = 0; 
long OnTime1 = 7000;
long OffTime1 = 14000; 

int ledPin2 =  13;  
int ledState2 = LOW; 
unsigned long previousMillis2 = 0; 
long OnTime2 = 12000;
long OffTime2 = 10000; 
     
void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);       
  pinMode(ledPin2, OUTPUT);      
}
     
void loop()
{
  unsigned long currentMillis = millis();
     
  if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW;
    previousMillis1 = currentMillis;
    digitalWrite(ledPin1, ledState1); 
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;
    previousMillis1 = currentMillis;   
    digitalWrite(ledPin1, ledState1);   
  }


      
  if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    ledState2 = LOW;
    previousMillis2 = currentMillis;  
    digitalWrite(ledPin2, ledState2); 
  }
  else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    ledState2 = HIGH;  
    previousMillis2 = currentMillis;   
    digitalWrite(ledPin2, ledState2);   
  }
}

Could anybody tell me where is fault, how to get what i need? :slight_smile: Thanks.

I want to get two LEDs turn ON in the same moment
Code below works on different way. One led start first, and later second.

long OnTime1 = 7000;
long OffTime1 = 14000;

long OnTime2 = 12000;
long OffTime2 = 10000;

The two cycle times (on time + off time) are not the same. Try making them the same and see if you stay in phase.

There are probably more precise ways to keep things synchronized over long periods of time by turning both leds on in the same code block, turning one off, then turning the second off, and then restarting the sequence. A state machine would work well.

Sorry, forgive me that on off times. I just copied lines with fault.

If you're saying that you haven't posted the complete sketch, please do it now.

I just copied lines with fault.

OK. No problem to fix that issue.

I looked closer at the code, and realize that when you equalize the periods there will be another issue.

I want to get two LEDs turn ON in the same moment

You start with the ledstate LOW/OFF, and they will turn on at different times because they have different off times. They will turn off together, but not on together.

To make them turn on together, you need to start with both Leds initially turned ON at the same time. I would turn them on in setup and change the initial state to HIGH/ON. They will then turn off at different times, but should light up again together.

damn, you have right. It was so easy and I didn't run into. Change state to high works. You're amazing.