Advertisement
pleasedontcode

Servo Control rev_04

May 20th, 2024
1,094
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-05-20 22:39:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement Arduino PID control algorithm to */
  21.     /* maintain ball position within 32 cm beam using */
  22.     /* Ultrasonic sensor and 360-degree Servo motor. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Servo.h> // https://github.com/arduino-libraries/Servo
  27. #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Ultrasonic_Sensor_HC_SR04_Echo_PIN_D6 = 6;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D5 = 5;
  39.  
  40. /***** DEFINITION OF PWM OUTPUT PINS *****/
  41. const uint8_t myservo_Servomotor_PWMSignal_PIN_D3 = 3;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. Servo myservo; // Initialize Servo object
  45. Ultrasonic ultrasonic(Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D5, Ultrasonic_Sensor_HC_SR04_Echo_PIN_D6); // Initialize Ultrasonic object
  46.  
  47. void setup(void)
  48. {
  49.     // put your setup code here, to run once:
  50.  
  51.     pinMode(Ultrasonic_Sensor_HC_SR04_Echo_PIN_D6, INPUT);
  52.     pinMode(Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D5, OUTPUT);
  53.     pinMode(myservo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  54.  
  55.     myservo.attach(myservo_Servomotor_PWMSignal_PIN_D3); // Attach Servo object to PWM pin
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // put your main code here, to run repeatedly:
  61.  
  62.     // Implement PID control algorithm to maintain ball position within 32 cm beam
  63.     float distance = ultrasonic.read(); // Read distance from ultrasonic sensor
  64.     float error = 32.0 - distance; // Calculate error
  65.     float kp = 0.1; // Proportional gain
  66.     int servoAngle = 90 + kp * error; // Calculate servo angle based on error
  67.  
  68.     // Ensure servo angle is within limits
  69.     if (servoAngle < 0) {
  70.         servoAngle = 0;
  71.     } else if (servoAngle > 180) {
  72.         servoAngle = 180;
  73.     }
  74.  
  75.     myservo.write(servoAngle); // Set servo angle based on PID control
  76.  
  77.     delay(100); // Delay for stability
  78. }
  79.  
  80. void updateOutputs(void)
  81. {
  82.     // No need to update outputs separately in this case
  83. }
  84.  
  85. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement