This lesson will retain the same configuration as the previous lessons!  Those who fail to plan, plan to fail, right?

We learned about how to implement the PWM using the newly minted command 'analogWrite()'.  However, it is impractical manually writing code to simulate fading in and out.  We will introduce a new programming code snippet that will allow us to efficiently and effectively fade in and out.  

Question:  How can we have our LED fade in with what we know already?

It's actually quite simple.  If we wanted to go from off to fully on, we can use our analogWrite() commands along with our delay() code.  Here is an example of what this would look like:

analogWrite(Green_LED, 0); //start off
delay(50); //delays for 50 ms

analogWrite(Green_LED, 5); //adjusted analog value to '5'
delay(50); //delays for 50 ms

analogWrite(Green_LED, 10); //adjusted analog value to '10'
delay(50); //delays for 50 ms


analogWrite(Green_LED, 255); //adjusted analog value to '255'
delay(50); //delays for 50 ms

We would have to write 102 lines of code, just to fade in.  If you have a lot of time on your hands, have fun...

Introducing the for loop

This may seem intimidating at first, but try to understand!  Once you understand the 'for loop', you will be able to apply this to anything.  Remember, we have the void loop(){...} which houses our main Arduino program. The void loop get's repeated over and over again.  

A for loop can be used within the void loop and it is isolated from the void loop until it is finished with whatever task it is supposed to accomplish.  Below is the introduction of the for loop code snippet.   


for (int analog_value= 0 ; analog_value<= 255; analog_value+= 5) {
     analogWrite(Green_LED, analog_value);
     delay(50);  //delays for 50 milliseconds


Anatomy of the for loop
  • We always start with for(....)
  • We need a variable and a starting value.  In this case, our integer variable name is 'analog_value'.  'analog_value' is set to 0 (or turned off).
  • Now we need a condition.  The for loop needs a conditional statement to exit the for loop.  Otherwise we will end up being in an infinite loop and we will most likely not be doing anything.  The condition we have set is: analog_value must be less than or equal to 255 (analog_value <= 255).
  • Lastly, we need an incrementer or decrementer.  Every time the for loop meets the condition of analog_value <= 255, we are going to add 5 to the current analog_value.  'analog_value += 5' means that we will be adding 5 every time, this is known as an incrementer.  We can change the increment to whatever integer we want.  We could increment slower by putting the value of 1 for instance.  
  • {...} <- Always close the for loop with a curly bracket.  It is important to ensure that the curly bracket is closed.  If we don't do that, this will throw an error.  
  • 'analogWrite(Green_LED, analog_value)' <- We've seen this many times now.  Remember how back in the stone age, we manually set the brightness (Lesson 06).  Now with our fancy 'for loop' we are setting the 'brightness' automatically with 'analog_value'.  The 'analog_value' will continue to change until the condition is not met.  For example, when 'analog_value' reaches 260, we will exit our 'for loop'.

Yes, there were a lot of words. We hope that you understand the basics of the for loop!  
Currently the program that is attached will turn the LED off, fade in, LED off, and then fade in repeatedly.  

Question:  How can we make the LED fade in and out?  
We'll give you a hint, we need to use a decrementer ('analog_value -= 5').  We also need to start somewhere different and set up a new condition.  See the second attachment if you get stuck!  

Try testing out different way and rates on how to speed up/slow down the fader!  This can be done by changing the delay or changing the increment/decrement value.  Have fun!

Quote 0 0