Advertisement
pleasedontcode

**Servo Control** rev_01

Feb 13th, 2025
143
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: ESP32 DevKit V1
  14.     - Source Code created on: 2025-02-13 13:10:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* solar tracker,        one vertical servo,one */
  21.     /* rotational servo, 4 ldr */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <ESP32Servo.h> //https://madhephaestus.github.io/ESP32Servo/annotated.html
  28. #include <analogWrite.h>    //https://madhephaestus.github.io/ESP32Servo/annotated.html
  29. #include <tone.h>   //https://madhephaestus.github.io/ESP32Servo/annotated.html
  30. #include <ESP32Tone.h>  //https://madhephaestus.github.io/ESP32Servo/annotated.html
  31. #include <ESP32PWM.h>   //https://madhephaestus.github.io/ESP32Servo/annotated.html
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38.  
  39. // Servo configuration
  40. #define SERVO_YAW 26   // Horizontal servo pin
  41. #define SERVO_TILT 27  // Vertical servo pin
  42.  
  43. // LDR pins
  44. #define LDR_TL 34  // Top-Left LDR
  45. #define LDR_TR 35  // Top-Right LDR
  46. #define LDR_BL 32  // Bottom-Left LDR
  47. #define LDR_BR 33  // Bottom-Right LDR
  48.  
  49. Servo servoYaw;
  50. Servo servoTilt;
  51.  
  52. // Servo angle limits
  53. #define MIN_ANGLE 45
  54. #define MAX_ANGLE 135
  55.  
  56. // Movement and control parameters
  57. #define MOVEMENT_STEP 1
  58. #define THRESHOLD 20
  59. #define DEADZONE 10            // Increased to prevent jitter
  60. #define SERVO_DELAY 20
  61. #define DEBUG_INTERVAL 1000
  62.  
  63. int yawAngle = 90;
  64. int tiltAngle = 90;
  65. int lastYawAngle = 90;
  66. int lastTiltAngle = 90;
  67.  
  68. unsigned long lastDebugTime = 0;
  69.  
  70. // Function to read and average LDR values
  71. int readLDR(int pin) {
  72.     long total = 0;
  73.     const int readings = 5;
  74.     for (int i = 0; i < readings; i++) {
  75.         total += analogRead(pin);
  76.         delay(2);
  77.     }
  78.     return total / readings;
  79. }
  80.  
  81. // Normalize LDR values between 0 and 100
  82. int normalizeLDR(int value) {
  83.     const int minVal = 300;   // Adjusted for more tolerance
  84.     const int maxVal = 4095;
  85.     return constrain(map(value, minVal, maxVal, 0, 100), 0, 100);
  86. }
  87.  
  88. void moveServos() {
  89.     // Clamp angles within range
  90.     yawAngle = constrain(yawAngle, MIN_ANGLE, MAX_ANGLE);
  91.     tiltAngle = constrain(tiltAngle, MIN_ANGLE, MAX_ANGLE);
  92.  
  93.     // Move Yaw servo gradually
  94.     if (yawAngle != lastYawAngle) {
  95.         if (yawAngle > lastYawAngle) lastYawAngle += MOVEMENT_STEP;
  96.         else if (yawAngle < lastYawAngle) lastYawAngle -= MOVEMENT_STEP;
  97.         servoYaw.write(lastYawAngle);
  98.     }
  99.  
  100.     // Move Tilt servo gradually
  101.     if (tiltAngle != lastTiltAngle) {
  102.         if (tiltAngle > lastTiltAngle) lastTiltAngle += MOVEMENT_STEP;
  103.         else if (tiltAngle < lastTiltAngle) lastTiltAngle -= MOVEMENT_STEP;
  104.         servoTilt.write(lastTiltAngle);
  105.     }
  106.  
  107.     delay(SERVO_DELAY);
  108. }
  109.  
  110. void setup() {
  111.     Serial.begin(115200);
  112.     // The following lines are compatible with ESP32
  113.     // analogSetAttenuation(ADC_11db); // Uncomment if using ADC attenuation settings
  114.     // analogSetWidth(12); // Uncomment if using ADC width settings
  115.  
  116.     servoYaw.attach(SERVO_YAW);
  117.     servoTilt.attach(SERVO_TILT);
  118.     delay(100);
  119.  
  120.     servoYaw.write(90);
  121.     servoTilt.write(90);
  122.     delay(500);
  123.  
  124.     Serial.println("System Initialized.");
  125. }
  126.  
  127. void loop() {
  128.     int topLeft = readLDR(LDR_TL);
  129.     int topRight = readLDR(LDR_TR);
  130.     int bottomLeft = readLDR(LDR_BL);
  131.     int bottomRight = readLDR(LDR_BR);
  132.  
  133.     int tl = normalizeLDR(topLeft);
  134.     int tr = normalizeLDR(topRight);
  135.     int bl = normalizeLDR(bottomLeft);
  136.     int br = normalizeLDR(bottomRight);
  137.  
  138.     int horizontalDiff = ((tr + br) - (tl + bl)) / 2;
  139.     int verticalDiff = ((bl + br) - (tl + tr)) / 2;
  140.  
  141.     if (abs(horizontalDiff) > DEADZONE) {
  142.         yawAngle += (horizontalDiff > 0) ? MOVEMENT_STEP : -MOVEMENT_STEP;
  143.     }
  144.  
  145.     if (abs(verticalDiff) > DEADZONE) {
  146.         tiltAngle += (verticalDiff > 0) ? -MOVEMENT_STEP : MOVEMENT_STEP;
  147.     }
  148.  
  149.     moveServos();
  150.  
  151.     if (millis() - lastDebugTime >= DEBUG_INTERVAL) {
  152.         Serial.println("\n--- Debug Info ---");
  153.         Serial.printf("Raw ADC: TL=%d, TR=%d, BL=%d, BR=%d\n", topLeft, topRight, bottomLeft, bottomRight);
  154.         Serial.printf("Normalized: TL=%d, TR=%d, BL=%d, BR=%d\n", tl, tr, bl, br);
  155.         Serial.printf("Diff: H=%d, V=%d\n", horizontalDiff, verticalDiff);
  156.         Serial.printf("Servos: Yaw=%d, Tilt=%d\n", lastYawAngle, lastTiltAngle);
  157.         Serial.println("-------------------");
  158.         lastDebugTime = millis();
  159.     }
  160.  
  161.     delay(20);
  162. }
  163.  
  164. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement