CCCPaste

"Button Listener" 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: "Button Listener"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-03-20 09:19:38

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* uando una persona preme il piccolo pulsante, */
	/* l'Arduino dovrebbe rilevare la pressione e */
	/* iniziare a sentire l’ambiente circostante per 5 */
	/* secondi (tempo nel quale tutte e tre le lampadine */
	/* LED si accendono e in cui l */
/****** 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 startListening(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t startButton_PushButton_PIN_D2 = 2;
const uint8_t sensor_HC_SR04_Echo_PIN_D4 = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t sensor_HC_SR04_Trigger_PIN_D3 = 3;
const uint8_t LED_LED_PIN_D5 = 5;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton startButton(startButton_PushButton_PIN_D2);
Ultrasonic sensor(sensor_HC_SR04_Trigger_PIN_D3, sensor_HC_SR04_Echo_PIN_D4);

bool isListening = false;
unsigned long listeningStartTime = 0;
const unsigned long LISTENING_DURATION = 5000;

void setup(void)
{
	// put your setup code here, to run once:

	pinMode(startButton_PushButton_PIN_D2, INPUT_PULLUP);
	pinMode(sensor_HC_SR04_Echo_PIN_D4, INPUT);

	pinMode(sensor_HC_SR04_Trigger_PIN_D3, OUTPUT);
	pinMode(LED_LED_PIN_D5, OUTPUT);

	// Initialize the start button
	startButton.begin();

	// Add the callback function to be called when the start button is pressed
	startButton.onPressed(startListening);
}

void loop(void)
{
	// put your main code here, to run repeatedly:

	startButton.read(); // Continuously read the status of the start button

	if (isListening)
	{
		unsigned long currentTime = millis();
		if (currentTime - listeningStartTime >= LISTENING_DURATION)
		{
			isListening = false;
			// Turn off all LEDs
			LED_LED_PIN_D5_rawData = LOW;
		}
		else
		{
			// Code to be executed while listening
			// You can add your own code here
			LED_LED_PIN_D5_rawData = HIGH;
		}
	}

	updateOutputs(); // Refresh output data
}

void updateOutputs()
{
	digitalWrite(sensor_HC_SR04_Trigger_PIN_D3, sensor_HC_SR04_Trigger_PIN_D3_rawData);
	digitalWrite(LED_LED_PIN_D5, LED_LED_PIN_D5_rawData);
}

void startListening()
{
	// Code to be executed when the start button is pressed
	// You can add your own code here
	isListening = true;
	listeningStartTime = millis();

	// Turn on all LEDs
	LED_LED_PIN_D5_rawData = HIGH;
}