/* Arduino Nativity Set LED Light Controller This sketck shows how to use Arduino sequencer for fading LED on PWN pins using the analogWrite() function. Circuit: - a ULN2003A Darlington Arrays http://www.networksoul.net/2010/11/arduino-nativity-set-led-light-controller-hardware/ This code is in the public domain. */ // Declarations, should not be edited 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 int brightnessCH2 = 0; // how bright the LED is int brightnessCapCH2 = 0; // the maximum value of brigther (0-255) float fadeAddCH2 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated float fadeSubCH2 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated float brightfloatCH2 = 0.0; // brightness in float int brightnessCH3 = 0; // how bright the LED is int brightnessCapCH3 = 0; // the maximum value of brigther (0-255) float fadeAddCH3 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated float fadeSubCH3 = 0.0; // how many points to fade the LED by. Leave 0 if should be autocalculated float brightfloatCH3 = 0.0; // brightness in float int cycleValue = 0; float delayvalue = 0; int cycledelay = 0; // 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 int fadePcCH2 = 5; // the maximum percent of Fade for CH2 int CH2FadeInStart = 513; // start time of FadeIn phase for CH2 int CH2FadeInStop = 700; // stop time of FadeIn phase for CH2 int CH2FadeOutStart = 710; //start time of FadeOut phase for CH2 int CH2FadeOutStop = 800; //stop time of FadeOut phase for CH2 int fadePcCH3 = 5; // the maximum percent of Fade for CH3 int CH3FadeInStart = 125; // start time of FadeIn phase for CH3 int CH3FadeInStop = 510; // stop time of FadeIn phase for CH3 int CH3FadeOutStart = 515; //start time of FadeOut phase for CH3 int CH3FadeOutStop = 800; //stop time of FadeOut phase for CH3 int programduration = 30; // light program duration in seconds void setup() { // initialize the serial port; needed for debugging below Serial.begin(9600); // declare pin 9 to be an output: pinMode(3, OUTPUT); // declare pin 6 to be an output: pinMode(5, OUTPUT); pinMode(6, OUTPUT); delayvalue = ((float(programduration) * 1000) / 1024); // main delay in ms calculated on program duration cycledelay = int(delayvalue); } 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; } // CHANNEL2 // Lets calculate the fadeStep related to duration of FadeIn and the cap percent if (fadeAddCH2 == 0) { fadeAddCH2 = ((255 * (float(fadePcCH2) / 100)) / (float(CH2FadeInStop) - float(CH2FadeInStart))); } // Lets calculate the fadeStep related to duration of FadeOut and the cap percent if (fadeSubCH2 == 0) { fadeSubCH2 = ((255 * (float(fadePcCH2) / 100)) / (float(CH2FadeOutStop) - float(CH2FadeOutStart))); } // FadeIn phase if (cycleValue >= CH2FadeInStart && cycleValue < CH2FadeInStop) { brightfloatCH2 = brightfloatCH2 + fadeAddCH2; brightnessCH2 = int(brightfloatCH2); } // control Max brightness brightnessCapCH2 = int(fadeAddCH2 * 255); constrain(brightnessCH2, 0, brightnessCapCH2); // FadeOut phase. Some extra cycles are useful to be sure to bring to 0 variables if (cycleValue >= CH2FadeOutStart && cycleValue < (CH2FadeOutStop + 20)) { brightfloatCH2 = brightfloatCH2 - fadeSubCH2; brightnessCH2 = int(brightfloatCH2); } // brightness must be positive if (brightnessCH2 < 0) { brightnessCH2 = 0; brightfloatCH2 = 0.0; } // CHANNEL3 // Lets calculate the fadeStep related to duration of FadeIn and the cap percent if (fadeAddCH3 == 0) { fadeAddCH3 = ((255 * (float(fadePcCH3) / 100)) / (float(CH3FadeInStop) - float(CH3FadeInStart))); } // Lets calculate the fadeStep related to duration of FadeOut and the cap percent if (fadeSubCH3 == 0) { fadeSubCH3 = ((255 * (float(fadePcCH3) / 100)) / (float(CH3FadeOutStop) - float(CH3FadeOutStart))); } // FadeIn phase if (cycleValue >= CH3FadeInStart && cycleValue < CH3FadeInStop) { brightfloatCH3 = brightfloatCH3 + fadeAddCH3; brightnessCH3 = int(brightfloatCH3); } // control Max brightness brightnessCapCH3 = int(fadeAddCH3 * 255); constrain(brightnessCH3, 0, brightnessCapCH3); // FadeOut phase. Some extra cycles are useful to be sure to bring to 0 variables if (cycleValue >= CH3FadeOutStart && cycleValue < (CH3FadeOutStop + 20)) { brightfloatCH3 = brightfloatCH3 - fadeSubCH3; brightnessCH3 = int(brightfloatCH3); } // brightness must be positive if (brightnessCH3 < 0) { brightnessCH3 = 0; brightfloatCH3 = 0.0; } // Write value to PINS analogWrite(3, brightnessCH1); analogWrite(5, brightnessCH2); analogWrite(6, brightnessCH3); if (cycleValue == 1024 ) { cycleValue = 0; } // The delay must be only one delay(cycledelay); }