Advertisement
pleasedontcode

"Servo Control" rev_01

May 8th, 2024
1,179
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: "Servo Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-05-08 23:30:16
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Improve user description requirement by specifying */
  21.     /* EasyButton library usage for debouncing a push */
  22.     /* button on pin D2 with INPUT_PULLUP configuration. */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* move the servo motor of 50° if the button is */
  25.     /* pressed. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> // Include the EasyButton library for debouncing a push button
  30. #include <Servo.h>      // Include the Servo library for controlling servo motor
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void moveServo(int degrees);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t BUTTON_PIN = 2; // Define the pin for the push button (D2)
  39.  
  40. /***** DEFINITION OF SERVO MOTOR PIN *****/
  41. const uint8_t SERVO_PIN = 9; // Define the pin for the servo motor
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. EasyButton button(BUTTON_PIN); // Create an EasyButton object for the push button
  45. Servo servoMotor; // Create a Servo object to control the servo motor
  46.  
  47. void setup(void)
  48. {
  49.     // Initialize the EasyButton and Servo objects
  50.     button.begin();
  51.     servoMotor.attach(SERVO_PIN); // Attach the servo motor to the specified pin
  52. }
  53.  
  54. void loop(void)
  55. {
  56.     // Read the button state
  57.     button.read();
  58.  
  59.     // Move the servo motor 50 degrees if the button is pressed
  60.     if (button.isPressed()) {
  61.         moveServo(50);
  62.     }
  63. }
  64.  
  65. void moveServo(int degrees)
  66. {
  67.     servoMotor.write(degrees); // Move the servo motor to the specified angle
  68.     delay(15); // Delay for stability
  69. }
  70.  
  71. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement