Breathing Lights | MindPlus Coding Kit for Arduino Started Tutorial E06
Chapter 3 Controllable Lights
RGD LED is actually just one category of LED lights. What makes it unique is that while common LED emits a single shade of white lights, RGB LED is capable to displaying all colors via its three bulbs of red, green, and blue colors.
Now let’s try making all kinds of controllable LED lights!
Project 1 Breathing Lights
As its names suggests, breathing lights consist of of lights that go from dim to bright and back to dim by the rhythm of breathing. Breathing lights have been applied in many areas, including missed calls / SMS notifications on mobile phones, mouse breathing lights, keyboard backlights, external batteries charging lights, and cosmetic lightings on sound systems, etc.
1. What is a function?
2. Make a breathing light
Key Points Analysis
What is a function?
In essence, a function’s role is to perform a certain function. Every function is made for a particular feature, while the name of the function suggests what it is for.
We have functions in mathematics too. y=f(x) is a general form of functions. In mathematics, x will go through the calculations defined between () in f(x), and return as y. It’s the same in Mind+: when y=f(x) is executed, x is defined as the variable, and y is returned after being processed by function f.
Variables can also be defined within a function, which are valid only when the function is executed. When the function execution is over, these variables are no longer valid.
A “function call” means an execution of a function. A function can call other functions as well as itself.
Connect the red LED to digital pin 10 on the UNO board.
Hardware connection (LED-R-10)
Please match the colors when plugging
To make breathing light fade-in and fade-out, we’ll break it down to three steps. We’ll design
the overall structure first, then achieve fade-in and fade-out effects.
1. Create variables
Find the function module first.
2. Write the program
1) Design overall structure
2) Achieve child function feature “fade-in”
To make lights go from off to gradually brighter, we can set a variable’s initial value to 0, and
make brightness value increase by 1 every 0.01 second until maximum brightness.
3) Achieve child function feature “fade-out”
The same for lights to go from maximum brightness to gradually dimmer: we can set a variable’s initial value to the maximum value 255
(See Further Reading for why it’s 255), and make brightness value decrease by 1 every 0.01 second, until it reaches 0.
The main body of the program is the main program on the left calling functions on the right, which are two functions defined.
3. Program’s effect
LED lights go gradually brighter and then gradually dimmer, demonstrating breathing effects.
4. Program analysis
In the main program, we set the variable i to 0 as an initial value. Although the default system value is already 0, but we still did it just to develop a good habit when we code.
After i is increased to 255 (maximum value) and the function is executed for a second time, i enters the next statements block, where it is decreased by 1 every time, i.e. increased by -1.
This program achieves “breathing lights” effects by controlling LED’s fade-in and fade-out using Arduino’s analog output. Breathing lights “take breathes”, as LED’s brightness increases gradually to maximum after an interval, before gradually dims to completely off and repeats the cycle. Changes in brightness are achieved by analog output module.
RGB color model uses RGB model to assign an intensity value, which is within 0-255, for the RGB component of every pixel in the image. Various colors are constituted through different intensities in three primary colors, red, green, and blue.
Each of the red, green, and blue color channels has 256 levels of brightness. At 0, the “light” has lowest brightness — light is off; while at 255, the “light” is brightest. When three colors have the same value of greyscale, they produce grey hues of different shades. That is to say, when their greyscales are all 0, they make the darkest black hue; when their greyscales are 255, they make the brightest white hue.
All colors on a computer screen are constituted by combinations of red, green, and blue lights of different proportions. A set of red, green, and blue lights is the minimal display unit. Any color on the screen can be expressed in a set of RGB values.
We achieved breathing light effects by function call. Now can we achieve “fade-in and fade-out” directly, without function at all? How?