// Define the pin connections
#define DATA_PIN 7
#define CLOCK_PIN 5
#define LATCH_PIN 6
// #define ENABLE_PIN 4
// Define the relay output pins on the 74HC595
#define RELAY1_PIN 0
#define RELAY2_PIN 1
#define RELAY3_PIN 2
#define RELAY4_PIN 3
#define RELAY5_PIN 4
#define RELAY6_PIN 5
void setup() {
// Initialize the pins as outputs
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
// pinMode(ENABLE_PIN, OUTPUT);
}
void loop() {
// Example usage: toggle each relay every 1 second
for (int i = 0; i < 6; i++) {
setRelay(i, true); // Turn on the relay
delay(1000);
setRelay(i, false); // Turn off the relay
delay(1000);
}
}
// Function to set a single relay on or off
void setRelay(int relayNumber, bool state) {
// Calculate the bit position of the relay
int bitPosition = 1 << relayNumber;
// Set the data pin high or low depending on the state
digitalWrite(DATA_PIN, state ? HIGH : LOW);
// Shift out the data
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitPosition);
// Latch the data
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
// Enable the output
// digitalWrite(ENABLE_PIN, LOW);
}
Comments
0 B
|👍
/👎