pleasedontcode

ESP32 Joysticks rev_01

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