Advertisement
hwthinker

T-relay S3-Test Relay

May 10th, 2024
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.22 KB | Source Code | 0 0
  1. // Define the pin connections
  2. #define DATA_PIN  7
  3. #define CLOCK_PIN 5
  4. #define LATCH_PIN 6
  5. // #define ENABLE_PIN 4
  6.  
  7. // Define the relay output pins on the 74HC595
  8. #define RELAY1_PIN 0
  9. #define RELAY2_PIN 1
  10. #define RELAY3_PIN 2
  11. #define RELAY4_PIN 3
  12. #define RELAY5_PIN 4
  13. #define RELAY6_PIN 5
  14.  
  15. void setup() {
  16.   // Initialize the pins as outputs
  17.   pinMode(DATA_PIN, OUTPUT);
  18.   pinMode(CLOCK_PIN, OUTPUT);
  19.   pinMode(LATCH_PIN, OUTPUT);
  20.   // pinMode(ENABLE_PIN, OUTPUT);
  21. }
  22.  
  23. void loop() {
  24.   // Example usage: toggle each relay every 1 second
  25.   for (int i = 0; i < 6; i++) {
  26.     setRelay(i, true);  // Turn on the relay
  27.     delay(1000);
  28.     setRelay(i, false);  // Turn off the relay
  29.     delay(1000);
  30.   }
  31. }
  32.  
  33. // Function to set a single relay on or off
  34. void setRelay(int relayNumber, bool state) {
  35.   // Calculate the bit position of the relay
  36.   int bitPosition = 1 << relayNumber;
  37.  
  38.   // Set the data pin high or low depending on the state
  39.   digitalWrite(DATA_PIN, state ? HIGH : LOW);
  40.  
  41.   // Shift out the data
  42.   shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitPosition);
  43.  
  44.   // Latch the data
  45.   digitalWrite(LATCH_PIN, HIGH);
  46.   digitalWrite(LATCH_PIN, LOW);
  47.  
  48.   // Enable the output
  49.   // digitalWrite(ENABLE_PIN, LOW);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement