Advertisement
pleasedontcode

"Button Listener" rev_01

Mar 20th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Button Listener"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-03-20 09:19:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* uando una persona preme il piccolo pulsante, */
  21.     /* l'Arduino dovrebbe rilevare la pressione e */
  22.     /* iniziare a sentire l’ambiente circostante per 5 */
  23.     /* secondi (tempo nel quale tutte e tre le lampadine */
  24.     /* LED si accendono e in cui l */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  29. #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void startListening(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t startButton_PushButton_PIN_D2 = 2;
  38. const uint8_t sensor_HC_SR04_Echo_PIN_D4 = 4;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t sensor_HC_SR04_Trigger_PIN_D3 = 3;
  42. const uint8_t LED_LED_PIN_D5 = 5;
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool sensor_HC_SR04_Trigger_PIN_D3_rawData = 0;
  47. bool LED_LED_PIN_D5_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float sensor_HC_SR04_Trigger_PIN_D3_phyData = 0.0;
  52. float LED_LED_PIN_D5_phyData = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. EasyButton startButton(startButton_PushButton_PIN_D2);
  56. Ultrasonic sensor(sensor_HC_SR04_Trigger_PIN_D3, sensor_HC_SR04_Echo_PIN_D4);
  57.  
  58. bool isListening = false;
  59. unsigned long listeningStartTime = 0;
  60. const unsigned long LISTENING_DURATION = 5000;
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.  
  66.     pinMode(startButton_PushButton_PIN_D2, INPUT_PULLUP);
  67.     pinMode(sensor_HC_SR04_Echo_PIN_D4, INPUT);
  68.  
  69.     pinMode(sensor_HC_SR04_Trigger_PIN_D3, OUTPUT);
  70.     pinMode(LED_LED_PIN_D5, OUTPUT);
  71.  
  72.     // Initialize the start button
  73.     startButton.begin();
  74.  
  75.     // Add the callback function to be called when the start button is pressed
  76.     startButton.onPressed(startListening);
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.  
  83.     startButton.read(); // Continuously read the status of the start button
  84.  
  85.     if (isListening)
  86.     {
  87.         unsigned long currentTime = millis();
  88.         if (currentTime - listeningStartTime >= LISTENING_DURATION)
  89.         {
  90.             isListening = false;
  91.             // Turn off all LEDs
  92.             LED_LED_PIN_D5_rawData = LOW;
  93.         }
  94.         else
  95.         {
  96.             // Code to be executed while listening
  97.             // You can add your own code here
  98.             LED_LED_PIN_D5_rawData = HIGH;
  99.         }
  100.     }
  101.  
  102.     updateOutputs(); // Refresh output data
  103. }
  104.  
  105. void updateOutputs()
  106. {
  107.     digitalWrite(sensor_HC_SR04_Trigger_PIN_D3, sensor_HC_SR04_Trigger_PIN_D3_rawData);
  108.     digitalWrite(LED_LED_PIN_D5, LED_LED_PIN_D5_rawData);
  109. }
  110.  
  111. void startListening()
  112. {
  113.     // Code to be executed when the start button is pressed
  114.     // You can add your own code here
  115.     isListening = true;
  116.     listeningStartTime = millis();
  117.  
  118.     // Turn on all LEDs
  119.     LED_LED_PIN_D5_rawData = HIGH;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement