Hi, I've recently been making a 24x5 led matrix with 74hc595's. But, the code I built has functions that have to run for every column of some text. Therefore making the process longer if the text gets bigger.
Here's my code:
/*
24 x 5 led matrix
Version 2.2
by: ArduMasterPro44
*/
#define Data1 3
#define Clock1 4
#define Data2 5
#define Clock2 6
#define Latch 7
uint8_t alphabet[95][4] = {
{ 3, 0x00, 0x00, 0x00 }, // " "
{ 1, 0x17, 0x00, 0x00 }, // "!"
{ 3, 0x03, 0x00, 0x03 }, // """
{ 3, 0x1F, 0x0A, 0x1F }, // "#"
{ 3, 0x0B, 0x1F, 0x0D }, // "$"
{ 3, 0x19, 0x04, 0x13 }, // "%"
{ 3, 0x0A, 0x15, 0x1A }, // "&"
{ 1, 0x03, 0x00, 0x00 }, // "'"
{ 2, 0x0E, 0x11, 0x00 }, // "("
{ 2, 0x11, 0x0E, 0x00 }, // ")"
{ 3, 0x05, 0x02, 0x05 }, // "*"
{ 3, 0x04, 0x0E, 0x04 }, // "+"
{ 1, 0x18, 0x00, 0x00 }, // ","
{ 3, 0x04, 0x04, 0x04 }, // "-"
{ 1, 0x10, 0x00, 0x00 }, // "."
{ 3, 0x18, 0x04, 0x03 }, // "/"
{ 3, 0x1F, 0x11, 0x1F }, // "0"
{ 3, 0x12, 0x1F, 0x10 }, // "1"
{ 3, 0x19, 0x15, 0x12 }, // "2"
{ 3, 0x11, 0x15, 0x1F }, // "3"
{ 3, 0x07, 0x04, 0x1F }, // "4"
{ 3, 0x17, 0x15, 0x1D }, // "5"
{ 3, 0x1E, 0x15, 0x1D }, // "6"
{ 3, 0x01, 0x01, 0x1F }, // "7"
{ 3, 0x1F, 0x15, 0x1F }, // "8"
{ 3, 0x17, 0x15, 0x1F }, // "9"
{ 1, 0x0A, 0x00, 0x00 }, // ":"
{ 1, 0x1A, 0x00, 0x00 }, // ";"
{ 3, 0x04, 0x0A, 0x11 }, // "<"
{ 3, 0x0A, 0x0A, 0x0A }, // "="
{ 3, 0x11, 0x0A, 0x04 }, // ">"
{ 3, 0x01, 0x15, 0x03 }, // "?"
{ 3, 0x0E, 0x11, 0x17 }, // "@"
{ 3, 0x1F, 0x05, 0x1F }, // "A"
{ 3, 0x1F, 0x15, 0x1B }, // "B"
{ 3, 0x1F, 0x11, 0x11 }, // "C"
{ 3, 0x1F, 0x11, 0x0E }, // "D"
{ 3, 0x1F, 0x15, 0x11 }, // "E"
{ 3, 0x1F, 0x05, 0x01 }, // "F"
{ 3, 0x1F, 0x11, 0x1D }, // "G"
{ 3, 0x1F, 0x04, 0x1F }, // "H"
{ 3, 0x11, 0x1F, 0x11 }, // "I"
{ 3, 0x18, 0x10, 0x1F }, // "J"
{ 3, 0x1F, 0x04, 0x1B }, // "K"
{ 3, 0x1F, 0x10, 0x10 }, // "L"
{ 3, 0x1F, 0x06, 0x1F }, // "M"
{ 3, 0x1F, 0x01, 0x1F }, // "N"
{ 3, 0x1F, 0x11, 0x1F }, // "O"
{ 3, 0x1F, 0x05, 0x07 }, // "P"
{ 3, 0x0F, 0x19, 0x0F }, // "Q"
{ 3, 0x1F, 0x05, 0x1A }, // "R"
{ 3, 0x17, 0x15, 0x1D }, // "S"
{ 3, 0x01, 0x1F, 0x01 }, // "T"
{ 3, 0x1F, 0x10, 0x1F }, // "U"
{ 3, 0x0F, 0x10, 0x1F }, // "V"
{ 3, 0x1F, 0x0C, 0x1F }, // "W"
{ 3, 0x1B, 0x04, 0x1B }, // "X"
{ 3, 0x07, 0x1C, 0x07 }, // "Y"
{ 3, 0x19, 0x15, 0x13 }, // "Z"
{ 2, 0x1F, 0x11, 0x00 }, // "["
{ 3, 0x03, 0x04, 0x18 }, // "\"
{ 2, 0x11, 0x1F, 0x00 }, // "]"
{ 3, 0x02, 0x01, 0x02 }, // "^"
{ 3, 0x10, 0x10, 0x10 }, // "_"
{ 2, 0x01, 0x02, 0x00 }, // "`"
{ 3, 0x0c, 0x12, 0x1E }, // "a"
{ 3, 0x1F, 0x12, 0x0C }, // "b"
{ 3, 0x1E, 0x12, 0x12 }, // "c"
{ 3, 0x0C, 0x12, 0x1F }, // "d"
{ 3, 0x0C, 0x1A, 0x14 }, // "e"
{ 3, 0x04, 0x1F, 0x05 }, // "f"
{ 3, 0x12, 0x15, 0x0E }, // "g"
{ 3, 0x1F, 0x04, 0x18 }, // "h"
{ 1, 0x1D, 0x00, 0x00 }, // "i"
{ 2, 0x10, 0x0D, 0x00 }, // "j"
{ 3, 0x1F, 0x04, 0x1A }, // "k"
{ 1, 0x1F, 0x00, 0x00 }, // "l"
{ 3, 0x1E, 0x04, 0x1E }, // "m"
{ 3, 0x1E, 0x02, 0x1E }, // "n"
{ 3, 0x1E, 0x12, 0x1E }, // "o"
{ 3, 0x1F, 0x09, 0x06 }, // "p"
{ 3, 0x06, 0x09, 0x1F }, // "q"
{ 3, 0x1E, 0x02, 0x04 }, // "r"
{ 3, 0x14, 0x16, 0x0A }, // "s"
{ 3, 0x02, 0x0F, 0x12 }, // "t"
{ 3, 0x1E, 0x10, 0x1E }, // "u"
{ 3, 0x0E, 0x10, 0x0E }, // "v"
{ 3, 0x1E, 0x08, 0x1E }, // "w"
{ 3, 0x1A, 0x04, 0x1A }, // "x"
{ 3, 0x17, 0x14, 0x0F }, // "y"
{ 3, 0x1A, 0x12, 0x16 }, // "z"
{ 3, 0x04, 0x1F, 0x11 }, // "{"
{ 1, 0x1F, 0x00, 0x00 }, // "|"
{ 3, 0x11, 0x1F, 0x04 }, // "}"
{ 3, 0x04, 0x0C, 0x08 } // "~"
};
const uint16_t delayTime = 100;
const uint8_t rows = 24;
const uint8_t columns = 5;
uint32_t time;
char message[] = "This is a test string";
void setup() {
pinMode(Data1, OUTPUT);
pinMode(Clock1, OUTPUT);
pinMode(Data2, OUTPUT);
pinMode(Clock2, OUTPUT);
pinMode(Latch, OUTPUT);
}
void loop() {
int8_t i = -24;
while (i < sizeOfString(message)) {
if (millis() - time >= delayTime) {
i++;
time = millis();
}
shiftText(message, i, rows, columns, delayTime);
}
}
void shiftText(char msg[], int16_t idx, uint8_t bitsX, uint8_t bitsY, uint16_t del) {
for (uint8_t i = 0; i < bitsX; i++) {
shiftByte(Data1, Data2, Clock1, Clock2, Latch, MSBFIRST, LSBFIRST, i, stringToChar(msg, i + idx), bitsX, bitsY); // Shift the byte to the matrix
}
}
uint8_t stringToChar(char str[], int16_t bit) { // Converts one peice of the character according to the bit variable
if (bit >= sizeOfString(message)) {
return 0x00;
} else if (bit < 0) {
return 0x00;
}
uint8_t bitMap[sizeOfString(message)] = { 0 }; // makes an array of the message in 3x5 font
uint8_t x = 0; // Keeps track of the X-Axis bit
for (uint8_t i = 0; i < strlen(str); i++) { // Loop for every character in the message
for (uint8_t j = 0; j < alphabet[str[i] - 32][0]; j++) { // Loop for every byte in the character
bitMap[x] = alphabet[str[i] - 32][j + 1]; // Store the byte in the bitMap[] array
x++;
}
if (i < strlen(str) - 1) { // Check if it isn't the last character of the message
bitMap[x] = 0; // Store a 0x00 byte in the bitMap[] array
x++;
}
}
return bitMap[bit];
}
uint8_t sizeOfString(char str[]) { // Returns the size of the string if it were to be made of the 3x5 font
uint8_t size = 0;
for (uint8_t i = 0; i < strlen(str); i++) { // Loop for every character in the message
size += alphabet[str[i] - 32][0];
if (i < strlen(str) - 1) { // Check if it isn't the last character of the message
size++;
}
}
return size;
}
void shiftByte(uint8_t data1, uint8_t data2, uint8_t clock1, uint8_t clock2, uint8_t latch, uint8_t bitOrderX, uint8_t bitOrderY, uint8_t pin, uint32_t val, uint8_t bitsX, uint8_t bitsY) {
digitalWrite(latch, LOW);
shiftBits(data1, clock1, bitOrderX, bit(pin), bitsX);
shiftBits(data2, clock2, bitOrderY, ~val, bitsY);
digitalWrite(latch, HIGH);
}
void shiftBits(uint8_t data, uint8_t clock, uint8_t bitOrder, uint32_t value, uint8_t bits) {
for (uint8_t i = 0; i < bits; i++) {
if (bitOrder == LSBFIRST) {
digitalWrite(data, value & 1);
value >>= 1;
} else if (bitOrder == MSBFIRST) {
digitalWrite(data, (value & bit(bits - 1)) != 0);
value <<= 1;
}
digitalWrite(clock, HIGH);
digitalWrite(clock, LOW);
}
}
Here's the schematic:
(Ignore the potentiometer, It's for later in my project)
I would greatly appreciate your help!
─ ArduMasterPro44 ─