Advertisement
pleasedontcode

Button Control rev_01

Jan 11th, 2024
88
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 Control
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-01-11 22:28:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* spradzaj do nieskończoność czas trwania podania */
  21.     /* napiecia do pin 8. Jeśli czas trwania napiecia do */
  22.     /* pin 8 jest równy 3000 ms podaj napiecie do pin 8 */
  23.     /* do czas trwania 10000 ms i zakoncz podanie */
  24.     /* napiecia do pin 8. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <EasyButton.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void checkButton(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t Button1_PushButton_PIN_D2 = 2;
  38. const uint8_t VoltageOutput_PIN_D8 = 8;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. EasyButton button(Button1_PushButton_PIN_D2);
  42.  
  43. // Flag to track if the voltage has been applied to pin 8
  44. bool voltageApplied = false;
  45.  
  46. // Time at which the voltage was applied to pin 8
  47. unsigned long voltageStartTime;
  48.  
  49. void setup(void)
  50. {
  51.   // Initialize the button
  52.   button.begin();
  53.  
  54.   // Set the button callback function
  55.   button.onPressedFor(3000, checkButton);
  56.  
  57.   // Set the voltage output pin as an output
  58.   pinMode(VoltageOutput_PIN_D8, OUTPUT);
  59.  
  60.   // Initialize the voltage output pin to LOW
  61.   digitalWrite(VoltageOutput_PIN_D8, LOW);
  62. }
  63.  
  64. void loop(void)
  65. {
  66.   // Continuously read the button state
  67.   button.read();
  68.  
  69.   // Check if the voltage has been applied to pin 8
  70.   if (voltageApplied)
  71.   {
  72.     unsigned long currentTime = millis();
  73.     unsigned long duration = currentTime - voltageStartTime;
  74.  
  75.     // Check if the duration is equal to 3000 ms
  76.     if (duration == 3000)
  77.     {
  78.       // Apply voltage to pin 8 for a duration of 10000 ms
  79.       digitalWrite(VoltageOutput_PIN_D8, HIGH);
  80.       delay(10000);
  81.       digitalWrite(VoltageOutput_PIN_D8, LOW);
  82.  
  83.       // Reset the flag and start time
  84.       voltageApplied = false;
  85.       voltageStartTime = 0;
  86.     }
  87.   }
  88. }
  89.  
  90. // Callback function called when the button is pressed for the specified duration
  91. void checkButton()
  92. {
  93.   // Set the flag and record the start time
  94.   voltageApplied = true;
  95.   voltageStartTime = millis();
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement