Hi! I'm just starting to use shift registers - I bought some 74HC595 chips. My project is a Braille reading control system, and I'm currently using LEDs for testing. I've written some code, but it's not working quite right. I suspect the issue might be related to synchronization between the Latch, CLK, and Data pins.
The program works like this:
It asks for a letter from A to Z.
If the letter is lowercase, it converts it to uppercase.
It checks against the predefined char values I assigned earlier.
Then it sends the binary data to the Data pin, updating the CLK pin each time (at least I think it does).
Finally it updates the Latch pin to display the pattern on the LED matrix.
The problem is that the correct LEDs aren't lighting up properly.
Code:
// Definición de pines
const int dataPin = 1;
const int latchPin = 2;
const int clockPin = 3;
// Mapeo corregido de letras a patrones (ajustado al orden físico de tus LEDs)
const byte patronesLetras[26] = {
// Bits: LED8 LED7 LED6 LED5 LED4 LED3 LED2 LED1 (orden físico)
0b00000001, // A
0b00000011, // B
0b00100001, // C
0b01100001, // D
0b01000001, // E
0b00100011, // F
0b01100011, // G
0b01000011, // H
0b00100010, // I
0b01100010, // J
0b00000101, // K
0b00000111, // L
0b00100101, // M
0b01100101, // N
0b01000101, // O
0b00100111, // P
0b01100111, // Q
0b01000111, // R
0b00100110, // S
0b01100110, // T
0b10000101, // U
0b10000111, // V
0b11100010, // W
0b10100101, // X
0b11100101, // Y
0b11000101, // Z
};
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
Serial.println("Sistema listo. Ingrese una letra (A-Z):");
}
void loop() {
if (Serial.available()) {
char letra = toupper(Serial.read());
if (letra >= 'A' && letra <= 'Z') {
byte patron = patronesLetras[letra - 'A'];
Serial.print("Letra: ");
Serial.print(letra);
Serial.print(" - Patron: ");
imprimirBinario(patron);
Serial.println();
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, patron); // Cambiado a LSBFIRST
digitalWrite(latchPin, HIGH);
delay(2000);
escribirRegistro(0b00000000);
}
}
}
void imprimirBinario(byte valor) {
Serial.print("0b");
for (int i = 7; i >= 0; i--) {
Serial.print(bitRead(valor, i));
}
}
void escribirRegistro(byte dato) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, dato);
digitalWrite(latchPin, HIGH);
}
