pleasedontcode

Dual Servos rev_02

Sep 24th, 2025
137
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: Dual Servos
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-09-24 12:55:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* According to the code make the code perfect for */
  21.     /* the project */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25.  
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <ESP32Servo.h>
  29.  
  30. /****** PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. int mapJoystick(int value, bool x_axis);
  34.  
  35. /***** DEFINITION OF PWM OUTPUT PINS *****/
  36. // Updated to reflect actual servo pins used in USER CODE
  37. const uint8_t SERVO1_PIN = 16;
  38. const uint8_t SERVO2_PIN = 17;
  39.  
  40. // Note: The original PRELIMINARY code defined D4 and D13 as PWM pins.
  41. // Those are not used with the current servo library configuration.
  42. // If you want to retain them, you can define and rewire, but ensure compatibility with ESP32Servo.
  43.  
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. Servo servo1, servo2;
  47.  
  48. /****** DEFINITION OF JOYSTICK AND CONTROL PINS *****/
  49. const int JOY1_X_PIN = 34;
  50. const int JOY1_Y_PIN = 35;
  51. const int JOY1_BTN_PIN = 32;
  52. const int JOY2_X_PIN = 36;
  53. const int JOY2_Y_PIN = 39;
  54. const int JOY2_BTN_PIN = 33;
  55.  
  56. // Calibration values for analog read (tune as needed)
  57. const int ANALOG_MIN = 0;      // ESP32 ADC minimum
  58. const int ANALOG_MAX = 4095;   // ESP32 ADC maximum
  59. const int CENTER = (ANALOG_MAX + ANALOG_MIN) / 2;
  60. const int DEADZONE = 100;      // ignore small movement near center
  61.  
  62. int angle1 = 0, angle2 = 0;
  63. unsigned long lastUpdate1 = 0, lastUpdate2 = 0;
  64. const unsigned long updateInterval = 10; // ms between updates
  65. unsigned long debounceTime1 = 0, debounceTime2 = 0;
  66. const unsigned long debounceDelay = 150; // ms
  67.  
  68.  
  69. void setup(void)
  70. {
  71.   // Initialize PWM timers for ESP32Servo
  72.   ESP32PWM::allocateTimer(0);
  73.   ESP32PWM::allocateTimer(1);
  74.   ESP32PWM::allocateTimer(2);
  75.   ESP32PWM::allocateTimer(3);
  76.  
  77.   servo1.setPeriodHertz(50);
  78.   servo2.setPeriodHertz(50);
  79.   // Attach servos to pins with typical 500-2500 us pulse range
  80.   servo1.attach(SERVO1_PIN, 500, 2500);
  81.   servo2.attach(SERVO2_PIN, 500, 2500);
  82.  
  83.   // Joystick button inputs
  84.   pinMode(JOY1_BTN_PIN, INPUT_PULLUP);
  85.   pinMode(JOY2_BTN_PIN, INPUT_PULLUP);
  86.  
  87.   // Start serial for debugging
  88.   Serial.begin(115200);
  89.   // Center the servos initially
  90.   angle1 = 90; angle2 = 90;
  91.   servo1.write(angle1);
  92.   servo2.write(angle2);
  93. }
  94.  
  95. int mapJoystick(int value, bool x_axis) {
  96.   // Map a single axis value to 0-180 range with a deadzone around center
  97.   if (abs(value - CENTER) < DEADZONE) return -1; // within dead zone
  98.   int angle = 0;
  99.   if (value < CENTER) {
  100.     // From min to center (left/up)
  101.     angle = map(value, ANALOG_MIN, CENTER - DEADZONE, 0, 90);
  102.   } else {
  103.     // From center to max (right/down)
  104.     angle = map(value, CENTER + DEADZONE, ANALOG_MAX, 90, 180);
  105.   }
  106.   return constrain(angle, 0, 180);
  107. }
  108.  
  109. void loop() {
  110.   unsigned long now = millis();
  111.  
  112.   // Joystick 1 and Servo 1
  113.   int joy1_x = analogRead(JOY1_X_PIN);
  114.   int joy1_y = analogRead(JOY1_Y_PIN);
  115.   bool joy1_btn = (digitalRead(JOY1_BTN_PIN) == LOW);
  116.  
  117.   if (now - lastUpdate1 > updateInterval) {
  118.     int dx = abs(joy1_x - CENTER);
  119.     int dy = abs(joy1_y - CENTER);
  120.     int mapped_angle = -1;
  121.     // Prioritize axis with greater deviation from center
  122.     if (dx > dy) {
  123.       mapped_angle = mapJoystick(joy1_x, true);
  124.     } else {
  125.       mapped_angle = mapJoystick(joy1_y, false);
  126.     }
  127.     if (mapped_angle != -1) {
  128.       angle1 = mapped_angle;
  129.     }
  130.     // Button to step servo by +1 degree when pressed (simple example)
  131.     if (joy1_btn && (now - debounceTime1 > debounceDelay)) {
  132.       angle1 = constrain(angle1 + 1, 0, 180);
  133.       debounceTime1 = now;
  134.     }
  135.     servo1.write(angle1);
  136.     lastUpdate1 = now;
  137.   }
  138.  
  139.   // Joystick 2 and Servo 2
  140.   int joy2_x = analogRead(JOY2_X_PIN);
  141.   int joy2_y = analogRead(JOY2_Y_PIN);
  142.   bool joy2_btn = (digitalRead(JOY2_BTN_PIN) == LOW);
  143.  
  144.   if (now - lastUpdate2 > updateInterval) {
  145.     int dx2 = abs(joy2_x - CENTER);
  146.     int dy2 = abs(joy2_y - CENTER);
  147.     int mapped_angle2 = -1;
  148.     if (dx2 > dy2) {
  149.       mapped_angle2 = mapJoystick(joy2_x, true);
  150.     } else {
  151.       mapped_angle2 = mapJoystick(joy2_y, false);
  152.     }
  153.     if (mapped_angle2 != -1) {
  154.       angle2 = mapped_angle2;
  155.     }
  156.     if (joy2_btn && (now - debounceTime2 > debounceDelay)) {
  157.       angle2 = constrain(angle2 + 1, 0, 180);
  158.       debounceTime2 = now;
  159.     }
  160.     servo2.write(angle2);
  161.     lastUpdate2 = now;
  162.   }
  163. }
  164.  
  165. /* END CODE */
  166.  
Advertisement
Add Comment
Please, Sign In to add comment