CCCPaste

Potentiometer Control rev_08

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

********* 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 *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/arduino-libraries/Servo
#include 	//https://github.com/rafaelnsantos/Relay
#include  // Include the XYZrobotServo 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 *****/
/***** 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; // Instantiate the Servo class object
XYZrobotServo xyzServo(Serial1, 1); // Instantiate the XYZrobotServo class object with Serial1 and ID 1

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
		relay_RelayModule_Signal_PIN_D3_rawData = 1; // Activate relay
	} else {
		myservo.write(0); // Set servo to 0° if potentiometer value is lower than 512
		relay_RelayModule_Signal_PIN_D3_rawData = 0; // Deactivate relay
	}

	updateOutputs(); // Refresh output data
}

void updateOutputs(void)
{
	digitalWrite(relay_RelayModule_Signal_PIN_D3, relay_RelayModule_Signal_PIN_D3_rawData);
	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 */