/*
Arduino | hardware-help
Problem with my 74HC595
Nicolaselmaslo OP — 8/30/24 at 5:51 PM
Hello i got a problem with my 74HC595.
Im trying to do a Matrix of LEDs 5x5 with Arduino Uno.
I do it on a simulator called Tinkercad and it worked perfectly.
but when im trying to pass fisical almost all LEDs turn on when
they dont need. I dont know if is something about Code or Hardware.
I left my code Here(Im using also a Keypad that doesnt have any problem).
*/
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
const int NUM_GLYPHS = 15;
const int DATA_PIN = 10;
const int LATCH_PIN = 11;
const int CLOCK_PIN = 12;
const int LED25_PIN = 13;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4};
byte colPins[COLS] = {3, 2, A1, A0};
struct glyphs {
char g_name[16];
byte srData[3];
bool led25On = false;
};
glyphs gData[NUM_GLYPHS];
char displayNames[NUM_GLYPHS][16] = {
"Digit 0", "Digit 1", "Digit 2", "Digit 3", "Digit 4",
"Digit 5", "Digit 6", "Digit 7", "Digit 8", "Digit 9",
"Happy ", "Heart ", "Star ", "Square ", "Triangle"
};
int shiftVals[NUM_GLYPHS][3] = { // sr1, sr2, sr3
{0x72, 0x94, 0xa7}, {0x21, 0x08, 0x42}, {0xf8, 0x7f, 0x0f},
{0xf8, 0x5e, 0x1f}, {0x8c, 0x7e, 0x10}, {0xfc, 0x3e, 0x1f},
{0xfc, 0x3f, 0x1f}, {0x78, 0x44, 0x44}, {0xfc, 0x7f, 0x1f},
{0xfc, 0x7e, 0x10}, {0x8a, 0x81, 0x17}, {0xdd, 0x62, 0xa2},
{0x23, 0xbe, 0xe2}, {0xfc, 0x63, 0x1f}, {0x01, 0x15, 0xf0}
};
bool led25[NUM_GLYPHS] {
false, false, true, true, true,
true, true, false, true, true,
false, false, false, true, false
};
byte keyIndex = 0;
char keyInput[8];
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void clearKeyBuffer() {
memset(keyInput, '\0', 8);
keyIndex = 0;
}
void initGlyphs() {
for (int g = 0; g < NUM_GLYPHS; g++) {
strncpy(gData[g].g_name, displayNames[g], 16);
gData[g].srData[0] = shiftVals[g][0];
gData[g].srData[1] = shiftVals[g][1];
gData[g].srData[2] = shiftVals[g][2];
gData[g].led25On = led25[g];
}
}
void ledWrite(int sr1Value, int sr2Value, int sr3Value, bool lastLED) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, sr3Value);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, sr2Value);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, sr1Value);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LED25_PIN, lastLED);
}
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LED25_PIN, OUTPUT);
initGlyphs();
Serial.println("Enter a value between 0 and 14,");
Serial.println("press '#' when done.\n");
}
void loop() {
bool error = false;
char dispBuffer[48];
char key = keypad.getKey();
if (key != NO_KEY) {
if (key >= '0' && key <= '9') {
//Serial.print(key);
keyInput[keyIndex] = key;
keyIndex++;
} else if (key == '#') {
//Serial.println();
int gIndex = atoi(keyInput);
if (gIndex >= NUM_GLYPHS) {
error = true;
} else {
snprintf(dispBuffer, 48, "Entry %d: \t%s \t0x%x, 0x%x, 0x%x", gIndex,
gData[gIndex].g_name, gData[gIndex].srData[0],
gData[gIndex].srData[1], gData[gIndex].srData[2]);
//Serial.println(gIndex);
Serial.println(dispBuffer);
ledWrite(gData[gIndex].srData[0], gData[gIndex].srData[1],
gData[gIndex].srData[2], gData[gIndex].led25On);
clearKeyBuffer();
}
} else {
error = true;
}
}
if (error) {
Serial.println("Invalid entry");
ledWrite(0xa8, 0x88, 0x8a, true);
clearKeyBuffer();
}
}