Advertisement
pleasedontcode

**Servo Control** rev_01

Dec 24th, 2024
288
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-12-24 07:19:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* muovere di 90° il servomotore con la pressione di */
  21.     /* un pulsante */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  28. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t pulsante_PushButton_PIN_D2        = 2;
  36. const uint8_t pulsante = 9; // Button pin
  37.  
  38. /***** DEFINITION OF PWM OUTPUT PINS *****/
  39. const uint8_t servo_Servomotor_PWMSignal_PIN_D5     = 5;
  40. const uint8_t servoPin = 8; // Servo pin
  41.  
  42. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  43. uint8_t servo_Servomotor_PWMSignal_PIN_D5_rawData       = 0;
  44.  
  45. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  46. float   servo_Servomotor_PWMSignal_PWMSignal_PIN_D5_phyData     = 0.0;
  47.  
  48. // Variable to store angle
  49. int angolo = 0; // Current angle of the servo
  50. int angoloStep = 90; // Step for angle increment
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. Servo servo; // Servo instance
  54. EasyButton button(pulsante); // EasyButton instance
  55.  
  56. void setup(void)
  57. {
  58.     // Initialize button and servo
  59.     pinMode(pulsante_PushButton_PIN_D2, INPUT_PULLUP);
  60.     servo.attach(servoPin); // Attach servo to pin 8
  61.  
  62.     Serial.begin(9600); // Initialize serial communication
  63.     button.begin(); // Initialize the button
  64. }
  65.  
  66. void loop(void)
  67. {
  68.     // Update button state
  69.     button.read(); // Read button state
  70.  
  71.     // Check if the button is pressed
  72.     if (button.isPressed()) {
  73.         angolo += angoloStep; // Increment angle
  74.         if (angolo > 180) { // Limit angle to 180 degrees
  75.             angolo = 0; // Reset angle
  76.         }
  77.         servo.write(angolo); // Move servo to new angle
  78.         delay(100); // Delay for servo movement
  79.         Serial.print("Current angle: "); // Print current angle
  80.         Serial.println(angolo);
  81.     }
  82. }
  83.  
  84. void updateOutputs()
  85. {
  86.     analogWrite(servo_Servomotor_PWMSignal_PIN_D5, servo_Servomotor_PWMSignal_PIN_D5_rawData);
  87. }
  88.  
  89. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement