Advertisement
pleasedontcode

Button Relay rev_01

Dec 15th, 2023
71
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: Button Relay
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-15 11:14:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connect a Motor Using Relay & Control from Serial */
  21.     /* Input  and Relay switching output will be 230V */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <EasyButton.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void toggleRelay(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t SREE_PushButton_PIN_D2 = 2;
  35. const uint8_t RELAY_PIN = 3;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38.  
  39. // Initialize the EasyButton object with the button pin
  40. EasyButton button(SREE_PushButton_PIN_D2);
  41.  
  42. void setup(void)
  43. {
  44.   // Initialize Serial for debugging purposes.
  45.   Serial.begin(9600);
  46.  
  47.   // Initialize the relay pin
  48.   pinMode(RELAY_PIN, OUTPUT);
  49.  
  50.   // Set the initial state of the relay to OFF
  51.   digitalWrite(RELAY_PIN, HIGH);
  52.  
  53.   // Initialize the button object
  54.   button.begin();
  55.  
  56.   // Register the toggleRelay function to be called when the button is pressed
  57.   button.onPressed(toggleRelay);
  58. }
  59.  
  60. void loop(void)
  61. {
  62.   // Read the button state
  63.   button.read();
  64.  
  65.   // Add your additional code here
  66. }
  67.  
  68. void toggleRelay(void)
  69. {
  70.   // Toggle the state of the relay
  71.   digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
  72.  
  73.   // Send the relay state information via Serial
  74.   if(digitalRead(RELAY_PIN)){
  75.     Serial.println("Relay OFF");
  76.   } else {
  77.     Serial.println("Relay ON");
  78.   }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement