If, else if problem

Hi. I'm new in programming Arduino and i have a problem. I try use function millis(), if and else if. I try blink len on Pin 13 when time is between 1-5sec and this is ok but second code is problem. I want to blink my led again between 6-8sec and efect is led is only on but why i don't know. Can you help me explain this problem? Thanks :wink:

unsigned long actualTime =0;
unsigned long rememberTime = 0;
unsigned long diferentTime = 0;
 
void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  
}
 
void loop(){
 
   actualTime = millis();

  diferentTime = actualTime - rememberTime;
 
  if (diferentTime >= 1000UL) {
    digitalWrite(13, HIGH);
    rememberTime = actualTime;}
    else if  (actualTime >=5000){
      digitalWrite(13,LOW);
    }

  if (actualTime >= 6000UL) {
    digitalWrite(13, HIGH);
    rememberTime = actualTime;}
    else if  (actualTime >=8000)
      digitalWrite(13,LOW);

       Serial.println(actualTime);
   
  }

actualTime = millis();

. . .

if (actualTime >= 6000UL)

6 seconds after you turn on or reset the Arduino this line of code will always be true. :wink:

Ok Larryd and what you think about this structure. It's work ok but i won't change structure this program.

unsigned long actualTime =0;
unsigned long rememberTime = 0;
unsigned long diferentTime = 0;
 
void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  
}
 
void loop(){
 
   actualTime = millis();

  diferentTime = actualTime - rememberTime;
 
  if (diferentTime >= 1000UL) {
    digitalWrite(13, HIGH);
    rememberTime = actualTime;}
    else if  (actualTime >=5000){
      digitalWrite(13,LOW);
    }

  if (actualTime >= 6000UL) {
    digitalWrite(13, HIGH);
    rememberTime = actualTime;}
     if  (actualTime >=8000){
      digitalWrite(13,LOW);}

       Serial.println(actualTime);
     }

You would like your LED “ON” from 1-5 seconds then again at 6-8 seconds then stay “OFF” forever?

if(( millis() >= 1000 && millis() <= 5000) || ( millis() >= 6000 && millis() <= 8000) )
{
   digitalWrite(13, HIGH);
}

else 
{
   digitalWrite(13, LOW);
}

gregory099:
Hi. I'm new in programming Arduino and i have a problem. I try use function millis(), if and else if. I try blink len on Pin 13 when time is between 1-5sec and this is ok but second code is problem. I want to blink my led again between 6-8sec and efect is led is only on but why i don't know. Can you help me explain this problem? Thanks :wink:

Your on-for-a-second/off-for-a-seccond blinking is missing the off-for-a-second part. The code will be easier to understand if you make the blinking part separate from the timing part.

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}


void blink()
{
  static unsigned long rememberTime =  0;


  if (millis() - rememberTime >= 100UL)
  {
    rememberTime = millis();
    // Toggle the LED:
    if (digitalRead(LED_BUILTIN) == LOW)
      digitalWrite(LED_BUILTIN, HIGH);
    else
      digitalWrite(LED_BUILTIN, LOW);
  }
}


void loop()
{
  unsigned long actualTime = millis();


  // It has not been at least a second since the timer was last started
  if  (actualTime < 5000 || (actualTime >= 6000 && actualTime < 8000))
    blink();
  else
    digitalWrite(LED_BUILTIN, LOW);


  Serial.println(actualTime);
}

Note: I sped up the blinking since you can fit only one 2-second cycle between 6 and 8 seconds.

Hello. What i need. I need in my sketch turn on/off fun and motor dc but i try do this on led. When millis() start i wont turn led on (HIGH) when time is 1-5 sec next turn of led (LOW). Next step when time in millis() is 6 sec turn led on(HIGH) and when millis() is 9sec turn ledn off (LOW). I need use millis() because delay stop my program. I write what i need witch function delay.

digitalWrite(13,HIGH);
delay(5000);
digitalWrite(13,LOW);
delay(3000);

// When millis() start i wont turn led on (HIGH)
// when time is 1-5 sec next turn of led (LOW).
// Next step when time in millis() is 6 sec turn led on(HIGH)
// and when millis() is 9sec turn ledn off (LOW).


// So:  HIGH until 1000, LOW until 6000, HIGH until 9000, then LOW forever


const byte Motor_DOPin = LED_BUILTIN;  // Use LED to represent motor


void setup()
{
  pinMode(Motor_DOPin, OUTPUT);
}


void loop()
{
  if (millis() < 1000)
    digitalWrite(Motor_DOPin, HIGH);  // HIGH until 1000
  else if (millis() < 6000)
    digitalWrite(Motor_DOPin, LOW);  // LOW until 6000
  else if (millis() < 9000)
    digitalWrite(Motor_DOPin, HIGH);  // HIGH until 9000
  else
    digitalWrite(Motor_DOPin, LOW);  // Then LOW forever
}

My friend johnwasser yes this i need very very thanks :slight_smile: It's work very well :D:D Thank you :slight_smile:

So, from reply #6 I understand that you understand delay() stops the Arduino. But since you haven't said what else you want to do, maybe it is ok to stop?