#include #include #include #include /* 7f (green) ff (red) and 00 blue is pretty yellow. */ #define MAX_YELLOW { 0x7f, 0xff, 0x00 } #define INIT_YELLOW { 0x3f, 0x7f, 0x00 } /* reference colors; max acts as a cap */ uint8_t max_yellow[3] = MAX_YELLOW; uint8_t init_yellow[3] = INIT_YELLOW; /* Pixels array for blitting */ Pixels pixels; Pixels off_pixels = { { 0 } }; #define CPLAY_LIGHTSENSOR A5 /* setup initializes the neopixel blitter and sets an initial color (off or medium) based on light value. One could decide just to set it to off and let it ramp up. */ void setup() { uint8_t i; /* TODO: set CPLAY_LS as input pin. */ uint16_t light = 0; uint8_t *initial; init_serial_stdio(); pinMode(CPLAY_LIGHTSENSOR, INPUT); light = analogRead(CPLAY_LIGHTSENSOR); initial = ((light > 200) ? &off_pixels[0][0] : &init_yellow[0]); init_neopixel_blit(); for(i=0;i<10;i++) { memcpy(&pixels[i], &initial, sizeof(init_yellow)); } neopixel_blit(pixels); } void loop() { uint16_t light = analogRead(CPLAY_LIGHTSENSOR); uint8_t newcolor[3]; printf("light %d\n", light); if(light < 100 || light > 300) { uint8_t i; if (light < 100) { /* this is slightly buggy, if max_yellow were 0xff, overflow would bring it down to zero. */ /* but since max_yellow isn't, it's ok */ newcolor[0] = min(pixels[0][0] + 1, max_yellow[0]); newcolor[1] = min(pixels[0][1] + 2, max_yellow[1]); newcolor[2] = min(pixels[0][2] + 0, max_yellow[2]); } if (light > 300) { /* this side is not buggy, at least not in the same way. */ newcolor[0] = pixels[0][0] > 1 ? pixels[0][0] - 1 : 0; newcolor[1] = pixels[0][1] > 2 ? pixels[0][1] - 2 : 0; newcolor[2] = pixels[0][2] > 2 ? pixels[0][2] - 2 : 0; } for(i=0;i<10;i++) { memcpy(&pixels[i], &newcolor, sizeof(init_yellow)); neopixel_blit(pixels); delay(100); } } neopixel_blit(pixels); /* might reduce this delay */ delay(1000); }