Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
16x32 RGB LED Matrix counter+cpu% display
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#include <Fonts/Arial4pt7b.h>
#include <Fonts/Arial7pt7b.h>
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define LAT A3
#define OE 9
#define A A0
#define B A1
#define C A2
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, true);
void setup() {
Serial.begin(115200);
matrix.begin();
draw();
}
int level = 99;
uint16_t text_col = matrix.Color333(1,1,0);
char msg[32] = "ready";
int msg_len = -1;
void loop() {
// do nothing
if (Serial.available()) {
int c = Serial.read();
if (c == '\n') {
msg_len = -1;
}
else if (c == '#') {
while(!Serial.available());
int r = Serial.read() - '0';
while(!Serial.available());
int g = Serial.read() - '0';
while(!Serial.available());
int b = Serial.read() - '0';
text_col = matrix.Color333(r,g,b);
}
else if (c == '%') {
while(!Serial.available());
level = Serial.read() - '0';
while(!Serial.available());
level = level * 10 + Serial.read() - '0';
}
else if (c == '~') {
memset(msg, 0, 32);
msg_len = 0;
do {
while (!Serial.available());
c = Serial.read();
if (c == '\n')
break;
if (c == '\r')
continue;
msg[msg_len++] = c;
} while (msg_len < 31);
scrollMsg();
memset(msg, 0, 32);
msg_len = 0;
}
else if (msg_len < 32) {
if (msg_len == -1) {
memset(msg, 0, 32);
msg_len = 0;
}
msg[msg_len++] = c;
}
}
draw();
}
void draw()
{
static int offset;
matrix.fillScreen(0);
matrix.setFont(&Arial_Narrow7pt7b);
matrix.setCursor(0, 9);
matrix.setTextSize(1);
matrix.setTextColor(text_col);
matrix.print(msg);
uint16_t col = matrix.Color333(1, 1, 1);
matrix.drawPixel(29, 0, col);
matrix.drawPixel(29, 1, col);
matrix.drawPixel(29, 2, col);
matrix.drawPixel(30, 1, col);
matrix.drawPixel(31, 0, col);
matrix.drawPixel(29, 5, col);
matrix.drawPixel(30, 4, col);
matrix.drawPixel(31, 3, col);
matrix.drawPixel(29, 7, col);
matrix.drawPixel(29, 8, col);
matrix.drawPixel(29, 9, col);
matrix.drawPixel(30, 8, col);
matrix.drawPixel(31, 7, col);
matrix.drawPixel(31, 8, col);
matrix.drawPixel(31, 9, col);
int x;
for (x = -1; x < 32*level/100; x++) {
matrix.drawLine(x, 12, x+1, 15, matrix.ColorHSV(1536/96*x + offset/4, 255, 160, true));
}
matrix.drawLine(x, 12, x, 13, matrix.ColorHSV(1536/96*x + offset/4, 255, 160, true));
offset = (offset + 1) % (1536*4);
matrix.setFont(&Arial_Narrow4pt7b);
matrix.setTextColor(matrix.ColorHSV(1536/96*50 + offset, 128, 160, true));
matrix.setCursor(24, 15);
matrix.print(level);
matrix.swapBuffers(false);
}
void scrollMsg() {
matrix.setFont(&Arial_Narrow7pt7b);
matrix.setTextSize(1);
matrix.setTextColor(text_col);
matrix.setTextWrap(false);
for (int x = 32; matrix.getCursorX() > 0; x--) {
matrix.fillScreen(0);
matrix.setCursor(x, 10);
matrix.print(msg);
matrix.swapBuffers(false);
delay(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment