Intro: Random RGB LED's
As many people with a garden, I do also like those "mood" lights, that are sold for high prices.
But... I have a lot of Arduino's lying around, as well as RGB -LED's.
So, it's easy to make those moodlights :-)
To copy this, you need some RGB LED's, resistors, at least one Arduino, and, as I did find, a bulb you can put over the LED's, so the light is diffused.
Sometimes you find those diffused parts in older outside porchlights, mine are "recycled" from defective LED-bulbs for house lighting.
It is so easy to make this, I don't give Fritzings in it...
Let's go to the next step...
Step 1: Easy, With a Little Trick...
Most here who have Arduinoes know how to fade a LED.
For those who don't: put a Pulse Width Modulated signal on the LED, and, by changing the Pulse Width, the lightstrenght of the LED changes too. Why?
Simply, with a PWM signal, you make the LED blink about 4000 or 9000 times a second (the frequency of the PWM signal). By using the analogWrite statement in programming the Arduino, you vary the time of the positive (and thus negative) period of the signal. The smaller the amount of time the positive period is on, the shorter the "ON"-blink of the LED. So the dimmer it seems.
There is no problem changing all the analogWrite value within the program, going thru loops and so on... But after a while you have the same sequence of dimming your RGB LED.
So... Just use the random() function of Arduino.
But this is not so "random" as we wish. It always starts with the same "seed"... So a big chance that your moodlight would show the sequence again...
So we "seed" the random generator with an always changing signal...
We do this simply like this:
In the setup we seed with the (quite random) tension we find on A0.
So our random generator start with a more random number.
Since we only need values from 0 to 255 to write away to our LED's, we use:
bl = random(0,256); (for the blue LED)
In next step I'll add the entire sketch...
If we make a loop that always catches random numbers, and feed those to the LED, we pretty much get a lamp that could give all the colours of the rainbow (plus a few thousand more..) But they just flicker. The flickering depends on how fast you let it cycle through the random numbers. And that was not as moody or Zen like I wanted it.
So I took the next step.
Step 2: Randomly Fading...
For each colour, I took two variables. The "old" and the new...
As it has been used before, the old variable stores the last value when the "new" gets a new value.
Then I made a mini- " if-then" routine.
A simple fade-routine as there is an example with the Arduino IDE. I just looked is the newer value was higher or lower then the previous value. Then I faded from the old to the new value. That was smooth :-)
Of course, you do that to all three the parts of the RGB-LED.
Up to the last step...
Step 3: Step 3 Is Up to You... From Idea to Project
The little clip shows how it looks.
Quite slowly fading, and not very hard "dots" because of the diffusing cap.
The example is with only two LED's.
How to go further? Use a smaller Arduino or stand alone ATMega, get the power from batteries or a PV cell. Use a LDR to switch it on when it's dark enough outside. Use different lamps, on different PWM outputs...
Hope you enjoyed this little trick!