CCCPaste

Control Servos 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: Control Servos
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-05-05 18:06:08

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* a system of two servo, that will close the road */
	/* when the button is pushed 1st time and open when */
	/* pushed 2nd time. */
/****** END SYSTEM REQUIREMENTS *****/

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void toggleServoPosition(Servo &servo);

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

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t Servomotor_PWMSignal_PIN_D3 = 3;
const uint8_t Servomotor_PWMSignal_PIN_D5 = 5;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo servo1; // Servo object for Servo motor 1
Servo servo2; // Servo object for Servo motor 2

bool isPushButtonPressed = false;
int pushButtonState = 0;

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(PushButton_PIN_D2, INPUT_PULLUP);

  pinMode(Servomotor_PWMSignal_PIN_D3, OUTPUT);
  pinMode(Servomotor_PWMSignal_PIN_D5, OUTPUT);

  servo1.attach(Servomotor_PWMSignal_PIN_D3); // Attach servo1 to PWM pin 3
  servo2.attach(Servomotor_PWMSignal_PIN_D5); // Attach servo2 to PWM pin 5
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  pushButtonState = digitalRead(PushButton_PIN_D2);

  if (pushButtonState == LOW && !isPushButtonPressed) {
    toggleServoPosition(servo1);
    isPushButtonPressed = true;
  } else if (pushButtonState == LOW && isPushButtonPressed) {
    toggleServoPosition(servo2);
    isPushButtonPressed = false;
  }
}

void updateOutputs()
{
  servo1.write(Servomotor_PWMSignal_PIN_D3_rawData);
  servo2.write(Servomotor_PWMSignal_PIN_D5_rawData);
}

void toggleServoPosition(Servo &servo)
{
  int pos = servo.read();
  if (pos == 0) {
    servo.write(180);
  } else {
    servo.write(0);
  }
}

/* END CODE */