CCCPaste

"Servo Control" rev_03

/********* 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 Uno
	- Source Code created on: 2024-06-23 14:35:32

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* When button is latched, servos move 90 degrees. */
	/* When button is released, servos return to 0 */
	/* degrees */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/evert-arias/EasyButton
#include        // https://github.com/arduino-libraries/Servo

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void onPressed(void);  // Callback function for button press
void onReleased(void); // Callback function for button release

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Button_PushButton_PIN_D2 = 2;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t Servo1_Servomotor_PWMSignal_PIN_D3 = 3;
const uint8_t Servo2_Servomotor_PWMSignal_PIN_D5 = 5;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
uint8_t Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
uint8_t Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 0;

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(Button_PushButton_PIN_D2);  // Initialize EasyButton object with the pin number

Servo servo1;  // Initialize Servo object for Servo1
Servo servo2;  // Initialize Servo object for Servo2

void onPressed()
{
  Serial.println("Button pressed");
  // When button is pressed, move servos to 90 degrees
  Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 90;
  Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 90;
}

void onReleased()
{
  Serial.println("Button released");
  // When button is released, move servos back to 0 degrees
  Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 0;
}

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.println(">>> EasyButton and Servo example <<<");

  pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);

  // Attach servos to their respective pins
  servo1.attach(Servo1_Servomotor_PWMSignal_PIN_D3);
  servo2.attach(Servo2_Servomotor_PWMSignal_PIN_D5);

  // Initialize EasyButton
  button.begin();
  button.onPressed(onPressed);
  button.onReleased(onReleased); // Register the release callback
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  button.read();  // Read the button state

  updateOutputs();  // Refresh output data
}

void updateOutputs()
{
  // Update servo positions based on raw data
  servo1.write(Servo1_Servomotor_PWMSignal_PIN_D3_rawData);
  servo2.write(Servo2_Servomotor_PWMSignal_PIN_D5_rawData);
}

/* END CODE */