// Define connection to 74HC595
int Latch_Pin = 10; // Pin connected to ST_CP (Latch pin) of 74HC595
int Clock_Pin = 11; // Pin connected to SH_CP (Clock pin) of 74HC595
int Data_Pin = 12; // Pin connected to DS (Data pin) of 74HC595
byte LEDs = 0x24; // Variable to hold the current LED pattern (8 bits)
// 0x00 00000000 0x00 All LEDs OFF
// 0xFF 11111111 0xFF All LEDs ON
// 0xAA 10101010 0xAA Alternate LEDs ON
// 0x55 01010101 0x55 Another Alternate LEDs ON
// 0x24 00100100 0x24 LEDs at positions 2 & 5 ON
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
pinMode(Latch_Pin, OUTPUT); // Set Latch_Pin as an output
pinMode(Clock_Pin, OUTPUT); // Set Clock_Pin as an output
pinMode(Data_Pin, OUTPUT); // Set Data_Pin as an output
}
void loop() {
if (Serial.available()>0){ // Check id there is also data avaliable in the Serial buffer
char input = Serial.read(); // Read the input character
if(input == '0'){
LEDs = 0x00; // All LEDs OFF
}
else if(input == '1'){
LEDs = 0xFF; // All LEDs ON
}
else if (input == 'A'){
LEDs = 0xAA; // Alternate LEDs ON
}
else if(input == 'B'){
LEDs = 0x55; // Another alternet LEDs ON
}
else if (input == 'C'){
LEDs == 0x24; // LEDs are position 2 & 5 ON
}
}
digitalWrite(Latch_Pin, LOW); // Pull Latch_Pin LOW to prepare for data transfer
Serial.print("LED Pattren = "); // Print LEDs Pattern to serial monitor
Serial.println(LEDs, HEX); // Print the current LED pattern (in decimal format)
shiftOut(Data_Pin, Clock_Pin, MSBFIRST, LEDs); // Send data to the shift register
digitalWrite(Latch_Pin, HIGH); // Pull Latch_Pin HIGH to latch the data and update LEDs
delay(100); // Delay for 100 ms before the next iteration
}