CCCPaste

Relay Control rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Relay Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-08-30 06:05:29

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* This code will This code will Write a code in */
	/* arduino uno that if the proximity sensor like */
	/* inductive sensor can sense an object then the */
	/* relay is on but if there is no object near the */
	/* proximity sensor for 1 minute give signal to the */
	/* relay as off. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/rafaelnsantos/Relay

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
bool readProximitySensor(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t relay_RelayModule_Signal_PIN_D2		= 2; // Relay pin
const uint8_t proximitySensorPin = 3; // Proximity sensor pin

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool	relay_RelayModule_Signal_PIN_D2_rawData		= 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float	relay_RelayModule_Signal_PIN_D2_phyData		= 0.0;

/***** TIME CONTROL VARIABLES *****/
unsigned long lastDetectionTime = 0; // Last time an object was detected
const unsigned long detectionTimeout = 60000; // 1 minute timeout

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize relay object on pin 2, Normally Open (NO)
Relay light(relay_RelayModule_Signal_PIN_D2, true); 

void setup(void)
{
	// Initialize the relay pin
	light.begin(); 
	// Set the proximity sensor pin as input
	pinMode(proximitySensorPin, INPUT); 
}

void loop(void)
{
	// Refresh output data
	updateOutputs(); 
}

void updateOutputs()
{
	// Read the proximity sensor state
	relay_RelayModule_Signal_PIN_D2_rawData = readProximitySensor();

	// Update the relay state based on the raw data
	if (relay_RelayModule_Signal_PIN_D2_rawData) {
		light.turnOn(); // Turn relay on if raw data is true
		lastDetectionTime = millis(); // Update the last detection time
	} else {
		// Check if the timeout has been reached
		if (millis() - lastDetectionTime >= detectionTimeout) {
			light.turnOff(); // Turn relay off if no object detected for 1 minute
		}
	}
}

bool readProximitySensor() {
	// Read the state of the proximity sensor
	return digitalRead(proximitySensorPin) == HIGH; // Assuming HIGH means object detected
}

/* END CODE */