Advertisement
pleasedontcode

**Servo Control** rev_02

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