CCCPaste

Servo Control rev_09

/********* 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:31:27

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* put servo to 180° if pot is at higher value (1024) */
	/* otherwise 0° if pot is at lower value (0). */
/****** SYSTEM REQUIREMENT 2 *****/
	/* activate relay when servo is 180° */
/****** END SYSTEM REQUIREMENTS *****/


/********* User code review feedback **********
#### Feedback 1 ####
- use relay library

********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include  // Include the Servo library
#include  // Include the Relay library

/****** 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 *****/
bool relay_RelayModule_Signal_PIN_D3_rawData = 0;
uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
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; // Instantiate the Servo class object
Relay light(relay_RelayModule_Signal_PIN_D3, 30); // Instantiate the Relay class object on pin 3 with a period of 30 seconds

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  pinMode(relay_RelayModule_Signal_PIN_D3, OUTPUT);
  pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  if (potValue >= 512)
  {
    myservo.write(180); // Set servo to 180° if potentiometer value is higher or equal to 512
    light.setRelayPosition(relayPositionClosed); // Activate relay
  }
  else
  {
    myservo.write(0); // Set servo to 0° if potentiometer value is lower than 512
    light.setRelayPosition(relayPositionOpen); // Deactivate relay
  }

  updateOutputs(); // Refresh output data
}

void updateOutputs(void)
{
  digitalWrite(relay_RelayModule_Signal_PIN_D3, light.getRelayPosition() == relayPositionClosed);
  servo_Servomotor_PWMSignal_PIN_D2_rawData = map(myservo.read(), 0, 180, 0, 255); // Map servo angle to PWM range
  analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
}

/* END CODE */