CCCPaste

Relay Toggle rev_03

/********* 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 Toggle
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-07-28 08:58:41

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Accendi e spegni il rele ogni 12 ore */
/****** END SYSTEM REQUIREMENTS *****/


/********* User code review feedback **********
#### Feedback 1 ####
- 'class Relay' has no member named 'loop'
#### Feedback 2 ####
- avvia con il rele normalmente chiuso
********* User code review feedback **********/

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void); // Function prototype for updating outputs

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t rele_RelayModule_Signal_PIN_D2		= 2;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool	rele_RelayModule_Signal_PIN_D2_rawData		= 1; // Start with relay ON (Normally Closed)

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create a relay on pin 2
Relay relay(rele_RelayModule_Signal_PIN_D2, false); // Initialize relay in Normally Closed mode

// Timing variables
unsigned long previousMillis = 0; // Store the last time the relay was toggled
const unsigned long interval = 12 * 60 * 60 * 1000; // 12 hours in milliseconds

void setup(void)
{
	// put your setup code here, to run once:
	Serial.begin(9600); // Initialize serial communication for debugging
	relay.begin(); // Initialize the relay
	pinMode(rele_RelayModule_Signal_PIN_D2, OUTPUT); // Set the relay pin as output
	updateOutputs(); // Ensure the initial state is set correctly
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	unsigned long currentMillis = millis(); // Get the current time

	// Check if 12 hours have passed
	if (currentMillis - previousMillis >= interval) {
		previousMillis = currentMillis; // Save the last time the relay was toggled
		rele_RelayModule_Signal_PIN_D2_rawData = !rele_RelayModule_Signal_PIN_D2_rawData; // Toggle the relay state
		updateOutputs(); // Refresh output data
	}
}

void updateOutputs()
{
	// Update the relay state based on the raw data
	digitalWrite(rele_RelayModule_Signal_PIN_D2, rele_RelayModule_Signal_PIN_D2_rawData);
}

/* END CODE */