/********* 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: "Light-Controlled Automation"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-19 07:03:35

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Develop a solar tracker system using Servo and */
	/* Relay libraries to control a servomotor and a */
	/* relay module. The system should adjust the solar */
	/* panel's position based on sunlight intensity, */
	/* optimizing energy capture. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Servo.h>  //https://github.com/arduino-libraries/Servo
#include <Relay.h>  //https://github.com/rafaelnsantos/Relay

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

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

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t servo1_Servomotor_PWMSignal_PIN_D3 = 3;

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t lightSensor_PIN_A0 = A0;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo myservo;  // Create an instance of the Servo class
Relay light(relay_RelayModule_Signal_PIN_D2, 10);  // Create an instance of the Relay class with a period of 10 seconds

void setup(void)
{
    // put your setup code here, to run once:

    pinMode(relay_RelayModule_Signal_PIN_D2, OUTPUT);
    myservo.attach(servo1_Servomotor_PWMSignal_PIN_D3);  // Attach the servo to the specified pin
    
    light.setRelayMode(relayModeAutomatic);  // Set the relay mode to automatic
}

void loop(void)
{
    // put your main code here, to run repeatedly:

    int lightIntensity = analogRead(lightSensor_PIN_A0);  // Read the light intensity from the sensor
    servo1_Servomotor_PWMSignal_PIN_D3_rawData = map(lightIntensity, 0, 1023, 0, 180);  // Map the light intensity to servo angle

    updateOutputs(); // Refresh output data
    light.loop();  // Control the relay based on period and duty cycle
}

void updateOutputs()
{
    digitalWrite(relay_RelayModule_Signal_PIN_D2, relay_RelayModule_Signal_PIN_D2_rawData);
    myservo.write(servo1_Servomotor_PWMSignal_PIN_D3_rawData);  // Write the servo position
}

/* END CODE */