You commented out the first line of the array
So now your array no longer has 41 entries so this for loop is addressing elements which do not exist
If you want to avoid this type of mistake you can have the compiler count the number of elements
const byte dataPin = 11;
const byte clockPin = 12;
const byte latchPin = 8;
struct LED {
byte reg;
byte bit;
};
const LED leds[] = {
{2,2}, {2,3}, {2,4}, {2,5}, {2,6},
{2,7}, {4,0}, {4,1}, {4,2}, {4,3},
{4,4}, {4,5}, {1,2}, {1,3}, {4,6},
{4,7}, {3,0}, {3,1}, {1,4}, {1,5},
{1,6}, {1,7}, {3,2}, {3,3}, {3,4},
{3,5}, {0,0}, {0,1}, {0,2}, {0,3},
{3,6}, {3,7}, {2,0}, {2,1}, {0,4},
{0,5}
};
const byte ledCnt = sizeof leds / sizeof leds[0];
byte shadow[5] = {0,0,0,0,0};
void setLED(byte pinNumber, bool state) {
LED l = leds[pinNumber];
if(state) shadow[l.reg] |= 1 << l.bit;
else shadow[l.reg] &= ~1 << l.bit;
digitalWrite(latchPin, LOW);
for(byte i = 0; i < 5; i++) shiftOut(dataPin, clockPin, MSBFIRST, shadow[i]);
digitalWrite(latchPin, HIGH);
}
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
for(byte i = 0; i < ledCnt; i++) {
setLED(i, HIGH);
delay(100);
setLED(i, LOW);
delay(30);
}
}
Or you can use a range for statement and iterate through all the LED elements in the array using a reference
const byte dataPin = 11;
const byte clockPin = 12;
const byte latchPin = 8;
struct LED {
byte reg;
byte bit;
};
const LED leds[] = {
{2,2}, {2,3}, {2,4}, {2,5}, {2,6},
{2,7}, {4,0}, {4,1}, {4,2}, {4,3},
{4,4}, {4,5}, {1,2}, {1,3}, {4,6},
{4,7}, {3,0}, {3,1}, {1,4}, {1,5},
{1,6}, {1,7}, {3,2}, {3,3}, {3,4},
{3,5}, {0,0}, {0,1}, {0,2}, {0,3},
{3,6}, {3,7}, {2,0}, {2,1}, {0,4},
{0,5}
};
byte shadow[5] = {0,0,0,0,0};
void setLED(const LED &led, bool state) {
if(state) shadow[led.reg] |= 1 << led.bit;
else shadow[led.reg] &= ~1 << led.bit;
digitalWrite(latchPin, LOW);
for(byte i = 0; i < 5; i++) shiftOut(dataPin, clockPin, MSBFIRST, shadow[i]);
digitalWrite(latchPin, HIGH);
}
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
for(const LED &led : leds) {
setLED(led, HIGH);
delay(100);
setLED(led, LOW);
delay(30);
}
}
Note that because I maintain a shadow array of the state of each led, you can turn on or off leds individually without turning off all the others.
By the way since both values in the struct are less than 7 you could use a bit field and now each entry in the array is only 1 byte. Pretty compact.
const byte dataPin = 11;
const byte clockPin = 12;
const byte latchPin = 8;
struct LED {
byte reg : 3;
byte bit : 3;
};
const LED leds[] = {
{2,2}, {2,3}, {2,4}, {2,5}, {2,6},
{2,7}, {4,0}, {4,1}, {4,2}, {4,3},
{4,4}, {4,5}, {1,2}, {1,3}, {4,6},
{4,7}, {3,0}, {3,1}, {1,4}, {1,5},
{1,6}, {1,7}, {3,2}, {3,3}, {3,4},
{3,5}, {0,0}, {0,1}, {0,2}, {0,3},
{3,6}, {3,7}, {2,0}, {2,1}, {0,4},
{0,5}
};
byte shadow[5] = {0,0,0,0,0};
void setLED(const LED &led, bool state) {
if(state) shadow[led.reg] |= 1 << led.bit;
else shadow[led.reg] &= ~1 << led.bit;
digitalWrite(latchPin, LOW);
for(byte i = 0; i < 5; i++) shiftOut(dataPin, clockPin, MSBFIRST, shadow[i]);
digitalWrite(latchPin, HIGH);
}
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
for(const LED &led : leds) {
setLED(led, HIGH);
delay(100);
setLED(led, LOW);
delay(30);
}
}
And you can make it a bit less readable If you store the register index in the upper nibble and the bit position in the lower nibble, each LED fits naturally into a single hex byte. Then the LED table is very compact to write and read.
Now the LED list is just raw hex values like 0x23, meaning register 2, bit 3. It’s denser, but still very explicit if you remember the encoding rule.
Here’s how it looks:
const byte dataPin = 11;
const byte clockPin = 12;
const byte latchPin = 8;
constexpr byte leds[] = {
0x22, 0x23, 0x24, 0x25, 0x26,
0x27, 0x40, 0x41, 0x42, 0x43,
0x44, 0x45, 0x12, 0x13, 0x46,
0x47, 0x30, 0x31, 0x14, 0x15,
0x16, 0x17, 0x32, 0x33, 0x34,
0x35, 0x00, 0x01, 0x02, 0x03,
0x36, 0x37, 0x20, 0x21, 0x04,
0x05
};
byte shadow[5] = {0,0,0,0,0};
void setLED(byte code, bool state) {
byte reg = code >> 4;
byte bit = code & 0x0F;
if(state) shadow[reg] |= 1 << bit;
else shadow[reg] &= ~(1 << bit);
digitalWrite(latchPin, LOW);
for(byte i = 0; i < 5; i++) shiftOut(dataPin, clockPin, MSBFIRST, shadow[i]);
digitalWrite(latchPin, HIGH);
}
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
for(byte code : leds) {
setLED(code, HIGH);
delay(100);
setLED(code, LOW);
delay(30);
}
}