Arduino Nativity Set LED Light Controller – Led fading effects
Now it’s time for some code.. :)
With Arduino it’s easy writing a simple sketch for dimming LEDs via PWN.. also for guy with (very) limited programming skills like me.
The learning material is very helpful and rich of examples.
We need to manage 3 independent led channels and a create a kind of “program” for lights effects.
Requirements for this first version:
- the total duration of the program/sequence should be configurable
- led brightness should be dimmable and max brightness configurable
- each led channel should have an independent fade-in phase, a constant light phase and a fade-out phase with scheduled start and stop times
Timing
The sequence is divided in 1024 steps, each one lasting for a number of milliseconds calculated dividing the total duratation (in ms) by 1024.
Of course we need to have only ONE delay at the end of the main loop.
..
float delayvalue = 0;
int cycledelay = 0;
int programduration = 30; // light program duration in seconds
…
void setup() {
…
delayvalue = ((float(programduration) * 1000) / 1024); // main delay in ms calculated on program duration
cycledelay = int(delayvalue);
}
…
void loop() {
…
delay(cycledelay);
}
In this example the program will last about 30 seconds.
Now we need to have a Fading (In and Out) lasting for the duration of the entire phase.
analogWrite function have only 255 values and we may want to have a longer lasting FadeIn/Out phase.
So we need to calculated the Fading step related to the duration of the phase.
It’s also useful to be able to control the maximum brightness we want to reach for creating for example weak illumination effects.
…
int brightnessCH1 = 0; // how bright the LED is
int brightnessCapCH1 = 0; // the maximum value of brigther (0-255)
float fadeAddCH1 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated
float fadeSubCH1 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated
float brightfloatCH1 = 0.0; // brightness in float
…
// Parameters, edit these for creating your custom show
…
int fadePcCH1 = 20; // the maximum percent of Fade for CH1
int CH1FadeInStart = 1; // start time of FadeIn phase for CH1
int CH1FadeInStop = 256; // stop time of FadeIn phase for CH1
int CH1FadeOutStart = 257; //start time of FadeOut phase for CH1
int CH1FadeOutStop = 512; //stop time of FadeOut phase for CH1
…
void setup() {
…
pinMode(3, OUTPUT);
…
}
…
void loop() {
cycleValue = cycleValue + 1;
// CHANNEL1
// Lets calculate the fadeStep related to duration of FadeIn and the cap percent
if (fadeAddCH1 == 0) {
fadeAddCH1 = ((255 * (float(fadePcCH1) / 100)) / (float(CH1FadeInStop) – float(CH1FadeInStart)));
//Serial.print(“fadeAddCH1: “);
//Serial.println(fadeAddCH1);
}
…
// Lets calculate the fadeStep related to duration of FadeOut and the cap percent
if (fadeSubCH1 == 0) {
// fadeSubCH1 = ((float(CH1FadeOutStop) – float(CH1FadeOutStart)) / 255) * (float(fadePcCH1) / 100);
fadeSubCH1 = ((255 * (float(fadePcCH1) / 100)) / (float(CH1FadeOutStop) – float(CH1FadeOutStart)));
//Serial.print(“fadeSubCH1: “);
//Serial.println(fadeSubCH1);
}
…
// FadeIn phase
if (cycleValue >= CH1FadeInStart && cycleValue < CH1FadeInStop) {
brightfloatCH1 = brightfloatCH1 + fadeAddCH1;
brightnessCH1 = int(brightfloatCH1);
}
...
// control Max brightness
brightnessCapCH1 = int(fadeAddCH1 * 255);
constrain(brightnessCH1, 0, brightnessCapCH1);
//Serial.print("brightnessCH1: ");
//Serial.println(brightnessCH1);
...
// FadeOut phase. Some extra cycles are useful to be sure to bring to 0 variables
if (cycleValue >= CH1FadeOutStart && cycleValue < (CH1FadeOutStop + 20)) {
brightfloatCH1 = brightfloatCH1 - fadeSubCH1;
brightnessCH1 = int(brightfloatCH1);
}
// brightness must be positive
if (brightnessCH1 < 0) {
brightnessCH1 = 0;
brightfloatCH1 = 0.0;
}
...
analogWrite(3, brightnessCH1);
if (cycleValue == 1024 ) {
cycleValue = 0;
}
// The delay must be only one
delay(cycledelay);
}
In this example Leds conntected to Channel 1 (PWN Pin 3);
- will have a maximum brightness of the 20 percent
- the FadeIn phase will start at step 1 and will end at step 256 (reaching the max brightness of the 20%)
- the FadeOut phase will start at step 257 (no constant phase) and will end at step 512 (reaching a zero value)
The code can be optimized a lot with for cycles and arrays instead of variables.. but it works and it’s easier to read.
This is the download link for the full sketch.
Have fun and please leave a comment if something is unclear.. :)
45 Comments to Arduino Nativity Set LED Light Controller – Led fading effects
Leave a comment
Categories
- Arduino (2)
- AskoziaPBX (6)
- cisco (1)
- Debian Lenny (6)
- general (1)
- Home server (6)
- Linux (6)
- m0n0wall (1)
- switching (1)
- voice (1)
- Voyage Linux (6)

great post, thanks for sharing
antidepressant drugs for cats…
Buy_generic drugs…
alberta alcohol and drug abuse commission…
Buy_generic meds…
drug detection testing…
Buy_generic meds…
recipes for gastric bypass patients…
Buy_now…
citalopram length of use…
Buy_drugs without prescription…
best life diet…
Buy_no prescription…
weight gain stories feeders…
Buy_drugs without prescription…
accutane birth defects…
Buy_it now…
menstrual patterns in menopause…
Buy_no prescription…
dependent disorder anxiety…
Buy_generic pills…
green nerf ds lite armour…
Buy_generic pills…
bmi chart for kids…
Buy_generic drugs…
herbal flee collars for dogs…
Buy_generic pills…
buy cheap clomid…
Buy_it now…
statistics on pregnancy in high school…
Buy_generic meds…
what is valtrex used for…
Buy_generic drugs…
cures for fingernail fungi…
Buy_generic meds…
symptoms of chocolate allergy…
Buy_it now…
deadliest type of cancer…
Buy_generic pills…
bacterial staphylococcus aureus meningitis symptoms…
Buy_without prescription…
genie francis weight loss…
Buy_now…
st joseph aspirin coupon…
Buy_generic pills…
persistent nausea and stomach pain…
Buy_generic meds…
cordarone intravenous…
Buy_generic meds…
respiratory infection humidity dry antibiotics avelox…
Buy_generic pills…
find clomid cheap in us…
Buy_now…
hot spots or cancer…
Buy_without prescription…
south beach diet foods to avoid…
Buy_no prescription…
what causes constant itching…
Buy_generic pills…
washington university marfan losartan study…
Buy_drugs without prescription…
clinical trial ediary data…
Buy_now it…
juliet’s on the spot acne treatment…
Buy_drugs without prescription…
altitude and pregnancy…
Buy_it now…
glucophage powered by vbulletin version 2.2.1…
Buy_generic pills…
neurontin and mood lability…
Buy_generic meds…
menses while on birth control pills…
Buy_generic drugs…
medical nebulizer…
Buy_generic meds…
can iodine help thyroid function…
Buy_generic drugs…
breven medication for add…
Buy_generic drugs…
drug citalopram side effects…
Buy_generic drugs…
information about treatment for leg edema…
Buy_generic drugs…
air purifiers for allergies…
Buy_generic meds…
dogs to detect cancer…
Buy_generic drugs…
azathioprine@average.cost” rel=”nofollow”>..…
Buynow…