Advertisement
pleasedontcode

"Servo Control" rev_02

Jan 2nd, 2024
82
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-02 08:01:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* To control servo motor angle */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* To control servo motor angle */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Arduino.h>
  27. #include <EasyButton.h>
  28. #include <Servo.h>
  29.  
  30. /****** SYSTEM REQUIREMENTS *****/
  31. /****** SYSTEM REQUIREMENT 1 *****/
  32.   /* To control servo motor angle */
  33. /****** SYSTEM REQUIREMENT 2 *****/
  34.   /* To control servo motor angle */
  35. /****** END SYSTEM REQUIREMENTS *****/
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t Servomotor_PushButton_PIN_D2 = 2;
  43. const uint8_t ServoMotor_PIN = 9; // Replace with the actual pin number for the servo motor
  44.  
  45. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  46. EasyButton button(Servomotor_PushButton_PIN_D2);
  47. Servo servo;
  48.  
  49. void setup(void)
  50. {
  51.     // put your setup code here, to run once:
  52.  
  53.     pinMode(Servomotor_PushButton_PIN_D2, INPUT_PULLUP);
  54.    
  55.     servo.attach(ServoMotor_PIN);
  56.     servo.write(90); // Set the servo to the initial angle of 90 degrees
  57. }
  58.  
  59. void loop(void)
  60. {
  61.     // put your main code here, to run repeatedly:
  62.    
  63.     button.update();
  64.  
  65.     if (button.isPressed()) {
  66.         // Perform actions when the button is pressed
  67.         int currentAngle = servo.read();
  68.         if (currentAngle == 90) {
  69.             servo.write(180); // Rotate the servo to 180 degrees
  70.         } else {
  71.             servo.write(90); // Rotate the servo back to 90 degrees
  72.         }
  73.         delay(500); // Delay to avoid multiple button presses
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement