// Pin Definitions
const int latchPin = 8; // Pin connected to ST_CP of 74HC595
const int clockPin = 12; // Pin connected to SH_CP of 74HC595
const int dataPin = 11; // Pin connected to DS of 74HC595
// Output pins for conditions A, B, C
const int outputPin1 = 2; // Arduino output 1
const int outputPin2 = 3; // Arduino output 2
const int outputPin3 = 4; // Arduino output 3
void setup() {
// Set pins as output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(outputPin1, OUTPUT);
pinMode(outputPin2, OUTPUT);
pinMode(outputPin3, OUTPUT);
}
void loop() {
// Run various sequences in the new order
ledBlinking();
ledChaser();
ledTheaterChase();
ledBreathing();
ledWave();
ledRandomHigh15(); // New sequence
}
// Function to update all shift registers
void updateShiftRegisters(unsigned long long data) {
digitalWrite(latchPin, LOW);
// Send the data to all 6 shift registers
for (int i = 0; i < 6; i++) {
shiftOut(dataPin, clockPin, MSBFIRST, (data >> (8 * (5 - i))) & 0xFF);
}
digitalWrite(latchPin, HIGH);
// Count the number of outputs that are on
int onCount = countOnBits(data);
// Set the Arduino output pins based on the conditions
if (onCount >= 1 && onCount <= 13) {
digitalWrite(outputPin1, HIGH);
digitalWrite(outputPin2, LOW);
digitalWrite(outputPin3, LOW);
} else if (onCount >= 14 && onCount <= 25) {
digitalWrite(outputPin1, HIGH);
digitalWrite(outputPin2, HIGH);
digitalWrite(outputPin3, LOW);
} else if (onCount >= 26 && onCount <= 48) {
digitalWrite(outputPin1, HIGH);
digitalWrite(outputPin2, HIGH);
digitalWrite(outputPin3, HIGH);
} else {
digitalWrite(outputPin1, LOW);
digitalWrite(outputPin2, LOW);
digitalWrite(outputPin3, LOW);
}
}
// Function to count the number of '1' bits in a 48-bit number
int countOnBits(unsigned long long data) {
int count = 0;
for (int i = 0; i < 48; i++) {
if (data & (1ULL << i)) {
count++;
}
}
return count;
}
// 1. LED Blinking
void ledBlinking() {
for (int i = 0; i < 3; i++) {
updateShiftRegisters(0xFFFFFFFFFFFF); // All LEDs on
delay(5000);
updateShiftRegisters(0x000000000000); // All LEDs off
delay(2000);
}
}
// 2. LED Chaser
void ledChaser() {
for (int i = 0; i < 48; i++) {
unsigned long long pattern = 1ULL << i;
updateShiftRegisters(pattern);
delay(500);
}
}
// 3. LED Theater Chase
void ledTheaterChase() {
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 48; i += 3) {
unsigned long long pattern = 0;
pattern |= 1ULL << i;
pattern |= 1ULL << (i + 1);
pattern |= 1ULL << (i + 2);
updateShiftRegisters(pattern);
delay(100);
}
delay(200); // Space between chase cycles
}
}
// 4. LED Breathing
void ledBreathing() {
for (int intensity = 0; intensity < 255; intensity++) {
unsigned long long pattern = (intensity << 40) | (intensity << 32) | (intensity << 24) | (intensity << 16) | (intensity << 8) | intensity;
updateShiftRegisters(pattern);
delay(10);
}
for (int intensity = 255; intensity >= 0; intensity--) {
unsigned long long pattern = (intensity << 40) | (intensity << 32) | (intensity << 24) | (intensity << 16) | (intensity << 8) | intensity;
updateShiftRegisters(pattern);
delay(10);
}
}
// 5. LED Wave
void ledWave() {
for (int offset = 0; offset < 48; offset++) {
unsigned long long pattern = 0;
for (int i = 0; i < 8; i++) {
int pos = (offset + i) % 48;
pattern |= 1ULL << pos;
}
updateShiftRegisters(pattern);
delay(100);
}
}
// 6. LED Random High 26
void ledRandomHigh15() {
for (int i = 0; i < 10; i++) {
unsigned long long pattern = 0;
int count = 0;
while (count < 26) {
int pos = random(0, 48);
if (!(pattern & (1ULL << pos))) { // Ensure the bit is not already set
pattern |= 1ULL << pos;
count++;
}
}
updateShiftRegisters(pattern);
delay(1000);
}
}