Components and supplies
1
Breadboard (generic)
1
Jumper wires (generic)
1
Arduino UNO
1
LED (generic)
Project description
Code
leds_lighting_effects.ino
c_cpp
1/* LEDs lighting effect sketch 2 * Created 2017 - March - 25 3 * By : Hesham Mohamed (LehKeda) 4 * 5 */ 6 7/* Global variables */ 8 9/* LEDs array . For proper fading effect you 10 * should attach your LEDs to PWM digital output 11 * PWM output pins are (3,5,6,9,10,11) in UNO board 12 * the array should be terminated with 0 value ; 13*/ 14int led[] = {9,10,11,0}; 15 16int led_array_lenth; 17int functions_array_lenth; 18 19/* LEDs lighting effect functions */ 20// get lenth of leds array 21void get_led_array_lenth(){ 22 for(int i=0; led[i];i++){ 23 led_array_lenth++; 24 } 25 } 26 27// To fade in/out each LED individually 28void fade_individual(){ 29 for (int i=0;i <led_array_lenth ; i++){ 30 // Fade in from min to max in increments of 5 points: 31 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 32 // sets the value (range from 0 to 255): 33 analogWrite(led[i], fadeValue); 34 // wait for 30 milliseconds to see the dimming effect 35 delay(30); 36 } 37 38 // fade out from max to min in increments of 5 points: 39 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 40 // sets the value (range from 0 to 255): 41 analogWrite(led[i], fadeValue); 42 // wait for 30 milliseconds to see the fading in effect 43 delay(30); 44 } 45 } 46 } 47 48// To fade in/out all LEDs at once . 49void fade_all(){ 50 for(int i=0; i<led_array_lenth; i++){ 51 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 52 53 // sets the fade value for all LEDs (range from 0 to 255): 54 for(int x=0; x<led_array_lenth; x++){ 55 analogWrite(led[x], fadeValue); 56 } 57 58 // wait for 30 milliseconds to see the dimming effect 59 delay(35); 60 } 61 62 // fade out from max to min in increments of 5 points: 63 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 64 65 // sets the fade value for all LEDs (range from 0 to 255): 66 for(int x=0; x<led_array_lenth; x++){ 67 analogWrite(led[x], fadeValue); 68 } 69 70 // wait for 30 milliseconds to see the fading in effect 71 delay(35); 72 } 73 } 74} 75 76// To randomly turn on LEDs 77void random_individual_led(){ 78 randomSeed(analogRead(0)); // To shuffle random function 79 for(int i=0; i<=led_array_lenth; i++){ 80 /* In order to make all LEDs trun on the max value 81 * for random function should be as same as LEDs array lenth 82 * because random function returns MAX-1 . 83 */ 84 int current_led=random(led_array_lenth); 85 86 // 255 value makes LED on all time . 87 analogWrite(led[current_led],255); 88 89 // Delay before turn off the LED / Max time the LED will be on . 90 delay(200); 91 92 // turn off LED 93 analogWrite(led[current_led],0); 94 } 95 } 96 97// To randomly light group of LEDs 98 void random_led_group(){ 99 randomSeed(analogRead(0)); // To shuffle random function 100 for (int i=0; i <=led_array_lenth; i++ ){ 101 // LEDs will be lit up ; 102 int current_leds[led_array_lenth]; 103 104 // Pointer to current_leds array , will be used to set LEDs will be lit up ; 105 int *ptr=current_leds; 106 107 // Number of LEDs will be lit 108 int number_of_leds= random(led_array_lenth); 109 110 for (int x=0; x < number_of_leds; x++){ 111 *ptr=led[random(led_array_lenth)]; // set LED will be lit up ; 112 113 // 255 value makes LED on all time . 114 analogWrite(*ptr,255); 115 116 ++ptr; // go to next item on current_leds array ; 117 } 118 119 // Delay before turn off the LED / Max time the LED will be on . 120 delay(200); 121 122 // turn off all LEDs have been lit 123 for (int y=0; current_leds[y]; y++){ 124 analogWrite(current_leds[y],0); 125 } 126 } 127 } 128 129/* Array of all LEDs lighting effect 130 * Add the name of function of effect 131 * you want or you can remove the name 132 * of function of effect you don't want 133 */ 134 135void (*leds_lighting_effects_functions[])()={fade_all,fade_individual, random_individual_led,random_led_group,0}; 136 137void get_functions_array_lenth(){ 138 for(int i=0; leds_lighting_effects_functions[i];i++){ 139 functions_array_lenth++; 140 } 141 } 142 143// the setup function runs once when you press reset or power the board 144void setup() { 145 146 // First we need to ge the lenth of LEDs array and functions array 147 get_led_array_lenth(); 148 get_functions_array_lenth(); 149} 150 151// the loop function runs over and over again forever 152void loop() { 153 randomSeed(analogRead(0)); 154 // Here we call functions respective to which effects we want 155 for (int i=0; leds_lighting_effects_functions[i]; i++){ 156 leds_lighting_effects_functions[random(functions_array_lenth)](); 157 } 158} 159 160
leds_lighting_effects.ino
c_cpp
1/* LEDs lighting effect sketch 2 * Created 2017 - March - 25 3 * By : Hesham Mohamed (LehKeda) 4 * 5 */ 6 7/* Global variables */ 8 9/* LEDs array . For proper fading effect you 10 * should attach your LEDs to PWM digital output 11 * PWM output pins are (3,5,6,9,10,11) in UNO board 12 * the array should be terminated with 0 value ; 13*/ 14int led[] = {9,10,11,0}; 15 16int led_array_lenth; 17int functions_array_lenth; 18 19/* LEDs lighting effect functions */ 20// get lenth of leds array 21void get_led_array_lenth(){ 22 for(int i=0; led[i];i++){ 23 led_array_lenth++; 24 } 25 } 26 27// To fade in/out each LED individually 28void fade_individual(){ 29 for (int i=0;i <led_array_lenth ; i++){ 30 // Fade in from min to max in increments of 5 points: 31 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 32 // sets the value (range from 0 to 255): 33 analogWrite(led[i], fadeValue); 34 // wait for 30 milliseconds to see the dimming effect 35 delay(30); 36 } 37 38 // fade out from max to min in increments of 5 points: 39 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 40 // sets the value (range from 0 to 255): 41 analogWrite(led[i], fadeValue); 42 // wait for 30 milliseconds to see the fading in effect 43 delay(30); 44 } 45 } 46 } 47 48// To fade in/out all LEDs at once . 49void fade_all(){ 50 for(int i=0; i<led_array_lenth; i++){ 51 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 52 53 // sets the fade value for all LEDs (range from 0 to 255): 54 for(int x=0; x<led_array_lenth; x++){ 55 analogWrite(led[x], fadeValue); 56 } 57 58 // wait for 30 milliseconds to see the dimming effect 59 delay(35); 60 } 61 62 // fade out from max to min in increments of 5 points: 63 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 64 65 // sets the fade value for all LEDs (range from 0 to 255): 66 for(int x=0; x<led_array_lenth; x++){ 67 analogWrite(led[x], fadeValue); 68 } 69 70 // wait for 30 milliseconds to see the fading in effect 71 delay(35); 72 } 73 } 74} 75 76// To randomly turn on LEDs 77void random_individual_led(){ 78 randomSeed(analogRead(0)); // To shuffle random function 79 for(int i=0; i<=led_array_lenth; i++){ 80 /* In order to make all LEDs trun on the max value 81 * for random function should be as same as LEDs array lenth 82 * because random function returns MAX-1 . 83 */ 84 int current_led=random(led_array_lenth); 85 86 // 255 value makes LED on all time . 87 analogWrite(led[current_led],255); 88 89 // Delay before turn off the LED / Max time the LED will be on . 90 delay(200); 91 92 // turn off LED 93 analogWrite(led[current_led],0); 94 } 95 } 96 97// To randomly light group of LEDs 98 void random_led_group(){ 99 randomSeed(analogRead(0)); // To shuffle random function 100 for (int i=0; i <=led_array_lenth; i++ ){ 101 // LEDs will be lit up ; 102 int current_leds[led_array_lenth]; 103 104 // Pointer to current_leds array , will be used to set LEDs will be lit up ; 105 int *ptr=current_leds; 106 107 // Number of LEDs will be lit 108 int number_of_leds= random(led_array_lenth); 109 110 for (int x=0; x < number_of_leds; x++){ 111 *ptr=led[random(led_array_lenth)]; // set LED will be lit up ; 112 113 // 255 value makes LED on all time . 114 analogWrite(*ptr,255); 115 116 ++ptr; // go to next item on current_leds array ; 117 } 118 119 // Delay before turn off the LED / Max time the LED will be on . 120 delay(200); 121 122 // turn off all LEDs have been lit 123 for (int y=0; current_leds[y]; y++){ 124 analogWrite(current_leds[y],0); 125 } 126 } 127 } 128 129/* Array of all LEDs lighting effect 130 * Add the name of function of effect 131 * you want or you can remove the name 132 * of function of effect you don't want 133 */ 134 135void (*leds_lighting_effects_functions[])()={fade_all,fade_individual, random_individual_led,random_led_group,0}; 136 137void get_functions_array_lenth(){ 138 for(int i=0; leds_lighting_effects_functions[i];i++){ 139 functions_array_lenth++; 140 } 141 } 142 143// the setup function runs once when you press reset or power the board 144void setup() { 145 146 // First we need to ge the lenth of LEDs array and functions array 147 get_led_array_lenth(); 148 get_functions_array_lenth(); 149} 150 151// the loop function runs over and over again forever 152void loop() { 153 randomSeed(analogRead(0)); 154 // Here we call functions respective to which effects we want 155 for (int i=0; leds_lighting_effects_functions[i]; i++){ 156 leds_lighting_effects_functions[random(functions_array_lenth)](); 157 } 158} 159 160
Project github repository
it's a github repository for the project
Project github repository
it's a github repository for the project
Downloadable files
fritzing project file
fritzing project file
My github repository
It's my github repository , all you need to do it just press on "clone or download" then "download ZIP" .
https://github.com/Lehkeda/Arduino_LEDs-lighting-effects
fritzing project file
fritzing project file
Comments
Only logged in users can leave comments