If inside if to control LED

Hello, I am using the code below to control the LED.
Iexpected the LED to be on for 10 s, and then off for 5s.
Also, I want to adjust the LED brightness when it is on, so I tried to flash it (10 ms on, 40 ms off) with the nested if structure.

However, the LED is always off. Is there anything wrong with my code?
Thanks,

        
int ledPin =  13;      
int ledState = LOW;            
long previousled = 0;        
long ledOnTime = 10000;          
long ledOffTime = 5000; 
//set LED on for 10s, off for 5s.

int ledflashState = LOW;
long previousledflash = 0;        
long ledflashOnTime = 10;          
long ledflashOffTime = 40; 
//during LED on, the LED flashes, 10ms on, 40ms off. This is used for adjust LED brightness.



 
void setup(void) {
 
  pinMode(ledPin, OUTPUT); 
 
  }
void loop() {
  long currentMillis = millis();
 
  
  if ((ledState == LOW) && (currentMillis - previousled >= ledOffTime))
  {
    ledState = HIGH;  
    previousled = currentMillis;
    previousledflash = currentMillis; 
    
      if ((ledflashState == HIGH) && (currentMillis - previousledflash >= ledflashOnTime))
    {
      ledflashState = LOW;
      previousledflash = currentMillis;
      digitalWrite(ledPin, ledflashState); 
    }
      else if((ledflashState == LOW) && (currentMillis - previousledflash >= ledflashOffTime))
    {
      ledflashState = HIGH;
      previousledflash = currentMillis;
      digitalWrite(ledPin, ledflashState); 
    }
  }

  
   else if((ledState == HIGH) && (currentMillis - previousled >= ledOnTime))
  {
    ledState = LOW;  
    previousled = currentMillis;  
    digitalWrite(ledPin, ledState);  
  }
  
}

The reason it won't "flash" the LED is because of the logic below. You are setting previousledflash to currentMillis right before you test currentMillis - previousledflash. currentMillis - previousledflash will always be 0!

    previousledflash = currentMillis;

    if ((ledflashState == HIGH) && (currentMillis - previousledflash >= ledflashOnTime))
    {
      ledflashState = LOW;
      previousledflash = currentMillis;
      digitalWrite(ledPin, ledflashState);
    }
    else if ((ledflashState == LOW) && (currentMillis - previousledflash >= ledflashOffTime))
    {
      ledflashState = HIGH;
      previousledflash = currentMillis;
      digitalWrite(ledPin, ledflashState);
    }

If you use an LED connected to one of the PWM pins you can control the brightness via the analogWrite() function.

Thank you for your reply! I think that is the case.
It inspired me to use parallel if instead of nested if structure, as the code showed below, and it works as expected!

The analogWrite also works.

Thank you.

        
int ledPin =  13;      
int ledState = LOW;            
long previousled = 0;        
long ledOnTime = 10000;          
long ledOffTime = 5000; 
//set LED on for 10s, off for 5s.

int ledflashState = LOW;
long previousledflash = 0;        
long ledflashOnTime = 10;          
long ledflashOffTime = 40; 
//during LED on, the LED flashes, 10ms on, 40ms off. This is used for adjust LED brightness.



 
void setup(void) {
 
  pinMode(ledPin, OUTPUT); 
 
  }
void loop() {
  long currentMillis = millis();
 
  
  if ((ledState == LOW) && (currentMillis - previousled >= ledOffTime))
  {
     
    previousled = currentMillis; 
    ledState = HIGH; 
  }
  
  if ((ledState == HIGH) &&(ledflashState == HIGH) && (currentMillis - previousledflash >= ledflashOnTime))
    {
      ledflashState = LOW;
      previousledflash = currentMillis;
      digitalWrite(ledPin, ledflashState); 
    }
    
  if((ledState == HIGH) &&(ledflashState == LOW) && (currentMillis - previousledflash >= ledflashOffTime))
    {
      ledflashState = HIGH;   
      previousledflash = currentMillis;
      digitalWrite(ledPin, ledflashState); 
    }
    
 

  
   else if((ledState == HIGH) && (currentMillis - previousled >= ledOnTime))
  {
    ledState = LOW;  
    previousled = currentMillis;  
    digitalWrite(ledPin, ledState);  
  }
  
}

kinda confusing

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.