Multi-Color Glow Lamp
Step 1 - Build the Project
Topics Covered:
In this code, two variables are interacting with each other, then passing a value to the outputs. The variables in the code are the only inputs here.
Step 2 - Upload the Code
Step 3 - Read the Walkthrough
Our idea is to have three colors fading up and down at slightly different strengths for a relaxing glow lamp. To create that steady fade, you create two variables: one holds the brightness, which is changed by ‘fade’ amount each loop through the code.
After setting up each output of the multicolor LED, the loop first gives each LED color a brightness to analogWrite(). To make each color fade a little differently, you can add some random amount from 0 to 255 to the starting point of any color. For the first loop through, the brightnesses will be 0, 50, and 100. On the second loop, the brightness values will equal 3,53, and 103.
When the brightness variable reaches its maximum, the ‘if’ statement changes the fade variable to a negative number, so the fade starts to reverse. Fade is then changed back to a positive when the brightness reaches 0.
There are two easy ways to modify this code to see the effects. First, change the fade variable to something larger so that the steps between each brightness are larger. Next, change the delay in the program to see the effect of making each step last a shorter while.