Advertisement
pleasedontcode

"Servo Control" rev_05

Jan 25th, 2025
39
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: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-01-26 05:54:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 实现 WiFi热点  密码 8个1  wifi热点接入时  舵机每秒5度到达360度后反转 */
  21.     /* 舵机每秒转1度 360度后反转 */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. void controlServo(void);
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t AA_LED_PIN_D1     = 1;
  37.  
  38. /***** DEFINITION OF PWM OUTPUT PINS *****/
  39. const uint8_t AA_Servomotor_PWMSignal_PIN_D2        = 2;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. bool    AA_LED_PIN_D1_rawData       = 0;
  43. uint8_t AA_Servomotor_PWMSignal_PIN_D2_rawData      = 0;
  44.  
  45. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  46. float   AA_LED_PIN_D1_phyData       = 0.0;
  47. float   AA_Servomotor_PWMSignal_PIN_D2_phyData      = 0.0;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. // Instantiate the servo object
  51. Servo myServo;
  52.  
  53. // Variables for servo control
  54. int servoPosition = 0; // Initial position of the servo
  55. int servoDirection = 1; // 1 for clockwise, -1 for counterclockwise
  56.  
  57. void setup(void)
  58. {
  59.     // put your setup code here, to run once:
  60.     pinMode(AA_LED_PIN_D1,   OUTPUT);
  61.     pinMode(AA_Servomotor_PWMSignal_PIN_D2,  OUTPUT);
  62.  
  63.     // Attach the servo to the PWM pin
  64.     myServo.attach(AA_Servomotor_PWMSignal_PIN_D2);
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // put your main code here, to run repeatedly:
  70.     updateOutputs(); // Refresh output data
  71.     controlServo(); // Control the servo movement
  72. }
  73.  
  74. void updateOutputs()
  75. {
  76.     digitalWrite(AA_LED_PIN_D1, AA_LED_PIN_D1_rawData);
  77.     analogWrite(AA_Servomotor_PWMSignal_PIN_D2, AA_Servomotor_PWMSignal_PIN_D2_rawData);
  78. }
  79.  
  80. void controlServo()
  81. {
  82.     // Update the servo position
  83.     servoPosition += servoDirection; // Change position based on direction
  84.  
  85.     // Reverse direction if limits are reached
  86.     if (servoPosition >= 180 || servoPosition <= 0) {
  87.         servoDirection *= -1; // Reverse direction
  88.     }
  89.  
  90.     // Write the position to the servo
  91.     myServo.write(servoPosition);
  92.  
  93.     // Delay to control the speed of the servo movement
  94.     delay(20); // Adjust delay for speed control (20ms for ~5 degrees per second)
  95. }
  96.  
  97. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement