#define DATA_PIN 8 // Pin connected to DS of 74HC595
#define LATCH_PIN 9 // Pin connected to STCP of 74HC595
#define CLOCK_PIN 10 // Pin connected to SHCP of 74HC595
uint8_t ndx = 0;
uint8_t ndx2 = 0;
uint8_t num = 0;
void updateShiftRegister()
{
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, num);
digitalWrite(LATCH_PIN, HIGH);
}
void increment () {
ndx2 = random(0, 8);
//Serial.print("ndx2 = ");
Serial.println(ndx2);
if (ndx2 == ndx) return;
if (ndx2 > ndx ) {
for (uint8_t i = ndx; i <= ndx2; i++) {
bitSet(num, ndx);
updateShiftRegister();
//Serial.print(num);
//Serial.print(" ");
if (i < ndx2) {
ndx ++;
}
delay(20);
}
//Serial.println();
//Serial.println(ndx);
}
else {
for (uint8_t i = ndx; i >= ndx2; i--) {
bitClear(num, ndx);
updateShiftRegister();
//Serial.print(num);
//Serial.print(" ");
if (ndx == 0) break;
if (ndx > ndx2) {
ndx --;
}
delay(20);
}
//Serial.println();
//Serial.println(ndx);
}
}
void setup() {
// put your setup code here, to run once:
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Hello World");
}
void loop() {
// put your main code here, to run repeatedly:
increment();
delay(100);
}