Advertisement
pleasedontcode

Relay Control rev_01

Aug 30th, 2024
117
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 Uno
  14.     - Source Code created on: 2024-08-30 06:05:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* This code will This code will Write a code in */
  21.     /* arduino uno that if the proximity sensor like */
  22.     /* inductive sensor can sense an object then the */
  23.     /* relay is on but if there is no object near the */
  24.     /* proximity sensor for 1 minute give signal to the */
  25.     /* relay as off. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. bool readProximitySensor(void);
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t relay_RelayModule_Signal_PIN_D2       = 2; // Relay pin
  39. const uint8_t proximitySensorPin = 3; // Proximity sensor pin
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. bool    relay_RelayModule_Signal_PIN_D2_rawData     = 0;
  44.  
  45. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  46. /***** used to store data after characteristic curve transformation *****/
  47. float   relay_RelayModule_Signal_PIN_D2_phyData     = 0.0;
  48.  
  49. /***** TIME CONTROL VARIABLES *****/
  50. unsigned long lastDetectionTime = 0; // Last time an object was detected
  51. const unsigned long detectionTimeout = 60000; // 1 minute timeout
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. // Initialize relay object on pin 2, Normally Open (NO)
  55. Relay light(relay_RelayModule_Signal_PIN_D2, true);
  56.  
  57. void setup(void)
  58. {
  59.     // Initialize the relay pin
  60.     light.begin();
  61.     // Set the proximity sensor pin as input
  62.     pinMode(proximitySensorPin, INPUT);
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     // Refresh output data
  68.     updateOutputs();
  69. }
  70.  
  71. void updateOutputs()
  72. {
  73.     // Read the proximity sensor state
  74.     relay_RelayModule_Signal_PIN_D2_rawData = readProximitySensor();
  75.  
  76.     // Update the relay state based on the raw data
  77.     if (relay_RelayModule_Signal_PIN_D2_rawData) {
  78.         light.turnOn(); // Turn relay on if raw data is true
  79.         lastDetectionTime = millis(); // Update the last detection time
  80.     } else {
  81.         // Check if the timeout has been reached
  82.         if (millis() - lastDetectionTime >= detectionTimeout) {
  83.             light.turnOff(); // Turn relay off if no object detected for 1 minute
  84.         }
  85.     }
  86. }
  87.  
  88. bool readProximitySensor() {
  89.     // Read the state of the proximity sensor
  90.     return digitalRead(proximitySensorPin) == HIGH; // Assuming HIGH means object detected
  91. }
  92.  
  93. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement