Advertisement
pleasedontcode

"Servo Control" rev_03

Jun 23rd, 2024
526
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-06-23 14:35:32
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When button is latched, servos move 90 degrees. */
  21.     /* When button is released, servos return to 0 */
  22.     /* degrees */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  27. #include <Servo.h>       // https://github.com/arduino-libraries/Servo
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. void onPressed(void);  // Callback function for button press
  34. void onReleased(void); // Callback function for button release
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t Button_PushButton_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF PWM OUTPUT PINS *****/
  40. const uint8_t Servo1_Servomotor_PWMSignal_PIN_D3 = 3;
  41. const uint8_t Servo2_Servomotor_PWMSignal_PIN_D5 = 5;
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. /***** used to store raw data *****/
  45. uint8_t Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  46. uint8_t Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 0;
  47.  
  48. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float Servo1_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  51. float Servo2_Servomotor_PWMSignal_PIN_D5_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. EasyButton button(Button_PushButton_PIN_D2);  // Initialize EasyButton object with the pin number
  55.  
  56. Servo servo1;  // Initialize Servo object for Servo1
  57. Servo servo2;  // Initialize Servo object for Servo2
  58.  
  59. void onPressed()
  60. {
  61.   Serial.println("Button pressed");
  62.   // When button is pressed, move servos to 90 degrees
  63.   Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 90;
  64.   Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 90;
  65. }
  66.  
  67. void onReleased()
  68. {
  69.   Serial.println("Button released");
  70.   // When button is released, move servos back to 0 degrees
  71.   Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  72.   Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 0;
  73. }
  74.  
  75. void setup(void)
  76. {
  77.   // put your setup code here, to run once:
  78.   Serial.begin(115200);
  79.   Serial.println();
  80.   Serial.println(">>> EasyButton and Servo example <<<");
  81.  
  82.   pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);
  83.  
  84.   // Attach servos to their respective pins
  85.   servo1.attach(Servo1_Servomotor_PWMSignal_PIN_D3);
  86.   servo2.attach(Servo2_Servomotor_PWMSignal_PIN_D5);
  87.  
  88.   // Initialize EasyButton
  89.   button.begin();
  90.   button.onPressed(onPressed);
  91.   button.onReleased(onReleased); // Register the release callback
  92. }
  93.  
  94. void loop(void)
  95. {
  96.   // put your main code here, to run repeatedly:
  97.   button.read();  // Read the button state
  98.  
  99.   updateOutputs();  // Refresh output data
  100. }
  101.  
  102. void updateOutputs()
  103. {
  104.   // Update servo positions based on raw data
  105.   servo1.write(Servo1_Servomotor_PWMSignal_PIN_D3_rawData);
  106.   servo2.write(Servo2_Servomotor_PWMSignal_PIN_D5_rawData);
  107. }
  108.  
  109. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement