CCCPaste

Servo Control rev_07

/********* 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:26:32

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

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/arduino-libraries/Servo
#include   // 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; // Creating an instance of the Servo class

void setup(void)
{
    // put your setup code here, to run once:
    myservo.attach(servo_Servomotor_PWMSignal_PIN_D2); // Attaching the servo to the specified pin

    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); // Read potentiometer value
    int servoAngle = map(potValue, 0, 1024, 0, 180); // Map potentiometer value to servo angle

    myservo.write(servoAngle); // Set servo angle based on potentiometer value

    if (potValue >= 512) {
        myservo.write(180); // Put servo to 180° if pot is at higher value (1024)
    } else {
        myservo.write(0);   // Put servo to 0° if pot is at lower value (0)
    }

    delay(15); // Delay for smooth servo movement
}

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