test
C
code posted
by
bc
created at 19 Sep 06:56
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
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); } |
6.25 KB in 7 ms with coderay