/*
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};
byte index = 0;
char keyInput[3];
char result[8];
struct glyphs {
char g_name[16];
int sr3Data;
int sr2Data;
int sr1Data;
bool led25On = false;
};
glyphs gDisplay[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 sr1Vals[NUM_GLYPHS] = {
0x72, 0x21, 0xf8, 0xf8, 0x8c,
0xfc, 0xfc, 0x78, 0xfc, 0xfc,
0x8a, 0xdd, 0x23, 0xfc, 0x01
};
int sr2Vals[NUM_GLYPHS] = {
0x94, 0x08, 0x7f, 0x5e, 0x7e,
0x3e, 0x3f, 0x44, 0x7f, 0x7e,
0x81, 0x62, 0xbe, 0x63, 0x15
};
int sr3Vals[NUM_GLYPHS] = {
0xa7, 0x42, 0x0f, 0x1f, 0x10,
0x1f, 0x1f, 0x44, 0x1f, 0x10,
0x17, 0xa2, 0xe2, 0x1f, 0xf0
};
bool led25[NUM_GLYPHS] {
false, false, true, true, true,
true, true, false, true, true,
false, false, false, true, false
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void initGlyphs() {
for (int g = 0; g < NUM_GLYPHS; g++) {
strncpy(gDisplay[g].g_name, displayNames[g], 16);
gDisplay[g].sr3Data = sr3Vals[g];
gDisplay[g].sr2Data = sr2Vals[g];
gDisplay[g].sr1Data = sr1Vals[g];
gDisplay[g].led25On = led25[g];
}
}
void ledWrite(int sr3Value, int sr2Value, int sr1Value, 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 clearKeyBuffer() {
memset(result, '\0', 8);
index = 0;
}
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LED25_PIN, OUTPUT);
initGlyphs();
}
void loop() {
bool error = false;
char dispBuffer[48];
char key = keypad.getKey();
if (key != NO_KEY) {
if (key >= '0' && key <= '9') {
//Serial.print(key);
result[index] = key;
index++;
} else if (key == '#') {
int dIndex = atoi(result);
if (dIndex >= NUM_GLYPHS) error = true;
//Serial.println(dIndex);
snprintf(dispBuffer, 48, "Entry %d: %s 0x%x, 0x%x, 0x%x", dIndex,
gDisplay[dIndex].g_name, gDisplay[dIndex].sr3Data,
gDisplay[dIndex].sr2Data, gDisplay[dIndex].sr1Data);
Serial.println(dispBuffer);
ledWrite(gDisplay[dIndex].sr3Data, gDisplay[dIndex].sr2Data, gDisplay[dIndex].sr1Data, gDisplay[dIndex].led25On);
clearKeyBuffer();
} else {
error = true;
//index = 0;
}
}
if (error) {
Serial.println("Invalid entry");
ledWrite(0xa8, 0x88, 0x8a, true);
clearKeyBuffer();
}
//}
}
/*
char TECLA = keypad.getKey();
if (TECLA) {
keyInput[index] = TECLA;
index++;
Serial.print(TECLA);
}
if (index == 2) {
if (!strcmp(keyInput, CLAVE_NUMBER0)) {
ledWrite(0xa7, 0x94, 0x72);
Serial.println("Number 0 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER1)) {
ledWrite(0x42, 0x8, 0x21);
Serial.println("Number 1 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER2)) {
ledWrite(0x0f, 0x7f, 0xf8);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 2 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER3)) {
ledWrite(0x1f, 0x5e, 0xf8);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 3 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER4)) {
ledWrite(0x10, 0x7e, 0x8c);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 4 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER5)) {
ledWrite(0x1f, 0x3e, 0xfc);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 5 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER6)) {
ledWrite(0x1f, 0x3f, 0xfc);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 6 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER7)) {
ledWrite(0x44, 0x44, 0x78);
Serial.println("Number 7 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER8)) {
ledWrite(0x1f, 0x7f, 0xfc);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 8 Printed");
}
else if (!strcmp(keyInput, CLAVE_NUMBER9)) {
ledWrite(0x10, 0x7e, 0xfc);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Number 9 Printed");
}
else if (!strcmp(keyInput, CLAVE_HAPPY)) {
ledWrite(0x17, 0x81, 0x8a);
//digitalWrite(LED25_PIN, HIGH);
Serial.println("Happy Face Printed");
}
else if (!strcmp(keyInput, CLAVE_HEART)) {
ledWrite(0xa2, 0x62, 0xdd);
Serial.println("Heart Printed");
}
else if (!strcmp(keyInput, CLAVE_STAR)) {
ledWrite(0xe2, 0xbe, 0x23);
Serial.println("Star Printed");
}
else if (!strcmp(keyInput, CLAVE_SQUARE)) {
ledWrite(0x1f, 0x63, 0xfc);
digitalWrite(LED25_PIN, HIGH);
Serial.println("Square Printed");
}
else if (!strcmp(keyInput, CLAVE_TRIANGLE)) {
ledWrite(0xf0, 0x15, 0x1);
Serial.println("Triangle Printed");
}
else if (!strcmp(keyInput, CLAVE_RESET)) {
Serial.println("RESET");
index = 0;
}
else {
ledWrite(0xa8, 0x88, 0x8a);
digitalWrite(LED25_PIN, HIGH);
Serial.println("INCORRECT");
index = 0;
}
}
}
*/