The most valuable part of this example is not its physical part but its logical one, and I'll show you the associated functions to deal with analog outputs and a very important control structure in any programming language called a for
loop in the code we will be using for this circuit.
Dealing with analog outputs from the programming side is just a matter of using the analogWrite()
function, which, as its name implies, writes an analog value through a pin.
It takes two parameters, similar to the digitalWrite()
function:
The other important concept we will come across in this example is a for
loop, which is a very important control structure present in almost every programing language that allows you to repeat a block of code a specific number of times.
For its operation, a for
loop needs what is usually called a control variable, that is, a variable that is going to take different values in a range for every iteration of the loop until it reaches a final value.
The general syntax of a for
loop is as follows:
for (initialization; condition; increment) { instructions; }
The three parts inside the parentheses have the following mission:
An example will greatly clarify the concept. We could write the following block of code:
for(time = 100 ; time <= 1000; time=time+50){ digitalWrite(led, HIGH); delay(time); digitalWrite(led, LOW); delay(time); }
In this example code, we will begin by making the time
variable take a value of 100
and make the loop iterate, executing all instructions inside as long as the time
variable has a value less than or equal to 1000
, incrementing it by 50
in every iteration.
This way, we would make an LED blink with lower frequencies for every iteration, given that we have used the time
variable as the parameter of the delay()
calls.
It would result in delay(100)
as the first iteration, delay(150)
as the second one, delay(200)
as the third one, and so on until the time
variable gets to a value of 1000
, in which case, the condition will evaluate to false and the for
loop won't continue to iterate. The program execution will follow through the next line of code just after the closing curly bracket of the for
loop.
For more information and some other examples of the use of a for
loop, you can go and visit the for
loop page on the Arduino site's Reference section at http://arduino.cc/en/Reference/For.
Regarding the rest of the code, I'm going to use three variables; two of them, which I've called led
and increment
, won't really change throughout the execution of the program, but they will greatly increase the readability of the code.
The led
variable is going to be used to store the pin number I'll use to control the LED, pin number 11, in this example. Remember that you can use whichever pin you want from those marked as PWM in the Arduino board and that you will have to set this variable as per your choice, accordingly.
The increment
variable will hold the step size we will use in the for
loops that we will use later. Using a variable just to use the increment
value makes it very convenient to make future modifications by just changing its value at the beginning of the code without the hassle of having to search throughout the code for every occurrence of the pretended value.
The other variable, which is intensity
, will be the key of this sketch, as it is going to be used as the control variable of two for
loops that we are going to use to go all along the range of possible values for the analogWrite()
calls we will use to gradually change the brightness of the LED.
Well, let's see this theory in action. Here is the complete code for the LED fading sketch:
/* Chapter 04 - Controlling outputs softly with analog outputs Single analog output to fade a LED By Francis Perea for Packt Publishing */ // Global variables we will use int led = 11; int intensity = 0; int increment = 5; // Configuration of the board void setup() { // Set the pin we are going to use as an output pinMode(led, OUTPUT); } // Main loop void loop() { // fade from minimum to maximum for(intensity=0; intensity<=255; intensity=intensity+increment){ analogWrite(led, intensity); delay(30); } // fade from maximum to minimum for(intensity=255; intensity>0; intensity=intensity-increment){ analogWrite(led, intensity); delay(30); } }
As I have explained previously, we begin by declaring and assigning initial values to the three global variables we are going to use all along the program.
In the setup()
function, we simply set the pin we are going to use as an output.
In the loop()
function, we have just two for
loops: one taking values from 0
to 255
in increments of 5
and the other taking values from 255
to 0
using -5
increments.
In every iteration of each for
loop, we simply set the changing value of the control variable, which is intensity
in our code, as the analog value we want to output to the LED and wait for 30 milliseconds to allow our eyes to see the change.
The first loop makes the intensity of the LED increase from 0V to 5V and the second one makes the intensity decrease from 5V to 0V, which is just the opposite.
I'll suggest that you play with this code, and as an exercise, you could make both parts of the fading run at different frequencies simply by setting different values for the increment
variable just before entering the for
loops, making it 10
when going up and 50
when going down. The resulting code could be as follows:
void loop() { // fade from minimum to maximum in increments of 10 increment = 10; for(intensity=0; intensity<=255; intensity=intensity+increment){ analogWrite(led, intensity); delay(30); } // fade from maximum to minimum in decrements of 50 increment = 50; for(intensity=255; intensity>0; intensity=intensity-increment){ analogWrite(led, intensity); delay(30); } }