Title / Description
Code int firefly[][3] = { { 9,10,11 } , { 3,5,6 } }; int num_fireflies = 0; int firefly1[] = {9,10,11}; int firefly2[] = {3,5,6}; int max_color = 300; void setup() { Serial.begin(9600); num_fireflies = sizeof(firefly) / sizeof(int) / 3; Serial.println("initializing..."); Serial.print("num_fireflies:"); Serial.print(num_fireflies); Serial.print("\n"); for(int x = 0; x < num_fireflies; x++) { for(int y = 0; y < 3; y++) { pinMode(firefly[x][y], OUTPUT); } } test_pattern(); } void loop() { int firefly_delay = random(2000,1000); // only fire one every 2-10 seconds for(int x = 0; x < firefly_delay; x++) { delay(1); } int firefly = random(0,num_fireflies-1); // pick a firefly at random unsigned char firefly_color[3]; boolean color_ok = false; // generate colors at random until we get one that's below the brightness threshold while(!color_ok) { firefly_color[0] = random(0,255); firefly_color[1] = random(0,255); firefly_color[2] = random(0,255); color_ok = check_color(firefly_color); } /* blinking the firefly : my basic concept is that fireflies fade up at different speeds, then hold that glow for a random period of time, then fade down at a speed quicker then they faded up. also, on occasion - they get real bright in the middle briefly. */ fade_up_color(firefly_color, firefly[firefly], random(1,25)); delay(random(1,15)); fade_down_color(firefly_color,firefly1, random(1,10)); } void set_color(unsigned char color[], int firefly_pins[]) { for(int x = 0; x < 3; x++) { analogWrite(firefly_pins[x], 255-color[x]); } } void set_color_on_all(unsigned char color[]) { for(int x = 0; x < 3; x++) { analogWrite(firefly1[x], 255-color[x]); analogWrite(firefly2[x], 255-color[x]); } } void fade_up_color(unsigned char color[], int firefly_pins[], int delay_duration) { unsigned char greatest_value = get_greatest_value(color); for(int x = 0; x <= greatest_value; x++) { if(color[0] >= x) { analogWrite(firefly_pins[0], 255-x); } if(color[1] >= x) { analogWrite(firefly_pins[1], 255-x); } if(color[2] >= x) { analogWrite(firefly_pins[2], 255-x); } delay(delay_duration); } } void fade_down_color(unsigned char color[], int firefly_pins[], int delay_duration) { unsigned char greatest_value = get_greatest_value(color); unsigned char current_red_value = color[0]; unsigned char current_green_value = color[1]; unsigned char current_blue_value = color[2]; for(int x = greatest_value; x >= 0; x--) { if(current_red_value > 0) { current_red_value--; analogWrite(firefly_pins[0], 255-current_red_value); } if(current_blue_value > 0) { current_blue_value--; analogWrite(firefly_pins[1], 255-current_blue_value); } if(current_green_value > 0) { current_green_value--; analogWrite(firefly_pins[2], 255-current_green_value); } delay(delay_duration); } } void fade_down_color_on_all(unsigned char color[], int delay_duration) { unsigned char greatest_value = get_greatest_value(color); unsigned char current_red_value = color[0]; unsigned char current_green_value = color[1]; unsigned char current_blue_value = color[2]; for(int x = greatest_value; x >= 0; x--) { if(current_red_value > 0) { current_red_value--; analogWrite(firefly1[0], 255-current_red_value); analogWrite(firefly2[0], 255-current_red_value); } if(current_blue_value > 0) { current_blue_value--; analogWrite(firefly1[1], 255-current_blue_value); analogWrite(firefly2[1], 255-current_blue_value); } if(current_green_value > 0) { current_green_value--; analogWrite(firefly1[2], 255-current_green_value); analogWrite(firefly2[2], 255-current_green_value); } delay(delay_duration); } } unsigned char get_greatest_value(unsigned char values[]) { unsigned char greatest_value = 0; if(values[0] > greatest_value) { greatest_value = values[0]; } if(values[1] > greatest_value) { greatest_value = values[1]; } if(values[2] > greatest_value) { greatest_value = values[2]; } return greatest_value; } boolean check_color(unsigned char color[]) { if((color[0] + color[1] + color[2]) > max_color) { return false; } else { return true; } } void test_pattern() { unsigned char color[] = { 0,0,0 }; int d = 300; int d2 = 100; set_color_on_all(color); delay(1000); delay(d2); color[0] = 255; color[1] = 255; color[2] = 255; set_color_on_all(color); delay(d2); color[0] = 0; color[1] = 0; color[2] = 0; set_color_on_all(color); delay(d2); color[0] = 255; color[1] = 255; color[2] = 255; set_color_on_all(color); delay(d2); color[0] = 0; color[1] = 0; color[2] = 0; set_color_on_all(color); delay(d2); color[0] = 255; color[1] = 255; color[2] = 255; set_color_on_all(color); delay(d2); color[0] = 0; color[1] = 0; color[2] = 0; set_color_on_all(color); delay(d); color[0] = 255; color[1] = 0; color[2] = 0; set_color_on_all(color); delay(d); color[0] = 0; color[1] = 255; color[2] = 0; set_color_on_all(color); delay(d); color[0] = 0; color[1] = 0; color[2] = 255; set_color_on_all(color); delay(d); color[0] = 255; color[1] = 255; color[2] = 255; set_color_on_all(color); delay(d); color[0] = 255; color[1] = 255; color[2] = 255; fade_down_color_on_all(color,5); }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code