Advertisement
pleasedontcode

Servo Control rev_01

Aug 25th, 2024
90
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-08-25 07:29:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* alla pressione del pulsante il servo si muove */
  21.     /* dalla posizione 0% all 99% a passi del 5% ogni */
  22.     /* 500ms, 0% corrisponde il massimo senso orario, 99% */
  23.     /* il massimo senso antiorario. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  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. void onPressed(void); // Function prototype for the button press callback
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t butt_PushButton_PIN_D12       = 12;
  37.  
  38. /***** DEFINITION OF PWM OUTPUT PINS *****/
  39. const uint8_t servo_Servomotor_PWMSignal_PIN_D10        = 10;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. uint8_t servo_Servomotor_PWMSignal_PIN_D10_rawData      = 0;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. // Instantiate the Servo object
  47. Servo myServo; // Create a Servo object to control the servomotor
  48.  
  49. // Instantiate the EasyButton object
  50. EasyButton button(butt_PushButton_PIN_D12); // Create an EasyButton object for the push button
  51.  
  52. void setup(void)
  53. {
  54.     Serial.begin(115200); // Initialize serial communication for debugging
  55.     pinMode(butt_PushButton_PIN_D12, INPUT_PULLUP); // Set push button pin as input with pull-up resistor
  56.     pinMode(servo_Servomotor_PWMSignal_PIN_D10, OUTPUT); // Set servomotor pin as output
  57.  
  58.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D10); // Attach the Servo object to the PWM pin
  59.  
  60.     // Initialize the EasyButton
  61.     button.begin(); // Initialize the button
  62.     button.onPressed(onPressed); // Set the callback for when the button is pressed
  63. }
  64.  
  65. // Callback function to be called when the button is pressed
  66. void onPressed()
  67. {
  68.     Serial.println("Button pressed"); // Print message to Serial Monitor
  69.     for (servo_Servomotor_PWMSignal_PIN_D10_rawData = 0; servo_Servomotor_PWMSignal_PIN_D10_rawData <= 99; servo_Servomotor_PWMSignal_PIN_D10_rawData += 5) {
  70.         myServo.write(map(servo_Servomotor_PWMSignal_PIN_D10_rawData, 0, 99, 0, 180)); // Map 0-99 to 0-180 degrees
  71.         delay(500); // Wait for 500ms before the next increment
  72.     }
  73. }
  74.  
  75. void loop(void)
  76. {
  77.     // Continuously read the status of the button
  78.     button.read(); // Read the button state
  79. }
  80.  
  81. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement