Hello all,
I’m trying to turn on and off an LED, then go to the next LED in line and do the same, with a for loop going through all outputs. (Using solenoids for finished product but using LEDs here for simplicity) I am currently using 2 595 shift registers to control 16 LEDs.
It gets a bit confusing for me when trying to use bytes in arrays with multiple shift registers and this is what I have scrounged together, I’ve already looked through and kinda derived my code from this tutorial:
Just to clarify, MY CODE WORKS but I am wondering if there is an easier/shorter way to do this (maybe I will want to use more than 2 shift registers one day):
int clockPin = 2;
int latchPin = 3;
int dataPin = 4;
byte firstEightLEDs[] = {1, 2, 4, 8, 16, 32, 64, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0};
byte secondEightLEDs[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 16, 32, 64, 128, 0};
const int NUM_LED = 16;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
for (int i = 0; i < NUM_LED; i++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, firstEightLEDs[i]);
shiftOut(dataPin, clockPin, MSBFIRST, secondEightLEDs[i]);
digitalWrite(latchPin, HIGH);
delay(500);
//Array value 16 is 0 for both arrays... so it turns both off
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, firstEightLEDs[16]);
shiftOut(dataPin, clockPin, MSBFIRST, secondEightLEDs[16]);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
Just trying to pick your brains to see if I can understand this topic a bit better,
Much Appreciated