Son aktivite 1 month ago

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

Revizyon 7aef8237c5ab714f347c9e550c47b0b4f1273824

pattylove.ino Ham
1#include <Adafruit_NeoPixel.h>
2#define PIN 6
3
4Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
5
6uint32_t matrix[5][8];
7const float heartPeriod = 500;
8const int rainbowPeriod = 10;
9const float pattyLovePeriod = 200;
10
11byte 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
19int stateMachine = 0;
20int pattyLoveCycles = 0;
21bool pattyLoveLock = true;
22uint32_t lastTime = 0;
23
24void 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
36inline byte breath(float period) {
37 return (byte) (127 * (sin(2 * PI * (1.0/period) * (millis()- lastTime) / PI) + 1));
38}
39
40inline 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
48inline 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
58inline void setColor(uint8_t x, uint8_t y, uint32_t color) {
59 strip.setPixelColor(x + y * 8, color);
60}
61
62inline uint32_t proportional(uint32_t value, uint32_t intensity) {
63 return (intensity * value / 255) & 0xFF;
64}
65
66void 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
82void 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
109void 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
116uint32_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
129void 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
163void 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}
181