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? Thanks.