Advertisement
pleasedontcode

Button Toggle rev_01

Feb 9th, 2024
98
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 Toggle
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-09 19:56:20
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if trigger is pressed, output 7 will turn on off */
  21.     /* for 10 times, then will stay on for 10 seconds */
  22.     /* then will turn on off 3 times then turn off */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t trigger_PushButton_PIN_D2 = 2;
  34. const uint8_t output_PIN_D7 = 7;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. EasyButton triggerButton(trigger_PushButton_PIN_D2);
  38.  
  39. // State variables
  40. bool isTriggered = false;
  41. bool isToggleOn = false;
  42. int toggleCount = 0;
  43. unsigned long startTime = 0;
  44.  
  45. void setup(void)
  46. {
  47.   // put your setup code here, to run once:
  48.   pinMode(trigger_PushButton_PIN_D2, INPUT_PULLUP);
  49.   pinMode(output_PIN_D7, OUTPUT);
  50.  
  51.   // Initialize the EasyButton object
  52.   triggerButton.begin();
  53. }
  54.  
  55. void loop(void)
  56. {
  57.   // put your main code here, to run repeatedly:
  58.  
  59.   // Read the state of the button
  60.   triggerButton.read();
  61.  
  62.   // Check if the button is pressed
  63.   if (triggerButton.isPressed())
  64.   {
  65.     // Toggle the output pin if the button is pressed
  66.     isTriggered = true;
  67.     toggleOutputPin();
  68.   }
  69.  
  70.   // Check if the toggle count has reached 10
  71.   if (toggleCount == 10)
  72.   {
  73.     // Turn on the output pin for 10 seconds
  74.     if (isToggleOn)
  75.     {
  76.       digitalWrite(output_PIN_D7, HIGH);
  77.       startTime = millis();
  78.       isToggleOn = false;
  79.     }
  80.     else
  81.     {
  82.       // Check if 10 seconds have passed
  83.       if (millis() - startTime >= 10000)
  84.       {
  85.         // Turn off the output pin
  86.         digitalWrite(output_PIN_D7, LOW);
  87.         isToggleOn = true;
  88.         toggleCount = 0;
  89.         isTriggered = false;
  90.       }
  91.     }
  92.   }
  93.  
  94.   // Check if the toggle count has reached 3 after the 10 seconds
  95.   if (toggleCount >= 10 && toggleCount < 13)
  96.   {
  97.     // Toggle the output pin
  98.     toggleOutputPin();
  99.   }
  100. }
  101.  
  102. void toggleOutputPin()
  103. {
  104.   if (isToggleOn)
  105.   {
  106.     digitalWrite(output_PIN_D7, HIGH);
  107.   }
  108.   else
  109.   {
  110.     digitalWrite(output_PIN_D7, LOW);
  111.   }
  112.  
  113.   isToggleOn = !isToggleOn;
  114.   toggleCount++;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement