/********* 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: Servo Control
	- Source Code NOT compiled for: Arduino Mega
	- Source Code created on: 2024-05-09 23:22:56

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* read pot and manage angle of the servo. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* put servo to 180° if pot is at higher value (1024) */
	/* otherwise 0° if pot is at lower value (0). */
/****** END SYSTEM REQUIREMENTS *****/


/********* User code review feedback **********
#### Feedback 1 ####
- activate relay if servo is 180°
#### Feedback 2 ####
- use functions of library relay.h to activate/deactivate relay 
********* User code review feedback **********/

/****** 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 ANALOG INPUT PINS *****/
const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t relay_RelayModule_Signal_PIN_D3 = 3;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo myServo; // Initialize the Servo object
Relay myRelay(relay_RelayModule_Signal_PIN_D3); // Initialize the Relay object

void setup(void)
{
  // put your setup code here, to run once:
  myServo.attach(servo_Servomotor_PWMSignal_PIN_D2); // Attach servo to PWM pin

  pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  pinMode(relay_RelayModule_Signal_PIN_D3, OUTPUT);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  int servoAngle = map(potValue, 0, 1023, 0, 180); // Map pot value to servo angle

  myServo.write(servoAngle); // Set servo angle based on pot value

  if (potValue >= 512) {
    myServo.write(180); // Put servo to 180° if pot is at higher value (1024)
    myRelay.activate(); // Activate relay if servo is at 180°
  } else {
    myServo.write(0); // Put servo to 0° if pot is at lower value (0)
    myRelay.deactivate(); // Deactivate relay
  }

  delay(15);
}

void updateOutputs(void)
{
  digitalWrite(relay_RelayModule_Signal_PIN_D3, relay_RelayModule_Signal_PIN_D3_rawData);
  analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
}

/* END CODE */