Advertisement
pleasedontcode

Relay Control rev_02

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