CCCPaste

Sensor Control rev_01

/********* 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: Sensor Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-09-30 17:18:14

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The application must implement the Ultrasonic */
	/* sensor for real-time distance detection and */
	/* utilize EasyButton for user-triggered actions, */
	/* enhancing interaction and responsiveness in the */
	/* CROSSING project environment. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/evert-arias/EasyButton
#include   // https://github.com/ErickSimoes/Ultrasonic

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t OD_HC_SR04_Echo_PIN_D3 = 3;

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

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Initialize EasyButton object with the button pin
EasyButton button(2); // Assuming button is connected to pin 2

// Initialize Ultrasonic object with the trigger and echo pins
Ultrasonic ultrasonic(OD_HC_SR04_Trigger_PIN_D2, OD_HC_SR04_Echo_PIN_D3); // Trigger pin, Echo pin

void setup(void)
{
    // Set up the ultrasonic sensor pins
    pinMode(OD_HC_SR04_Echo_PIN_D3, INPUT);
    pinMode(OD_HC_SR04_Trigger_PIN_D2, OUTPUT);
    
    // Initialize serial communication for debugging
    Serial.begin(115200);
    Serial.println();
    Serial.println(">>> Ultrasonic and EasyButton Example <<<");
    
    // Initialize the button
    button.begin();
    button.onPressedFor(2000, buttonPressedTwoSeconds); // Set action for button press duration

    if (button.supportsInterrupt()) {
        button.enableInterrupt(buttonISR); // Enable interrupt for button
        Serial.println("Button will be used through interrupts");
    }
}

void loop(void)
{
    // Refresh output data
    updateOutputs(); 
    button.update(); // Update button state

    // Read distance from ultrasonic sensor and print it
    int distance = ultrasonic.read(); // Get distance in cm
    Serial.print("Distance in CM: ");
    Serial.println(distance);
}

void updateOutputs()
{
    digitalWrite(OD_HC_SR04_Trigger_PIN_D2, OD_HC_SR04_Trigger_PIN_D2_rawData);
}

// Function called when the button is pressed for two seconds
void buttonPressedTwoSeconds(void) {
    Serial.println("Button pressed for two seconds");
}

// Interrupt service routine for button
void buttonISR(void) {
    button.read(); // Read the button state
}

/* END CODE */