Zuletzt aktiv 7 months ago

Controle de Matriz de LED feita com Fita de LED WS2812B (usando biblioteca NeoPixel da Adafruit)

racerxdl's Avatar Lucas Teske hat die Gist bearbeitet 10 years ago. Zu Änderung gehen

1 file changed, 180 insertions

pattylove.ino(Datei erstellt)

@@ -0,0 +1,180 @@
1 + #include <Adafruit_NeoPixel.h>
2 + #define PIN 6
3 +
4 + Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
5 +
6 + uint32_t matrix[5][8];
7 + const float heartPeriod = 500;
8 + const int rainbowPeriod = 10;
9 + const float pattyLovePeriod = 200;
10 +
11 + byte img[5][64] = {
12 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
13 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
14 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
15 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
16 + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
17 + };
18 +
19 + int stateMachine = 0;
20 + int pattyLoveCycles = 0;
21 + bool pattyLoveLock = true;
22 + uint32_t lastTime = 0;
23 +
24 + void setup() {
25 + // put your setup code here, to run once:
26 +
27 + strip.begin();
28 + strip.show(); // Initialize all pixels to 'off'
29 + for (int i=0; i<40; i++) {
30 + strip.setPixelColor(i, 0);
31 + }
32 + clearMatrix();
33 + Serial.begin(115200);
34 + }
35 +
36 + inline byte breath(float period) {
37 + return (byte) (127 * (sin(2 * PI * (1.0/period) * (millis()- lastTime) / PI) + 1));
38 + }
39 +
40 + inline void clearMatrix() {
41 + for (int x=0;x<8;x++) {
42 + for (int y=0;y<5;y++) {
43 + matrix[y][x] = 0;
44 + }
45 + }
46 + }
47 +
48 + inline void refreshMatrix() {
49 + // 5x8
50 + for (int x=0;x<8;x++) {
51 + for (int y=0;y<5;y++) {
52 + setColor(x, y, matrix[y][x]);
53 + }
54 + }
55 + strip.show();
56 + }
57 +
58 + inline void setColor(uint8_t x, uint8_t y, uint32_t color) {
59 + strip.setPixelColor(x + y * 8, color);
60 + }
61 +
62 + inline uint32_t proportional(uint32_t value, uint32_t intensity) {
63 + return (intensity * value / 255) & 0xFF;
64 + }
65 +
66 + void doPattyLove() {
67 + byte pos = ((int)((millis()- lastTime) / pattyLovePeriod)) % (64 - 8);
68 + if (pos == 0 && !pattyLoveLock) {
69 + pattyLoveCycles++;
70 + pattyLoveLock = true;
71 + } else if (pos != 0) {
72 + pattyLoveLock = false;
73 + }
74 +
75 + for (int x=0; x<8; x++) {
76 + for (int y=0; y<5; y++) {
77 + matrix[y][x] = img[y][x+pos] ? Wheel((x+pos)*8, 64) : 0x000000;
78 + }
79 + }
80 + }
81 +
82 + void doHeart() {
83 + byte intensity = breath(heartPeriod);
84 + uint32_t pink = proportional(0xCC, intensity) << 16 | proportional(0x11, intensity);
85 +
86 + matrix[0][2] = pink;
87 + matrix[0][5] = pink;
88 +
89 + for (int i=1;i<7;i++) {
90 + matrix[1][i] = pink;
91 + }
92 +
93 + for (int i=1;i<7;i++) {
94 + matrix[2][i] = pink;
95 + }
96 +
97 + for (int i=1;i<6;i++) {
98 + matrix[2][i] = pink;
99 + }
100 +
101 + for (int i=2;i<6;i++) {
102 + matrix[3][i] = pink;
103 + }
104 +
105 + matrix[4][3] = pink;
106 + matrix[4][4] = pink;
107 + }
108 +
109 + void doRainbow() {
110 + byte pos = ((millis()- lastTime) / rainbowPeriod) & 255;
111 + for (int i=0; i< strip.numPixels(); i++) {
112 + matrix[i/8][i%8] = Wheel((i+pos) & 255, 32);
113 + }
114 + }
115 +
116 + uint32_t Wheel(byte WheelPos, byte intensity) {
117 + WheelPos = 255 - WheelPos;
118 + if(WheelPos < 85) {
119 + return strip.Color(proportional(255 - WheelPos * 3, intensity), 0, proportional(WheelPos * 3, intensity));
120 + }
121 + if(WheelPos < 170) {
122 + WheelPos -= 85;
123 + return strip.Color(0, proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity));
124 + }
125 + WheelPos -= 170;
126 + return strip.Color(proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity), 0);
127 + }
128 +
129 + void updateMachineState() {
130 + switch(stateMachine) {
131 + case 0:
132 + if (millis() - lastTime > 4600) {
133 + stateMachine = 1;
134 + lastTime = millis();
135 + pattyLoveCycles = 0;
136 + Serial.println("Patty Love!");
137 + }
138 + break;
139 + case 1:
140 + if (pattyLoveCycles == 1) {
141 + stateMachine = 2;
142 + lastTime = millis();
143 + Serial.println("Heart Only");
144 + }
145 + break;
146 + case 2:
147 + if (millis() - lastTime > 4000) {
148 + stateMachine = 3;
149 + lastTime = millis();
150 + Serial.println("Blank");
151 + clearMatrix();
152 + }
153 + case 3:
154 + if (millis() - lastTime > 5000) {
155 + stateMachine = 0;
156 + Serial.println("Heart BG");
157 + lastTime = millis();
158 + }
159 + break;
160 + }
161 + }
162 +
163 + void loop() {
164 + updateMachineState();
165 + switch (stateMachine) {
166 + case 0:
167 + doRainbow();
168 + doHeart();
169 + break;
170 + case 1:
171 + doPattyLove();
172 + break;
173 + case 2:
174 + doHeart();
175 + break;
176 + }
177 +
178 + refreshMatrix();
179 + delay(1);
180 + }
Neuer Älter