// Flash LED trying to emulate an incandescent bulb for Multiple LEDs, but not flashing in sync // Ver 0.1 // variable for which LED is presently be processed byte LED_Working; // Array to define output pin numbers int LED_Pin[6] = {3, 5, 6, 9, 10, 11}; // Variable Array for when to toggle LED unsigned long LED_Tog_Time[7]; /* Constants for Pulse Width Modulation (PWM) percentage used in analogWrite() PX where x is percent of max max is 255 Thus P5 is 5% of 255, or about 13 */ const byte P0 = 0, P5 = 13, P10 = 25, P20 = 51, P30 = 76, P40 = 102, P50 = 127, P60 = 153, P70 = 178, P80 = 204, P90 = 229, P100 = 255; //Arrays to hold the PWM values and the Delay values (in milliseconds) used to create incandescent bulb look from LED byte LED_PWM[11] = {P0, P30, P60, P100, (P70 + P5), P70, P60, P50, P30, (P10 + P5), P10}; unsigned long LED_Delay[11] = {1000, 20, 30, 800, 30, 30, 30, 35, 35, 35, 15}; /* LED_Phase Array is used to keep track of which PWM and Delay are to be used. The LED_Phase number points to which PWM and Delay to use by being an index into LED_PWM and LED_Delay Arrays */ byte LED_Phase[6] = {0, 0, 0, 0, 0, 0}; unsigned long LED_Async_Delay[6] = {0, 13, 19, 23, 29, 37}; // unique per LED delay to create async flashing effect // Variable for Time readings and calculations unsigned long Time_Reading; void setup() { // put your setup code here, to run once: pinMode(LED_Pin[0], OUTPUT), (LED_Pin[1], OUTPUT), (LED_Pin[2], OUTPUT), (LED_Pin[3], OUTPUT), (LED_Pin[4], OUTPUT), (LED_Pin[5], OUTPUT); Time_Reading = millis(); // get an initial time // write 0 to all LEDs so they are off LED_Working = 0; do { analogWrite (LED_Pin[LED_Working], 0); // Turn LED off LED_Tog_Time[LED_Working] = Time_Reading + 1000; // Set next LED phase to start 1000 milliseconds (1sec) from present time ++ LED_Working; // Increment to next LED } while (LED_Working < 6); LED_Working = 0; //point to first LED for start of looping } void loop() { // put your main code here, to run repeatedly: Time_Reading = millis(); // Get the present time do { if (Time_Reading > LED_Tog_Time[LED_Working]) // if present time greater than next phase time, process next phase { analogWrite (LED_Pin[LED_Working], LED_PWM[LED_Phase[LED_Working]]); // write PWM value LED_Tog_Time[LED_Working] = Time_Reading + LED_Delay[LED_Phase[LED_Working]]; // set up next phase change time LED_Phase[LED_Working] = LED_Phase[LED_Working] + 1; // bump to next phase if (LED_Phase[LED_Working] > 10) // If phase exceeds last phase ==> { LED_Phase[LED_Working] = 0; // bump back to phase 0 LED_Tog_Time[LED_Working] = Time_Reading + LED_Async_Delay[LED_Working]; // Add in extra delay to achieve Asyncronous look } } ++ LED_Working; // increment to point to next LED } while (LED_Working < 6); LED_Working = 0; //point to first LED for start of looping }