I was using this code with a teensy lc but had to swap it for a teensy 4.0 when I accidentally damaged it. Since then, the shift registers don't seem to shift.
I have tried using a separate shift register and have tested the pins on the teensy. The teensy pins are responding as expected, but the shift register only lights up one pin and regardless of what pin I select, it's always the same one that lights (pin 6).
Before I dig deeper, does anyone know of any compatibility issues between these the teensy 4.0 and 74HC595n shift registers, or anything else I might have missed?
Tia!
I have tried using a separate shift register and have tested the pins on the teensy. The teensy pins are responding as expected, but the shift register only lights up one pin and regardless of what pin I select, it's always the same one that lights (pin 6).
Before I dig deeper, does anyone know of any compatibility issues between these the teensy 4.0 and 74HC595n shift registers, or anything else I might have missed?
Tia!
Code:
/*
This is a simplified version of code i've been developing for a midi instrument. This section is for controlling 48 leds via 6 74HC595N shift registers.
I have removed the rest of the code for simplicity/readability.
This code worked on a teensy LC but doesn't work now on a teensy 4.0. I had to replace the LC after it stopped working. I was soldering some piezos onto
the board holding the teensy with the teensy switched on when it seemed to stop responding. I assume this i has something to do with why it stopped working.
I am now testing this code on a single shift register and have noticed that one of the eight leds lights up, but no matter what led[] i choose, it's
always the same one that lights up. It's as if the shift register is not shifting the data along...any suggestions?
*/
///////////////////////////Shift Registers///////////////////
int led[48];
const int outputEnable = 4; // green
const int dataPin = 6; // SER yellow
const int latchPin = 7; // RCLK blue
const int clockPin = 8; // SRCLK white
const int numRegisters = 6;
const int numLedsPerRegister = 8;
const int numLeds = numRegisters * numLedsPerRegister;
byte ledStates[numRegisters] = { 0 };
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(outputEnable, OUTPUT);
digitalWrite(outputEnable, HIGH);
updateShiftRegisters();
}
void loop() {
setLedState(led[2], HIGH); //no matter what led[?] goes here, the same slot on the shift register lights up...
delay(100);
setLedState(led[2], LOW);
delay(100);
}
///////////////////////////LED Functions//////////////////////////
void updateShiftRegisters() {
digitalWrite(outputEnable, HIGH); // Disable output while updating
for (int reg = numRegisters - 1; reg >= 0; reg--) {
digitalWrite(latchPin, LOW); // Activate the latch to start sending data
for (int bit = numLedsPerRegister - 1; bit >= 0; bit--) { // Shift out the bits for each LED
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, (ledStates[reg] >> bit) & 1); // Send the data to the shift register
digitalWrite(clockPin, HIGH);
}
delay(1);
digitalWrite(latchPin, HIGH); // Deactivate the latch to update the LEDs
}
digitalWrite(outputEnable, LOW); // Enable output after updating
}
void setLedState(int ledIndex, int brightness) {
if (ledIndex >= 0 && ledIndex < numRegisters * numLedsPerRegister) {
int reg = ledIndex / numLedsPerRegister;
int bit = ledIndex % numLedsPerRegister;
ledStates[reg] = (ledStates[reg] & ~(1 << bit)) | ((brightness > 0) << bit); // Set the brightness value to the corresponding bit in the array
updateShiftRegisters();
}
}
/////////////////////////////////////////////////////////////////