#include <TinyDebug.h>
// Pins connected to the shift registers
const int clock = 0; /* SRCLK */
const int latch = 1; /* RCLK */
const int data = 2; /* SER */
void setup() {
Debug.begin();
// Set the pins to output mode
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(data, OUTPUT);
}
void loop() {
// Generate a random 16-bit number
uint16_t pattern = random(65536);
String binaryString = String(pattern, BIN);
// Pad the string with leading zeros
while (binaryString.length() < 16) {
binaryString = "0" + binaryString;
}
Debug.println();
Debug.println(binaryString);
// Swap 15th bit with 10th bit and 14th bit with 11th bit
binaryString[14] = binaryString[9];
binaryString[13] = binaryString[10];
Debug.println(binaryString);
pattern = strtoul(binaryString.c_str(), NULL, 2);
// Send the bits to the shift registers
for (int i = 0; i < 16; i++) {
digitalWrite(data, (pattern & (1 << i)) ? HIGH : LOW);
digitalWrite(clock, HIGH);
digitalWrite(clock, LOW);
}
// Latch the data into the outputs of the shift registers
digitalWrite(latch, HIGH);
digitalWrite(latch, LOW);
// Wait for a while before the next pattern
delay(1000);
}