Advertisement
pleasedontcode

Relay Control rev_01

Oct 16th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Relay Control
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-10-16 14:24:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* PIN 28 Relais, PIN 30 Taster. Schreiben einen Code */
  21.     /* dass wenn ich den Taster das erste mal betätige */
  22.     /* das relais einmal schaltet und bleibt, wenn ich */
  23.     /* das zweite mal drücke soll dass Relais erst nach 2 */
  24.     /* Sekunden schalten und warten bis ich das nächste */
  25.     /* mal */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t RELAY_PIN = 28; // Pin for the relay
  37. const uint8_t BUTTON_PIN = 30; // Pin for the button
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Instance of the button using the EasyButton library.
  41. EasyButton button(BUTTON_PIN); // Initialize the EasyButton object with the defined button pin
  42.  
  43. // State variable to track the relay status
  44. bool relayState = false; // Relay is initially off
  45. bool firstPress = true; // Variable to track if it's the first press
  46.  
  47. void setup(void)
  48. {
  49.     // Set the relay pin as output
  50.     pinMode(RELAY_PIN, OUTPUT);
  51.     digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
  52.  
  53.     // Set the button pin as input with pull-up resistor
  54.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  55.  
  56.     // Initialize the button.
  57.     button.begin();
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // Continuously read the status of the button.
  63.     button.read();
  64.  
  65.     // Check if the button was pressed
  66.     if (button.wasPressed()) {
  67.         if (firstPress) {
  68.             // If it's the first press, turn on the relay
  69.             digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
  70.             relayState = true; // Update the relay state
  71.             firstPress = false; // Change state to indicate first press is done
  72.         } else {
  73.             // If it's the second press, wait for 2 seconds before turning it off
  74.             delay(2000); // Wait for 2 seconds
  75.             digitalWrite(RELAY_PIN, LOW); // Turn off the relay
  76.             relayState = false; // Update the relay state
  77.             firstPress = true; // Reset for the next cycle
  78.         }
  79.     }
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement