LED fade + strobe effect

Hi guys,

Im pretty new to programming the arduino so I thought Id run through all the basic stuff. I did the flashing of the LED using millis rather than delay and was able to make the LED have a fading effect.

Currently trying to link it all into one program such that when the program starts, first there is a strobe effect for a period of time, and then the fading effect following that. The problem is that once, the LED performs its flashes, it goes into the fading and doesnt want to get out of that section and loop back to the flashing. I cant really understand why, ive been trying various if conditions, while loop and for loops, but there is clearly something im missing.

Here is my code

const int ledPin =9; // PWM
int brightness = 0;
int fadeAmount = 5;
int counterStrobe =0;
int counterFade =0;
int strobeFlag=1;
int fadeFlag =1;

int ledState = LOW;
unsigned long previousMillis =0;
const long interval = 30;

void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
if(counterStrobe ==0){
while(strobeFlag!=0){
unsigned long currentMillis=millis();
if(currentMillis - previousMilis >= interval){
previousMillis = currentMillis;
if(ledState==LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin,ledState);
counterStrobe++;
if(counterStrobe ==20)
{
counterFade=0;
strobeFlag=0;
fadeFlag=1;
}
}
}
}

for(counterFade;counterFade<50;counterFade++)
{
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if(brightness ==0 || brightness == 255)
delay(30);
}
counterStrobe=0;
}

this code does not work and i cant really figure out why

I could not able to understand what you are trying to do.

What is use of strobe flag here;

Why you running this condition below thing work when if(counterStrobe !=0)

for(counterFade;counterFade<50;counterFade++)
  {
    analogWrite(ledPin, brightness);
    brightness = brightness + fadeAmount;
    if(brightness ==0 || brightness == 255)
      delay(30);
  }

so put serial.print statement to know which part of code is executing

The flags are simply used to break out of while loops if i need them.

The problem is that after the fade sequence, the program doesnt loop back around and start flashing again

Can you explain properly what you expecting your output.

http://arduino.cc/en/tutorial/fading

simple fading avilable here. If you can share schematic or flowchat good to undersstand

Circuit is simply Resistor connected to anode of LED Into pin 9 and cathode to ground

//The pin which the LED is connected. Anode (longer leg) to pin 13 (digital)
// and cathode (shorter leg) always to ground.
const int ledPin = 9;
int brightness =0;     // brightnes of LED
int fadeAmount = 5;    // the amount the brightness changes each time
int counterStrobe = 0; // counter for flashes
int counterFade =0 ;   // counter for fade
int strobeFlag =1;
int fadeFlag=1;


//Setting the state of the LED to be low ie OFF
int ledState = LOW;
// previousMillis is used so that we know the last state
unsigned long previousMillis = 0;
// The interval which the LED is on /off. In general time variables
// are set as long to prevent overflows.
const long interval = 30;

void setup()
{
  //Setting up the LED to be an output
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}




void loop()
{

  if(counterStrobe==0){
    //making currentMillis to store the time in milliseconds
    while(strobeFlag!=0){
      unsigned long currentMillis=millis();

      //So if the the LED was on, we check that the time that it was on
      // minus the previous time   greater than or equal to the interval
      // which is 1000, if it is greater then we set the time to be stored
      // in previous for use next time
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;


        //If the LED is off, then make it high else, if it is High, then make it low
        if(ledState==LOW) 
          ledState =HIGH;
        else
          ledState = LOW;

        //Makes the LED High or Low, so what the conditions, make the ledPin,
        //which is 13, High or low depending on condition - outputs the ledState.
        digitalWrite(ledPin, ledState);
        counterStrobe++;
        if(counterStrobe == 100)
        {
          counterFade=0;
          strobeFlag = 0;
          fadeFlag=1;
        }


      }

    }
  }





 counterStrobe=0; 
  analogWrite(ledPin, brightness);
  // change the brightness for next time through the loop
  brightness = brightness + fadeAmount;

  
  //reverse the direction of the fading at the ends of the fade
  if(brightness == 0 || brightness == 255) fadeAmount = -fadeAmount;
 delay(30);
  
  previousMillis=0;

  }

Currently this code make the led flash as expected and begin to fade after the code is executed. The problem is that is continues fading forever and does not loop back to flashing,

I wasnt it to cycle through both flashing and fading effects, but it gets stuck after one loop.

I can get them to work individually but would like to alternate effects.

You have to do something else with strobeFlag and fadeFlag, currently you never reset strobeFlag to 0 anywhere, and fadeFlag is not used.

Currently this code make the led flash as expected and begin to fade after the code is executed. The problem is that is continues fading forever and does not loop back to flashing,

I wasnt it to cycle through both flashing and fading effects, but it gets stuck after one loop.

I can get them to work individually but would like to alternate effects.

Didi you use the code. Do you wanted to expected to code work like this??

[code]int ledPin = 9;    // LED connected to digital pin 9

void setup()  { 
  // nothing happens in setup 
} 

void loop()  { 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}

[/code]

Yes guix, i know, i have tried using them but it doesnt produce the effect i want.

@AMPS my code works

it flashes and then it dims brighter and darker but continues that effect indefinitely , but doesn't loop back no matter what i do, which doesnt make sense to me because the whole idea of void loop is to loop everything within it forever

So again , it's not that the effects don't work . It's that they don't continue over and over again.

First Strobe effect works for allocated period of time

Next , fading occurs , but works indefinitely , it does not end ad therefore the flashing never occurs again

Check below code fil your requirement

const int ledPin =9; // PWM
int brightness = 0;
int fadeAmount = 5;
int counterStrobe =0;
int counterFade =0;
int strobeFlag=1;
int fadeFlag =1;

int ledState = LOW;
 long int previousMillis =0;
const long interval = 30;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  if(counterStrobe ==0){
    Serial.println("counterStrobe entered");
    while(strobeFlag!=0){
      Serial.println("strobe flag entered");
      unsigned long currentMillis=millis();
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
        if(ledState==LOW)
          ledState = HIGH;
        else
          ledState = LOW;
        digitalWrite(ledPin,ledState);
        counterStrobe++;
        Serial.print("counterStrobe:"); Serial.println(counterStrobe);
        if(counterStrobe ==20)
        {
          counterFade=0;
          strobeFlag=0;
          fadeFlag=1;
          Serial.println("helllo");
           
        }
      }
      counterStrobe=0;
    }
    
  } 

  for(counterFade;counterFade<50;counterFade++)
  {
    Serial.println("for loop entered");
    
    analogWrite(ledPin, brightness);
    brightness = brightness + fadeAmount;
    if(brightness ==0 || brightness == 255)
      delay(30);
     // counterStrobe=0;
       Serial.println("for loop exit");
  }
 
}

Please use the code from post #5.

The code you posted doesnt work the same way mine does. I tried using serial prints and the fade effect just occurs forever

That means you need fade effect as itself in your code.

In your code the fade effect get stop after count ==20 .

Put serial print statement in each stage,i could not able to understand your requirement.

question is

How frequent you wanna fading effect??? can you explain in simple words since my english is too bad. Fading i say flickering ; you want LED to flicker continuously or you want set time frame within that time frame it should flicker & after that it goes HIGH

I have the fade effect, and the flashing effect.

I can control the flashing of the LED - so thats turning the led on and off over and over again.

Fading is where you increase and decrease brightness

I can do both of these things

But the flashing sequence (on off on off) goes only once

and the fading (increasing and decreasing brightness) continues forever

so the problem is that the program does not loop back around to the start.

How can you differentiate fading & flashing effect Because LED will running in too fast .I have tried your code . i also tested fading & flickering problem

Individually they are working fine. but difficult to diffreciate

Its easy to differentiate, just change the interval that the LED flashes - the interval variable controls this, change it 1000 and it will stay on and off for a second at a time,

fading is a constant addition or subtraction

const int ledPin =6; // PWM
int brightness = 0;
int fadeAmount = 5;
 int counterStrobe =0;
static int counterFade =0;
static int strobeFlag=1;
int fadeFlag =1;
static int counter=0;
int ledState = LOW;
 long int previousMillis =0;
const long interval = 30;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
   Serial.print("counter strobe value1:");Serial.println(counterStrobe);
  if(counterStrobe ==0){
  //  Serial.println("counterStrobe entered");
  Serial.print("strobe value1:");Serial.println(strobeFlag);
    while(strobeFlag!=0){
    //  Serial.println("strobe flag entered");
      unsigned long currentMillis=millis();
      if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
        if(ledState==LOW)
          ledState = HIGH;
        else
          ledState = LOW;
        digitalWrite(ledPin,ledState);
        counterStrobe++;
     //   Serial.print("counterStrobe:"); Serial.println(counterStrobe);
        if(counterStrobe >=20)
        {
        //  counterFade=0;
          strobeFlag=0;
         // fadeFlag=1;
         
          Serial.print("helllo");
           
        }
      }
    }
  } else
  {

  for(counterFade;counterFade<50;counterFade++)
  {
  //  Serial.println("for loop entered");
    
    analogWrite(ledPin, brightness);
    brightness = brightness + fadeAmount;
    if(brightness ==0 || brightness == 255)
      delay(30);
    // Serial.println("for loop exit");
     counter=counter+1;
     Serial.print("counter:"); Serial.println(counter);
  }

  
  Serial.print("counter strobe value2:");Serial.println(counterStrobe);
   Serial.print("strobe value2:");Serial.println(strobeFlag);
  counterStrobe=0; strobeFlag=1;  counterFade=0;
  }
 
}

This coding fine as you said. i found that your counter strobe vlu not setting to Zero. because of which it remain in same high, check this code give feedback is what you expected.

Thank you so much, that works much better, ill inspect the code and analyse the changes, THANK YOU :smiley:

try to explain in simple words. it taken me 1 day to understand. remove all Serial print statements.remove unnecessary tags.

Good luck with your project