Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Servo Control"
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-01-26 05:54:18
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* 实现 WiFi热点 密码 8个1 wifi热点接入时 舵机每秒5度到达360度后反转 */
- /* 舵机每秒转1度 360度后反转 */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Deneyap_Servo.h> //https://github.com/deneyapkart/deneyap-servo-arduino-library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- void controlServo(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t AA_LED_PIN_D1 = 1;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t AA_Servomotor_PWMSignal_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool AA_LED_PIN_D1_rawData = 0;
- uint8_t AA_Servomotor_PWMSignal_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float AA_LED_PIN_D1_phyData = 0.0;
- float AA_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Instantiate the servo object
- Servo myServo;
- // Variables for servo control
- int servoPosition = 0; // Initial position of the servo
- int servoDirection = 1; // 1 for clockwise, -1 for counterclockwise
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(AA_LED_PIN_D1, OUTPUT);
- pinMode(AA_Servomotor_PWMSignal_PIN_D2, OUTPUT);
- // Attach the servo to the PWM pin
- myServo.attach(AA_Servomotor_PWMSignal_PIN_D2);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- controlServo(); // Control the servo movement
- }
- void updateOutputs()
- {
- digitalWrite(AA_LED_PIN_D1, AA_LED_PIN_D1_rawData);
- analogWrite(AA_Servomotor_PWMSignal_PIN_D2, AA_Servomotor_PWMSignal_PIN_D2_rawData);
- }
- void controlServo()
- {
- // Update the servo position
- servoPosition += servoDirection; // Change position based on direction
- // Reverse direction if limits are reached
- if (servoPosition >= 180 || servoPosition <= 0) {
- servoDirection *= -1; // Reverse direction
- }
- // Write the position to the servo
- myServo.write(servoPosition);
- // Delay to control the speed of the servo movement
- delay(20); // Adjust delay for speed control (20ms for ~5 degrees per second)
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement