Joystick not working its giving only x-axis values!

The Y is always zero

Joystick X: 18, Y: 0
Backward Left
Joystick X: 18, Y: 0
Backward Left
Joystick X: 19, Y: 0
Backward Left
Joystick X: 22, Y: 0
Backward Left
Joystick X: 23, Y: 0
Backward Left
Joystick X: 24, Y: 0
Backward Left
Joystick X: 27, Y: 0
Backward Left
Joystick X: 28, Y: 0
Backward Left
Joystick X: 31, Y: 0
Backward Left
Joystick X: 34, Y: 0
Backward Left
Joystick X: 35, Y: 0
Backward Left
Joystick X: 36, Y: 0
Backward Left
Joystick X: 38, Y: 0
Backward Left
Joystick X: 39, Y: 0
Backward Left
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

// WiFi credentials
char auth[] = "";
char ssid[] = "";
char pass[] = "";

// Motor control pins
#define IN_1 5
#define IN_2 18
#define IN_3 19
#define IN_4 21
#define En_A 23
#define En_B 22

// PWM setup
const int fixedSpeed = 100;
const int freq = 1000;
const int resolution = 8;
const int DEAD_ZONE = 50;  // Increased dead zone for better stability

bool isOn = false;
BlynkTimer timer;
unsigned long lastJoystickTime = 0;

// === Movement Functions ===

void forward() {
  Serial.println("Moving Forward");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH);   // Motor A
  digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW);   // Motor B
  ledcWrite(En_A, fixedSpeed);
  ledcWrite(En_B, fixedSpeed);
}

void backward() {
  Serial.println("Moving Backward");
  digitalWrite(IN_1, HIGH); digitalWrite(IN_2, LOW);   // Motor A
  digitalWrite(IN_3, LOW); digitalWrite(IN_4, HIGH);   // Motor B
  ledcWrite(En_A, fixedSpeed);
  ledcWrite(En_B, fixedSpeed);
}

void left() {
  Serial.println("Turning Left");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW);
  ledcWrite(En_A, 0);
  ledcWrite(En_B, fixedSpeed);
}

void right() {
  Serial.println("Turning Right");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH);
  digitalWrite(IN_3, LOW); digitalWrite(IN_4, LOW);
  ledcWrite(En_A, fixedSpeed);
  ledcWrite(En_B, 0);
}

void forwardRight() {
  Serial.println("Forward Right");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH);
  digitalWrite(IN_3, LOW); digitalWrite(IN_4, LOW);
  ledcWrite(En_A, fixedSpeed);
  ledcWrite(En_B, fixedSpeed / 2);
}

void forwardLeft() {
  Serial.println("Forward Left");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW);
  ledcWrite(En_A, fixedSpeed / 2);
  ledcWrite(En_B, fixedSpeed);
}

void backwardRight() {
  Serial.println("Backward Right");
  digitalWrite(IN_1, HIGH); digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW); digitalWrite(IN_4, LOW);
  ledcWrite(En_A, fixedSpeed);
  ledcWrite(En_B, fixedSpeed / 2);
}

void backwardLeft() {
  Serial.println("Backward Left");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW); digitalWrite(IN_4, HIGH);
  ledcWrite(En_A, fixedSpeed / 2);
  ledcWrite(En_B, fixedSpeed);
}

void stop() {
  Serial.println("Stopping");
  digitalWrite(IN_1, LOW); digitalWrite(IN_2, LOW);
  digitalWrite(IN_3, LOW); digitalWrite(IN_4, LOW);
  ledcWrite(En_A, 0);
  ledcWrite(En_B, 0);
}

// === Joystick Idle Check ===

void checkJoystickIdle() {
  if (isOn && (millis() - lastJoystickTime > 200)) {
    stop();
  }
}

// === Setup ===

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);

  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT);
  pinMode(IN_3, OUTPUT);
  pinMode(IN_4, OUTPUT);

  // Attach PWM with new API
  ledcAttach(En_A, freq, resolution);
  ledcAttach(En_B, freq, resolution);

  stop();
  Serial.println("Robot setup done!");
  timer.setInterval(100, checkJoystickIdle);
}

// === Blynk Joystick (V0) ===

BLYNK_WRITE(V0) {
  if (isOn) {
    int x = param[0].asInt();
    int y = param[1].asInt();
    Serial.printf("Joystick X: %d, Y: %d\n", x, y);
    lastJoystickTime = millis();

    // Increased dead zone for both axes
    if (abs(x - 512) < DEAD_ZONE && abs(y - 512) < DEAD_ZONE) {
      stop();  // Neutral zone
    }
    else if (y >= 650 && x >= 650) {
      forwardRight();
    }
    else if (y >= 650 && x <= 400) {
      forwardLeft();
    }
    else if (y <= 400 && x >= 650) {
      backwardRight();
    }
    else if (y <= 400 && x <= 400) {
      backwardLeft();
    }
    else if (y >= 300) {
      forward();
    }
    else if (y <= 300) {
      backward();
    }
    else if (x >= 650) {
      right();
    }
    else if (x <= 400) {
      left();
    }
  } else {
    stop();
  }
}

// === Blynk Switch (V3) ===

BLYNK_WRITE(V3) {
  isOn = param.asInt();
  Serial.println(isOn ? "Robot is ON" : "Robot is OFF");
  stop();
}

void loop() {
  Blynk.run();
   timer.run();
}

@qw1 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your serial monitor output and code so that they display correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

done sir!!

How have you configured your V0 datastream?

Pete.

Okay, you’re using the widget in Simple mode, with two datastreams, V0 and V1.

The code you are using is designed for Advanced mode, with a single String type datastream on V0.

If you want to use Simple mode you’ll need to add a BLYNK_WRITE(V1) handler in your code, and stop attempting to print both X and Y values in the handlers that only receive X OR Y values.

Pete.

Ok, so to use advanced mode, I will have to vary min and max values between 0 and 255 and rewrite the code according to those limits!!?

qw1

To use Advanced mode your datastream needs to be a STRING data type.
String datastreams don’t allow you to define minimum and maximum values in your datastream setup.

The joystick will output 0-255 values when in Advanced mode, so yes - if you choose to go down the Advanced mode route you will need to adjust your code.

If you choose to stick with the Simple mode approach then you’ll also need to adjust your code.

Pete.

ok, thank you so much, Pete!!