#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN D5
#define PIXEL_COUNT 64
Adafruit_NeoPixel RGB_matrix = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Colour library ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
uint8_t palette [17][3] = {
{0, 0, 0}, // 0. Black USED
{255, 0, 0}, // 1. Red USED
{0, 255, 0}, // 2. Green
{0, 0, 255}, // 3. Blue USED
{255, 255, 255}, // 4. White USED
{255, 0, 255}, // 5. Purple
{255, 255, 0}, // 6. Lemon
{50, 255, 255}, // 7. Aqua USED
{255, 222, 30}, // 8. Gold USED
{255, 75, 0}, // 9. Amber USED
{255, 50, 0}, // 10. Orange USED
{255, 0, 30}, // 11. Pink USED
{0, 255, 120}, // 12. Teal
{255, 0, 8}, // 13. Magenta USED
{0, 255, 40}, // 14. Jade
{255, 175, 0}, // 15. Yellow USED
{0,255,255} // 16. Cyan
};
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Candle fire ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
int flame_FrameCount = 4;
int flame_Delays = 200;
int flame [4][8][8] = {
// Frame 1
{{0, 0, 1, 0, 0, 0, 0, 0 },
{0, 0, 1, 1, 0, 0, 0, 1 },
{0, 0, 1, 10, 1, 0, 0, 0 },
{0, 1, 1, 10, 10, 1, 0, 0 },
{0, 1, 10, 9, 10, 1, 1, 0 },
{0, 1, 10, 9, 9, 10, 1, 0 },
{0, 1, 10, 10, 10, 1, 0, 0 },
{0, 0, 1, 1, 1, 1, 0, 0 }},
// Frame 3
{{0, 0, 0, 0, 0, 0, 0, 0 },
{1, 0, 0, 0, 1, 0, 0, 0 },
{0, 0, 0, 1, 10, 1, 0, 0 },
{0, 0, 1, 10, 10, 1, 0, 0 },
{0, 1, 1, 10, 9, 1, 1, 0 },
{0, 1, 10, 9, 9, 10, 1, 0 },
{0, 1, 10, 10, 10, 10, 1, 0 },
{0, 0, 1, 1, 1, 1, 0, 0 }},
// Frame 2
{{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 1, 0, 0, 0, 1 },
{0, 0, 1, 10, 1, 0, 0, 0 },
{0, 0, 1, 10, 10, 1, 0, 0 },
{0, 1, 1, 9, 10, 1, 1, 0 },
{0, 1, 10, 9, 9, 10, 1, 0 },
{0, 1, 10, 10, 10, 10, 1, 0 },
{0, 0, 1, 1, 1, 1, 0, 0 }},
// Frame 4
{{1, 0, 0, 0, 0, 1, 0, 0 },
{0, 0, 0, 0, 1, 1, 0, 0 },
{0, 0, 0, 1, 10, 1, 0, 0 },
{0, 0, 1, 10, 9, 10, 1, 0 },
{0, 1, 1, 10, 9, 10, 1, 0 },
{0, 1, 10, 9, 9, 10, 1, 0 },
{0, 0, 1, 10, 10, 10, 1, 0 },
{0, 0, 1, 1, 1, 1, 0, 0 }}
};
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Empty screen ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
int NA_FrameCount = 1;
int NA_delays = 1000;
int NA [1][8][8] = {
// Frame 1
{{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 }}};
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Animation / Colour demo ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
int colorDemo_FrameCount = 1;
int colorDemo[1][8][8] = {
{{1, 2, 3, 4, 5, 6, 7, 8 },
{9, 10, 11, 12, 13, 14, 15, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 }}};
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Animation / Heartbeat ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
int heartbeat_FrameCount = 5;
int heartbeat_Delays = 250;
int heartbeat_filled [5][8][8] = {
// Frame 1
{{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }},
// Frame 2
{{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 11, 11, 1, 1, 0 },
{ 0, 1, 1, 11, 11, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }},
// Frame 3
{{ 0, 1, 1, 0, 0, 1, 1, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 11, 1, 1, 11, 1, 1 },
{ 1, 1, 11, 11, 11, 11, 1, 1 },
{ 1, 1, 11, 11, 11, 11, 1, 1 },
{ 0, 1, 1, 11, 11, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 }},
// Frame 4
{{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 11, 11, 1, 1, 0 },
{ 0, 1, 1, 11, 11, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }},
// Frame 5
{{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }}
};
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Check encoder function ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
#define CLK D7
#define DT D6
#define SW D2
int brightness_range [11] = {0,1,5,10,15,25,50,75,100,150,255};
int counter = 3;
int counter_max = 10;
bool currentStateCLK;
bool lastStateCLK;
bool currentStateSW;
bool lasStateSW = 1;
int mode = 0;
int mode_max = 2;
void check_encoder () {
currentStateCLK = digitalRead(CLK);
// If last and current CLK are different, a pulse occurred / React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
if (digitalRead(DT) != currentStateCLK) {
counter++;
if (counter > counter_max) {counter = counter_max;}
}
else {
counter--;
if (counter < 0) {counter = 0;}
}
RGB_matrix.setBrightness(brightness_range[counter]);
}
lastStateCLK = currentStateCLK; // Remember last CLK state
// Read the button state
currentStateSW = digitalRead(SW);
if (currentStateSW ==0 && lasStateSW == 1){
mode = mode + 1;
if (mode > mode_max) {mode = 0;}
}
if (currentStateSW != lasStateSW){lasStateSW = currentStateSW;}
Serial.print(digitalRead(CLK));
Serial.print(" ");
Serial.print(digitalRead(DT));
Serial.print(" ");
Serial.print(digitalRead(SW));
Serial.println(" ");
}
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| "Display" funciton ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
unsigned long lastMillis = 0;
void DisplayAnimation (int InputAnimation[][8][8], int FrameCount = 1, int delays = 200) {
for (int Frame = 0; Frame < FrameCount; Frame++){
for (int Y=0; Y<8; Y++) {
for (int X=0; X<8; X++) {
uint32_t colour = RGB_matrix.Color(palette[InputAnimation[Frame][Y][X]][0], palette[InputAnimation[Frame][Y][X]][1], palette[InputAnimation[Frame][Y][X]][2]);
RGB_matrix.setPixelColor(Y*8+X, colour);
}
}
RGB_matrix.show();
while (millis() - lastMillis <= delays) {
check_encoder ();
delay(10);
}
lastMillis = millis();
}
}
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Branching funciton ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
void branching_function () {
if (mode == 0) {DisplayAnimation(heartbeat_filled, heartbeat_FrameCount, heartbeat_Delays);}
else if (mode == 1) {DisplayAnimation(flame, flame_FrameCount, flame_Delays);}
else if (mode == 2) {DisplayAnimation(colorDemo, colorDemo_FrameCount);}
/* |||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||| Main body ||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||| */
void setup() {
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
Serial.begin(9600);
// Matrix display initiation
RGB_matrix.begin();
RGB_matrix.setBrightness(brightness_range[counter]);
DisplayAnimation(NA, NA_FrameCount);
delay(100);
}
void loop() {branching_function ();}
Comments