Advertisement
pleasedontcode

"Servo Control" rev_02

Jun 23rd, 2024
544
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-06-23 14:33:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When cutton 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();
  33. void onPressed();  // Callback function for button press
  34. void onReleased(); // 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 instance
  55.  
  56. Servo servo1;  // Initialize Servo instance for Servo1
  57. Servo servo2;  // Initialize Servo instance for Servo2
  58.  
  59. // Callback function to be called when the button is pressed.
  60. void onPressed() {
  61.   Serial.println("Button pressed");
  62.   Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 90; // Move servos to 90 degrees
  63.   Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 90;
  64. }
  65.  
  66. // Callback function to be called when the button is released.
  67. void onReleased() {
  68.   Serial.println("Button released");
  69.   Servo1_Servomotor_PWMSignal_PIN_D3_rawData = 0; // Move servos back to 0 degrees
  70.   Servo2_Servomotor_PWMSignal_PIN_D5_rawData = 0;
  71. }
  72.  
  73. void setup(void) {
  74.   // put your setup code here, to run once:
  75.   Serial.begin(115200);
  76.   Serial.println();
  77.   Serial.println(">>> EasyButton and Servo example <<<");
  78.  
  79.   pinMode(Button_PushButton_PIN_D2, INPUT_PULLUP);
  80.  
  81.   // Attach servos to the corresponding pins
  82.   servo1.attach(Servo1_Servomotor_PWMSignal_PIN_D3);
  83.   servo2.attach(Servo2_Servomotor_PWMSignal_PIN_D5);
  84.  
  85.   // Initialize the button
  86.   button.begin();
  87.   // Add the callback function to be called when the button is pressed.
  88.   button.onPressed(onPressed);
  89.   // Add the callback function to be called when the button is released.
  90.   button.onPressedFor(100, onReleased); // Using onPressedFor to simulate onReleased
  91. }
  92.  
  93. void loop(void) {
  94.   // put your main code here, to run repeatedly:
  95.   button.read();  // Continuously read the status of the button
  96.   updateOutputs();  // Refresh output data
  97. }
  98.  
  99. void updateOutputs() {
  100.   // Update servo positions based on raw data
  101.   servo1.write(Servo1_Servomotor_PWMSignal_PIN_D3_rawData);
  102.   servo2.write(Servo2_Servomotor_PWMSignal_PIN_D5_rawData);
  103. }
  104.  
  105. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement