Advertisement
pleasedontcode

**Motor Control** rev_01

Dec 2nd, 2024
68
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: **Motor Control**
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-12-02 18:05:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn on stepper motor 100rpm by pushing start */
  21.     /* button stop stepper motor by pushing stop button */
  22.     /* twice */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton.h
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t start_PushButton_PIN_D2       = 2;
  36. const uint8_t stop_PushButton_PIN_D3        = 3;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Create instances of EasyButton for start and stop buttons
  40. EasyButton startButton(start_PushButton_PIN_D2);
  41. EasyButton stopButton(stop_PushButton_PIN_D3);
  42.  
  43. // Variable to track the stepper motor state
  44. bool motorRunning = false;
  45. int stopPressCount = 0;
  46.  
  47. // Function to start the stepper motor
  48. void startMotor() {
  49.     // Code to turn on the stepper motor at 100 RPM
  50.     Serial.println("Stepper motor started at 100 RPM.");
  51.     motorRunning = true;
  52. }
  53.  
  54. // Function to stop the stepper motor
  55. void stopMotor() {
  56.     // Code to turn off the stepper motor
  57.     Serial.println("Stepper motor stopped.");
  58.     motorRunning = false;
  59. }
  60.  
  61. // Callback function for start button pressed
  62. void onStartPressed() {
  63.     if (!motorRunning) {
  64.         startMotor();
  65.     }
  66. }
  67.  
  68. // Callback function for stop button pressed
  69. void onStopPressed() {
  70.     stopPressCount++;
  71.     if (stopPressCount >= 2) {
  72.         stopMotor();
  73.         stopPressCount = 0; // Reset count after stopping
  74.     }
  75. }
  76.  
  77. void setup(void)
  78. {
  79.     // put your setup code here, to run once:
  80.     Serial.begin(115200); // Initialize Serial for debugging
  81.     Serial.println("System Initialized.");
  82.  
  83.     pinMode(start_PushButton_PIN_D2, INPUT_PULLUP);
  84.     pinMode(stop_PushButton_PIN_D3, INPUT_PULLUP);
  85.  
  86.     // Initialize the buttons
  87.     startButton.begin();
  88.     stopButton.begin();
  89.  
  90.     // Attach callback functions
  91.     startButton.onPressed(onStartPressed);
  92.     stopButton.onPressed(onStopPressed);
  93. }
  94.  
  95. void loop(void)
  96. {
  97.     // put your main code here, to run repeatedly:
  98.     startButton.read(); // Read the status of the start button
  99.     stopButton.read();  // Read the status of the stop button
  100. }
  101.  
  102. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement