Advertisement
pleasedontcode

"Voltage Relay" rev_01

Jul 19th, 2024
194
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: "Voltage Relay"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-19 05:30:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when input of 2v is provided to giop 0, we recieve */
  21.     /* output from giop 4 to drive relay */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void updateOutputs(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t input_PIN_D0 = 0;
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t relay_RelayModule_Signal_PIN_D4 = 4;
  37.  
  38. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  39. /***** used to store raw data *****/
  40. bool relay_RelayModule_Signal_PIN_D4_rawData = 0;
  41.  
  42. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  43. /***** used to store data after characteristic curve transformation *****/
  44. float relay_RelayModule_Signal_PIN_D4_phyData = 0.0;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. Relay light(relay_RelayModule_Signal_PIN_D4, true); // Initialize relay on pin 4, Normally Open
  48.  
  49. void setup(void)
  50. {
  51.     // put your setup code here, to run once:
  52.     pinMode(input_PIN_D0, INPUT); // Initialize the input pin
  53.     light.begin(); // Initialize the relay pin
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     // put your main code here, to run repeatedly:
  59.     updateOutputs(); // Refresh output data
  60. }
  61.  
  62. void updateOutputs()
  63. {
  64.     // Read the input voltage on pin D0
  65.     int inputVoltage = analogRead(input_PIN_D0);
  66.  
  67.     // Check if the input voltage is approximately 2V
  68.     if (inputVoltage >= 409 && inputVoltage <= 614) // Assuming 3.3V ADC reference, 2V is approximately in this range
  69.     {
  70.         light.turnOn();  // Turn relay on
  71.     }
  72.     else
  73.     {
  74.         light.turnOff(); // Turn relay off
  75.     }
  76.  
  77.     bool isLightOn = light.getState(); // Get relay state
  78.     // Use isLightOn variable as needed
  79. }
  80.  
  81. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement