// https://forum.arduino.cc/t/water-show-with-timing/1207999

#include <Adafruit_NeoPixel.h> // ALTO777

const int dataPin = 2;   // DS
const int latchPin = 3;  // STCP
const int clockPin = 4;  // SHCP

#define NUM_SHIFT_REG 4
const int numRegisterPins = NUM_SHIFT_REG * 8;
bool registers[numRegisterPins];

byte pattern[] = {0b11000011, 0b10101010, 0b11110000, 0b11111111, 0b100000000, 0b00000001};
byte p = sizeof(pattern) / sizeof(pattern[0]);
int delayTime = 100;

#define NUM_LEDS numRegisterPins
#define LED_PIN 7
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();

  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

  clearRegisters();
}

void loop() {
  // showPattern(random(p));
  // firstLast();
  randomrelay();
  // cascade(); // one at a time
}

void randomrelay() {
  int i = random(32);
  int j = random(2);

  setRegisterPin(i, j);
  writeRegisters();
  delay(100);
  clearRegisters();
}

void cascade() {
  for (int i = 0; i < numRegisterPins; i++) {
    setRegisterPin(i, 1);
    writeRegisters();
    delay(delayTime);
    setRegisterPin(i, 0);
    writeRegisters();
  }
}

void firstLast() {
  setRegisterPin(0, 1); // set first relay
  setRegisterPin(31, 1); // set last relay
  writeRegisters(); // display
}

void showPattern(byte pat) {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, pattern[pat]);
  digitalWrite(latchPin, HIGH);
  delay(500);
  pattern[pat] = ~pattern[pat]; // Invert the pattern
}

void setRegisterPin(int index, int value) {
  // Set an individual pin HIGH or LOW
  registers[index] = value;
  strip.setPixelColor(31 - index, value ? 0xffff00 : 0x010101);
}

void writeRegisters() {
  // Set and display registers
  digitalWrite(latchPin, LOW);
  for (int i = 0; i < numRegisterPins; i++) {
    digitalWrite(clockPin, LOW);
    digitalWrite(dataPin, registers[i]);
    digitalWrite(clockPin, HIGH);
  }
  digitalWrite(latchPin, HIGH);
  strip.show();
}

void clearRegisters() { // clear all pins
  for (int i = 0; i < numRegisterPins; i++) {
    registers[i] = LOW;
  }
  strip.clear();
}
74HC595
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
74HC595
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
74HC595
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
74HC595
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module