CCCPaste

"Servo Control" rev_02

/********* 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 compiled for: Arduino Uno
	- Source Code created on: 2024-06-23 14:33:56

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* When cutton 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 onPressed();  // Callback function for button press
void onReleased(); // 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 instance

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

// Callback function to be called when the button is pressed.
void onPressed() {
  Serial.println("Button pressed");
  Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 90; // Move servos to 90 degrees
  Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 90;
}

// Callback function to be called when the button is released.
void onReleased() {
  Serial.println("Button released");
  Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0; // Move servos back to 0 degrees
  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 the corresponding pins
  servo1.attach(Servo1_Servomotor_PWMSignal_PIN_D3);
  servo2.attach(Servo2_Servomotor_PWMSignal_PIN_D5);

  // Initialize the button
  button.begin();
  // Add the callback function to be called when the button is pressed.
  button.onPressed(onPressed);
  // Add the callback function to be called when the button is released.
  button.onPressedFor(100, onReleased); // Using onPressedFor to simulate onReleased
}

void loop(void) {
  // put your main code here, to run repeatedly:
  button.read();  // Continuously read the status of the button
  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 */