Project 9 – LED Fire Effect

Project 9 will use LEDs and a flickering random light effect, via PWM again, to mimic the effect of a flickering flame. If you place these LEDs inside a house on a model railway, for example, you can make it look like the house is on fire, or you can use it in a fireplace in your house instead of wood logs. This is a simple example of how LEDs can be used to create special effects for movies, stage plays, model dioramas, model railways, etc.

Parts Required

This time we are going to use three LEDs: one red and two yellow.

    Red Diffused 5mm LED           images

2 Yellow Diffused 5mm LED      images

3 Current Limiting Resistor         images

Connect It Up

Power down your Arduino, then connect your three LEDs as shown in Figure 3-6. This is essentially the same circuit as in Project 8, but using one red and two yellow LEDs instead of a red, green, and blue. Again, the effect is best seen when the light is diffused using a cylinder of paper, or when bounced off a white card or mirror onto another surface.

images

Figure 3-6. The circuit for Project 9 – LED Fire Effect (see insert for color version)

Enter the Code

Open up your Arduino IDE and type in the code from Listing 3-5.

Listing 3-5. Code for Project 9

// Project 9 - LED Fire Effect
int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 11;

void setup()
{
        pinMode(ledPin1, OUTPUT);
        pinMode(ledPin2, OUTPUT);
        pinMode(ledPin3, OUTPUT);
}

void loop()
{
        analogWrite(ledPin1, random(120)+135);
        analogWrite(ledPin2, random(120)+135);
        analogWrite(ledPin3, random(120)+135);
delay(random(100));
}

Press the Verify/Compile button at the top of the IDE to make sure there are no errors in your code. If this is successful, click the Upload button.

If you have done everything right, the LEDs will flicker in a random manner to simulate a flame or fire effect.

Project 9 – LED Fire Effect – Code Overview

Let's take a look at the code for this project. First, you declare and initialize some integer variables that will hold the values for the digital pins you are connecting your LEDs to:

 int ledPin1 = 9;
 int ledPin2 = 10;
 int ledPin3 = 11;

Then, set them to be outputs:

 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);

The main program loop sends out a random value between 0 and 120; add 135 to it to get full LED brightness for the PWM Pins 9, 10, and 11:

 analogWrite(ledPin1, random(120)+135);
 analogWrite(ledPin2, random(120)+135);
 analogWrite(ledPin3, random(120)+135);

Lastly, you have a random delay between ON and 100ms:

 delay(random(100));

The main loop then starts again, causing the flicker effect. Bounce the light off a white card or a mirror onto your wall and you will see a very realistic flame effect.

The hardware is simple and you should understand it by now, so let's move on to Project 10.

EXERCISE

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.219.197