pattylove.ino
· 4.6 KiB · Arduino
Raw
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
uint32_t matrix[5][8];
const float heartPeriod = 500;
const int rainbowPeriod = 10;
const float pattyLovePeriod = 200;
byte img[5][64] = {
{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},
{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},
{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},
{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},
{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}
};
int stateMachine = 0;
int pattyLoveCycles = 0;
bool pattyLoveLock = true;
uint32_t lastTime = 0;
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show(); // Initialize all pixels to 'off'
for (int i=0; i<40; i++) {
strip.setPixelColor(i, 0);
}
clearMatrix();
Serial.begin(115200);
}
inline byte breath(float period) {
return (byte) (127 * (sin(2 * PI * (1.0/period) * (millis()- lastTime) / PI) + 1));
}
inline void clearMatrix() {
for (int x=0;x<8;x++) {
for (int y=0;y<5;y++) {
matrix[y][x] = 0;
}
}
}
inline void refreshMatrix() {
// 5x8
for (int x=0;x<8;x++) {
for (int y=0;y<5;y++) {
setColor(x, y, matrix[y][x]);
}
}
strip.show();
}
inline void setColor(uint8_t x, uint8_t y, uint32_t color) {
strip.setPixelColor(x + y * 8, color);
}
inline uint32_t proportional(uint32_t value, uint32_t intensity) {
return (intensity * value / 255) & 0xFF;
}
void doPattyLove() {
byte pos = ((int)((millis()- lastTime) / pattyLovePeriod)) % (64 - 8);
if (pos == 0 && !pattyLoveLock) {
pattyLoveCycles++;
pattyLoveLock = true;
} else if (pos != 0) {
pattyLoveLock = false;
}
for (int x=0; x<8; x++) {
for (int y=0; y<5; y++) {
matrix[y][x] = img[y][x+pos] ? Wheel((x+pos)*8, 64) : 0x000000;
}
}
}
void doHeart() {
byte intensity = breath(heartPeriod);
uint32_t pink = proportional(0xCC, intensity) << 16 | proportional(0x11, intensity);
matrix[0][2] = pink;
matrix[0][5] = pink;
for (int i=1;i<7;i++) {
matrix[1][i] = pink;
}
for (int i=1;i<7;i++) {
matrix[2][i] = pink;
}
for (int i=1;i<6;i++) {
matrix[2][i] = pink;
}
for (int i=2;i<6;i++) {
matrix[3][i] = pink;
}
matrix[4][3] = pink;
matrix[4][4] = pink;
}
void doRainbow() {
byte pos = ((millis()- lastTime) / rainbowPeriod) & 255;
for (int i=0; i< strip.numPixels(); i++) {
matrix[i/8][i%8] = Wheel((i+pos) & 255, 32);
}
}
uint32_t Wheel(byte WheelPos, byte intensity) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(proportional(255 - WheelPos * 3, intensity), 0, proportional(WheelPos * 3, intensity));
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity));
}
WheelPos -= 170;
return strip.Color(proportional(WheelPos * 3, intensity), proportional(255 - WheelPos * 3, intensity), 0);
}
void updateMachineState() {
switch(stateMachine) {
case 0:
if (millis() - lastTime > 4600) {
stateMachine = 1;
lastTime = millis();
pattyLoveCycles = 0;
Serial.println("Patty Love!");
}
break;
case 1:
if (pattyLoveCycles == 1) {
stateMachine = 2;
lastTime = millis();
Serial.println("Heart Only");
}
break;
case 2:
if (millis() - lastTime > 4000) {
stateMachine = 3;
lastTime = millis();
Serial.println("Blank");
clearMatrix();
}
case 3:
if (millis() - lastTime > 5000) {
stateMachine = 0;
Serial.println("Heart BG");
lastTime = millis();
}
break;
}
}
void loop() {
updateMachineState();
switch (stateMachine) {
case 0:
doRainbow();
doHeart();
break;
case 1:
doPattyLove();
break;
case 2:
doHeart();
break;
}
refreshMatrix();
delay(1);
}
| 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, 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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 | } |
| 181 |