/********* 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: Hardware Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-05-20 22:26:53

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Arduino pid code to keep the ball in the middle of */
	/* the bar in the beam and ball control system so */
	/* that the length of the beam is 32 cm and the */
	/* ultrasonic sensor and 360 degree servo motor that */
	/* is connected to the beam through the lever. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Servo.h>  // https://github.com/arduino-libraries/Servo
#include <Ultrasonic.h>  // https://github.com/ErickSimoes/Ultrasonic

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t h1_HC_SR04_Echo_PIN_D4 = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t h1_HC_SR04_Trigger_PIN_D2 = 2;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t myservo_Servomotor_PWMSignal_PIN_D3 = 3;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool h1_HC_SR04_Trigger_PIN_D2_rawData = 0;
uint8_t myservo_Servomotor_PWMSignal_PIN_D3_rawData = 0;

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo myservo; // Create an instance of the Servo class
Ultrasonic ultrasonic(12, 13); // Initialize Ultrasonic object with trigger and echo pins

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

    pinMode(h1_HC_SR04_Echo_PIN_D4, INPUT);
    pinMode(h1_HC_SR04_Trigger_PIN_D2, OUTPUT);
    pinMode(myservo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    updateOutputs(); // Refresh output data
}

void updateOutputs(void)
{
    digitalWrite(h1_HC_SR04_Trigger_PIN_D2, h1_HC_SR04_Trigger_PIN_D2_rawData);
    analogWrite(myservo_Servomotor_PWMSignal_PIN_D3, myservo_Servomotor_PWMSignal_PIN_D3_rawData);
}

/* END CODE */