Exercise 28 - Candle light
1. INTRODUCTION
On this exercise, we are going to try simulating a candle light with Arduino. A candle do a characteristic randomly blink and it is not the entire same colour.
To improve the effect you can cover the LEDs with a white paper like int the
picture.
2. HOW TO CONNECT?
To simulate a candle light we need three LEDs, 2 yellow and one red. The connection is trivial, like in EXERCISE_2 but connecting three LEDs instead only one to pins 11, 10, 9. On the other hand, connect to GND all of cathodes.
EXERCISE_A |
Materials:
· Arduino UNO board
· Protoboard
· Some wire Jumpers
· 3x LEDs
· 3x 220 ohms resistor
3. PROGRAMMING
First assign LEDs to digital pins with PWM.
int led1 = 10;
int led2 = 9;
int led3 = 11;
On setup set assigned pins as OUTPUT
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
On loop, to simulate the flame of a candle you have to use random() function. In this function, you can put the arguments in two ways:
· random(number). Select a random number between zero and number.
· random(number1,number2). Select a random number between number1 and number2.
Knowing the use of this function, you can apply it to the code. The LEDs are written with random values. On the other hand, the delay between values also is defined randomly.
void loop() {
analogWrite(led1, random(135,255));
analogWrite(led2, random(135,255));
analogWrite(led3, random(135,255));
delay(random(50,150));
}