//https://github.com/rmorenojr/ElegooTutorial/blob/master/Lesson%2024%20-%2074HC595%20Shift%20Register/SR_74HC595_32bit/SR_74HC595_32bit.ino
const int tDelay = 100;
const int dataPin = 8;
const int latchPin = 9;
const int clockPin = 10;
bool DirectionState = 0;
void updateShiftRegister(unsigned long leds, bool isMSBFIRST = true){
unsigned int leds16 = int(leds);
unsigned int leds32 = int(leds>>16);
byte low16LED = lowByte(leds16);
byte high16LED = highByte(leds16);
byte low32LED = lowByte(leds32);
byte high32LED = highByte(leds32);
digitalWrite(latchPin, LOW);
if (isMSBFIRST == false) {
shiftOut(dataPin, clockPin, LSBFIRST, low16LED);
shiftOut(dataPin, clockPin, LSBFIRST, high16LED);
shiftOut(dataPin, clockPin, LSBFIRST, low32LED);
shiftOut(dataPin, clockPin, LSBFIRST, high32LED);
}
else {
shiftOut(dataPin, clockPin, MSBFIRST, high32LED);
shiftOut(dataPin, clockPin, MSBFIRST, low32LED);
shiftOut(dataPin, clockPin, MSBFIRST, high16LED);
shiftOut(dataPin, clockPin, MSBFIRST, low16LED);
}
digitalWrite(latchPin, HIGH);
}
void setup(){
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop(){
unsigned long leds = 0B11111111111111111111111111111111;
/* updateShiftRegister(leds);
delay(tDelay);
leds = 0B00000000000000000000000000000000;
updateShiftRegister(leds);
delay(tDelay);
leds = 0B11111111111111111111111111111111;
updateShiftRegister(leds);
delay(tDelay);*/
for (int i = 8; i < 32; i++){
leds = 0B00000000000000000000000000000000;
bitSet(leds, i);
updateShiftRegister(leds, DirectionState);
delay(tDelay);
}
for (int i = 32; i > 8; i--){
leds = 0B00000000000000000000000000000000;
bitSet(leds, i);
updateShiftRegister(leds, DirectionState);
delay(tDelay);
}
// DirectionState = !DirectionState;
}