Advertisement
pleasedontcode

Button Interrupt rev_01

Dec 27th, 2023
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 Interrupt
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-28 00:52:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 4 floor lift, when call button press from any */
  21.     /* floor lift reaches there and floor sensor detects */
  22.     /* lift car the motor will off. when call button */
  23.     /* pressed from another floor lift starts move until */
  24.     /* floor sensor detects lift car on that floor */
  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.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Callbutton_PushButton_PIN_D2 = 2;
  37.  
  38. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  39. EasyButton button(Callbutton_PushButton_PIN_D2);  // Instance of the EasyButton class
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void motorOff();
  43. void moveToFloor(int floor);
  44.  
  45. void setup(void)
  46. {
  47.   // Initialize Serial for debugging purposes.
  48.   Serial.begin(115200);
  49.  
  50.   Serial.println();
  51.   Serial.println(">>> EasyButton interrupts example <<<");
  52.  
  53.   // Initialize the button.
  54.   button.begin();
  55.  
  56.   // Define button callback functions
  57.   button.onPressed([]() {
  58.     motorOff();
  59.   });
  60.  
  61.   button.onSequence(2, 1500, []() {
  62.     button.disableInterrupt(); // Disable interrupt during movement
  63.     moveToFloor(2);
  64.     delay(1000); // Wait for the floor sensor to detect the lift car
  65.     button.enableInterrupt([]() {}); // Re-enable interrupt after movement is complete
  66.   });
  67.  
  68.   // Enable interrupt if supported by the board
  69.   if (button.supportsInterrupt())
  70.   {
  71.     button.enableInterrupt([]() {});
  72.     Serial.println("Button will be used through interrupts");
  73.   }
  74. }
  75.  
  76. void loop(void)
  77. {
  78.   // put your main code here, to run repeatedly:
  79.  
  80.   button.read();  // Read button state and trigger callbacks if necessary
  81. }
  82.  
  83. void motorOff()
  84. {
  85.   // Code to turn off the motor
  86.   Serial.println("Motor off");
  87. }
  88.  
  89. void moveToFloor(int floor)
  90. {
  91.   // Code to move the lift to the desired floor
  92.   Serial.print("Moving to floor ");
  93.   Serial.println(floor);
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement