int latchPin = 8; // SR Latch Pin 12 Digital pin
int dataPin = 11; // SR Pin 14 PWM pin
int clockPin = 12; // SR Clock Pin 11 Digital pin
unsigned int Var = 0;
int numOfRegisters = 4;
byte* registerState;
void regWrite(int pin, bool state){
//Determines register
int reg = ( pin / 8 ) + 1;
//Determines pin for actual register
// int actualPin = pin - (8 * reg);
int actualPin = ( pin % 8 );
Serial.print("reg: ");
Serial.print(reg);
Serial.print("\n");
Serial.print("pin: ");
Serial.print(pin);
Serial.print("\n");
//Begin session
digitalWrite(latchPin, LOW);
for (int i = numOfRegisters; i > 0; i--) {
//Get actual states for register
byte* states = ®isterState[i];
//Update state
if (i == reg){
bitWrite(*states, actualPin, state);
}
//Write
shiftOut(dataPin, clockPin, MSBFIRST, *states);
delay(100);
}
//End session
digitalWrite(latchPin, HIGH);
}
void setup() {
Serial.begin(9600);
//Initialize array
registerState = new byte[numOfRegisters];
for (size_t i = 0; i < numOfRegisters; i++) {
registerState[i] = 0;
}
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
for (int k = 0; k < 32; k++){
regWrite(k, HIGH);
Serial.print("k: ");
Serial.print(k);
Serial.print("\n");
delay(500);
regWrite(k, LOW);
}
}
void loop() {
}
//Determines register
int reg = ( pin / 8 ) + 1;
//Determines pin for actual register
// int actualPin = pin - (8 * reg);
int actualPin = ( pin % 8 );
The register starts at 1 not 0 for some reason. The k counter is 0 to 31 for the LEDs.