|
| 1 | +#include <Adafruit_NeoPixel.h> |
| 2 | + |
| 3 | +#define centerLED 29 |
| 4 | + |
| 5 | +int array[5] = {250,150,50,20,0}; |
| 6 | + |
| 7 | +int r = 0; |
| 8 | +int g = 0; |
| 9 | +int b = 0; |
| 10 | +int wait = 12; |
| 11 | +int test = 0; |
| 12 | +int posB = 0; |
| 13 | +int posR = 59; |
| 14 | +int posG = 23; |
| 15 | +boolean reverseB = false; |
| 16 | +boolean reverseR = false; |
| 17 | +boolean reverseG = true; |
| 18 | +boolean connected = false; |
| 19 | +boolean booted = false; |
| 20 | + |
| 21 | +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(60,7,NEO_GRBW + NEO_KHZ800); |
| 22 | + |
| 23 | +void setup () |
| 24 | +{ |
| 25 | + Serial.begin(4800); |
| 26 | + pixels.begin(); |
| 27 | + int amount = pixels.numPixels()*2/3; |
| 28 | +} |
| 29 | + |
| 30 | + void loop() |
| 31 | +{ |
| 32 | + if (booted == false){ |
| 33 | + posR = BootStripes(1,posR,reverseR); |
| 34 | + posG = BootStripes(2,posG,reverseG); |
| 35 | + posB = BootStripes(3,posB,reverseB); |
| 36 | + } |
| 37 | + |
| 38 | + if (Serial.available() > 3){ |
| 39 | + test = Serial.read(); |
| 40 | + booted = true; |
| 41 | + if(test == 255) |
| 42 | + Visualizer(); |
| 43 | + else if(test == 50) |
| 44 | + rainbowCycle(); |
| 45 | + else if(test == 49) |
| 46 | + ClearStrand(); |
| 47 | + } |
| 48 | + pixels.show(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + void rainbowCycle() { |
| 53 | + uint16_t i, j; |
| 54 | + |
| 55 | + for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel |
| 56 | + for(i=0; i< pixels.numPixels(); i++) { |
| 57 | + pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255)); |
| 58 | + if (Serial.available() > 3) |
| 59 | + return; |
| 60 | + } |
| 61 | + pixels.show(); |
| 62 | + delay(wait); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + void Visualizer(){ |
| 67 | + |
| 68 | + r= Serial.read(); |
| 69 | + g= Serial.read(); |
| 70 | + b= Serial.read(); |
| 71 | + |
| 72 | + for (int i = pixels.numPixels(); i > centerLED; i--){ |
| 73 | + uint32_t color = pixels.getPixelColor(i-1); |
| 74 | + pixels.setPixelColor(i, color); |
| 75 | + } |
| 76 | + for (int i = 0; i < centerLED; i++){ |
| 77 | + uint32_t color = pixels.getPixelColor(i+1); |
| 78 | + pixels.setPixelColor(i, color); |
| 79 | + } |
| 80 | + pixels.setPixelColor(centerLED, pixels.Color((int)r,(int)g,(int)b,0)); |
| 81 | + } |
| 82 | + |
| 83 | + int BootStripes(int sel,int pos, boolean& reverse){ |
| 84 | + for (int j = 0; j <5; j++){ |
| 85 | + uint32_t color = pixels.Color(0,0,array[j],0); |
| 86 | + pixels.setPixelColor(pos+j, AddColor(pixels.getPixelColor(pos+j),array[j],sel)); |
| 87 | + pixels.setPixelColor(pos-j, AddColor(pixels.getPixelColor(pos-j),array[j],sel)); |
| 88 | + } |
| 89 | + delay(wait); |
| 90 | + if (pos <= 0) |
| 91 | + reverse = true; |
| 92 | + if (pos >= pixels.numPixels()) |
| 93 | + reverse = false; |
| 94 | + if (reverse == false) |
| 95 | + pos--; |
| 96 | + else |
| 97 | + pos++; |
| 98 | + return pos; |
| 99 | + } |
| 100 | + |
| 101 | + void ClearStrand(){ |
| 102 | + for (int i = 0; i < pixels.numPixels(); i++) |
| 103 | + pixels.setPixelColor(i, pixels.Color(0,0,0,0)); |
| 104 | + } |
| 105 | + |
| 106 | + uint8_t GetColor(uint32_t color, int sel){ |
| 107 | + uint8_t result = 0; |
| 108 | + if (sel == 1) |
| 109 | + color = color<<8; |
| 110 | + if (sel == 2) |
| 111 | + color = color<<16; |
| 112 | + if (sel == 3) |
| 113 | + color = color<<24; |
| 114 | + result = color>>24; |
| 115 | + return result; |
| 116 | + } |
| 117 | +uint32_t AddColor(uint32_t base, uint8_t add, int sel){ |
| 118 | + return AddColor(base,add,sel,false); |
| 119 | +} |
| 120 | + |
| 121 | +uint32_t AddColor(uint32_t base, uint8_t add, int sel, boolean ammend){ |
| 122 | + uint32_t correction = 255; |
| 123 | + uint32_t addbig = (uint32_t)add; |
| 124 | + int shift[4] = {24,16,8,0}; |
| 125 | + if (ammend == false) |
| 126 | + base =(~(correction<<shift[sel]))&base; |
| 127 | + addbig = (uint32_t)add << shift[sel]; |
| 128 | + uint32_t result = base | addbig; |
| 129 | + return result; |
| 130 | +} |
| 131 | + |
| 132 | +uint32_t Wheel(byte WheelPos) { |
| 133 | + WheelPos = 255 - WheelPos; |
| 134 | + if(WheelPos < 85) { |
| 135 | + return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 136 | + } |
| 137 | + if(WheelPos < 170) { |
| 138 | + WheelPos -= 85; |
| 139 | + return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 140 | + } |
| 141 | + WheelPos -= 170; |
| 142 | + return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 143 | +} |
| 144 | + |
0 commit comments