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.. :)

Tags: ,

Monday, November 29th, 2010 Arduino

45 Comments to Arduino Nativity Set LED Light Controller – Led fading effects

  1. great post, thanks for sharing

  2. Daniel on December 17th, 2010
  3. antidepressant drugs for cats

    Buy_generic drugs…

  4. JOHNNY on October 18th, 2011
  5. EDWIN on October 18th, 2011
  6. drug detection testing

    Buy_generic meds…

  7. MILTON on October 19th, 2011
  8. TIMOTHY on October 20th, 2011
  9. citalopram length of use

    Buy_drugs without prescription…

  10. ZACHARY on October 20th, 2011
  11. best life diet

    Buy_no prescription…

  12. LESLIE on October 21st, 2011
  13. weight gain stories feeders

    Buy_drugs without prescription…

  14. HERMAN on October 21st, 2011
  15. accutane birth defects

    Buy_it now…

  16. LEO on October 22nd, 2011
  17. menstrual patterns in menopause

    Buy_no prescription…

  18. HENRY on October 23rd, 2011
  19. dependent disorder anxiety

    Buy_generic pills…

  20. HUBERT on October 24th, 2011
  21. green nerf ds lite armour

    Buy_generic pills…

  22. ALFRED on October 24th, 2011
  23. bmi chart for kids

    Buy_generic drugs…

  24. PERRY on October 26th, 2011
  25. herbal flee collars for dogs

    Buy_generic pills…

  26. RANDY on October 26th, 2011
  27. buy cheap clomid

    Buy_it now…

  28. NICHOLAS on October 26th, 2011
  29. JOSHUA on October 27th, 2011
  30. what is valtrex used for

    Buy_generic drugs…

  31. GEORGE on October 28th, 2011
  32. cures for fingernail fungi

    Buy_generic meds…

  33. BEN on October 30th, 2011
  34. FERNANDO on October 30th, 2011
  35. deadliest type of cancer

    Buy_generic pills…

  36. MILTON on October 31st, 2011
  37. PETER on October 31st, 2011
  38. STEPHEN on October 31st, 2011
  39. st joseph aspirin coupon

    Buy_generic pills…

  40. KIRK on November 1st, 2011
  41. persistent nausea and stomach pain

    Buy_generic meds…

  42. WALLACE on November 2nd, 2011
  43. cordarone intravenous

    Buy_generic meds…

  44. ENRIQUE on November 2nd, 2011
  45. NELSON on November 2nd, 2011
  46. find clomid cheap in us

    Buy_now…

  47. JUSTIN on November 2nd, 2011
  48. hot spots or cancer

    Buy_without prescription…

  49. DALE on November 5th, 2011
  50. south beach diet foods to avoid

    Buy_no prescription…

  51. PETER on November 6th, 2011
  52. what causes constant itching

    Buy_generic pills…

  53. CHARLIE on November 6th, 2011
  54. washington university marfan losartan study

    Buy_drugs without prescription…

  55. JEFFREY on November 6th, 2011
  56. clinical trial ediary data

    Buy_now it…

  57. NORMAN on November 7th, 2011
  58. juliet’s on the spot acne treatment

    Buy_drugs without prescription…

  59. JERRY on November 8th, 2011
  60. altitude and pregnancy

    Buy_it now…

  61. BRANDON on November 9th, 2011
  62. MICHEAL on November 9th, 2011
  63. neurontin and mood lability

    Buy_generic meds…

  64. ANDRE on November 10th, 2011
  65. menses while on birth control pills

    Buy_generic drugs…

  66. TROY on November 10th, 2011
  67. medical nebulizer

    Buy_generic meds…

  68. DAN on November 11th, 2011
  69. can iodine help thyroid function

    Buy_generic drugs…

  70. LAWRENCE on November 13th, 2011
  71. breven medication for add

    Buy_generic drugs…

  72. JOSHUA on November 14th, 2011
  73. drug citalopram side effects

    Buy_generic drugs…

  74. STEVE on November 15th, 2011
  75. NELSON on November 16th, 2011
  76. air purifiers for allergies

    Buy_generic meds…

  77. JORGE on November 16th, 2011
  78. dogs to detect cancer

    Buy_generic drugs…

  79. LANCE on November 17th, 2011
  80. azathioprine@average.cost” rel=”nofollow”>..

    Buynow…

  81. BOBBY on December 10th, 2011

Leave a comment

 

Categories

AskoziaPBX

Create your full features PBX IP with voicemail and Automated Attendant with alix2d3 or alix2d2 system board.

You can find the PBX IP preconfigured image here:


And obtain all support information "how to" here: