My joystick and servos doesn´t work

Hi, i have this code, WEB IDE says that it´s correct but it doesn´t make anything, i hope you please help me:( i want to make servoX and servoY moves according to the joystick as a game
this is my code:

Servo servoX;
Servo servoY;

int joystickX = 0;
int joystickY = 0;

int ejeX = A0;
int ejeY = A1;

void setup()
{
    pinMode(D2,OUTPUT);
    pinMode(D3,OUTPUT);
    pinMode(D4,INPUT);
    pinMode(ejeX,INPUT);
    pinMode(ejeY,INPUT);
    servoX.attach(D2);
    servoY.attach(D3);
    servoX.write(0);
    servoY.write(0);
    Serial.begin(9600);
}

void loop()
{
    joystickX = analogRead(ejeX);
    joystickY = analogRead(ejeY);
    
    imprimeEjes();
    
    joystickX = map(joystickX,0,512,0,90);
    joystickY = map(joystickY,0,512,0,90);
    
    servoX.write(joystickX);
    servoY.write(joystickY);
    
    delay(40);
}

void imprimeEjes()
{
    Serial.print("X= ");
    Serial.print(joystickX);
    
    Serial.print("Y= ");
    Serial.print(joystickY);
}

Have you checked what raw values you get from your analogRead() calls?
Have you tried to control the servos directly to distinguish whether the issue is in the servo or the analog read part?
What servos are you using?
How do you power them?
What joysticks are you using?
How have you wired your joysticks?

1 Like

Hola Valeria y bienvenida a la comunidad de Particle!!!
Solo queria darte la bienvenia and tell you that we hope you can find the info you need in this place.
If something is not clear, just keep asking!
There’s a ton of people around here willing to help, and you got one of the best in that post above mine.

un saludo!
Gustavo.

1 Like

@ScruffR Yeah I already saw the values ​​that my analogRead throws, my servos are working correctly i’ve already tried them with another codes,I’m using 2 sg90 servos (the blue ones), I give power to the servos through an external battery and to the joystick through the particle photon , I am using a simple analog joystick like the one in the photo, and connect it as the photo shows

With these joysticks you need to consider that they do have a center position at approximately half the supply voltage in order to provide positive an “negative” values.
The next point to consider is that the ADCs on the Photon have a range of 0…4095

Have a try with this code, which works fine on my side

const int pinSERVO_X = D2;
const int pinSERVO_Y = D3;
const int pinJOY_X   = A0;
const int pinJOY_Y   = A1;
const int pinJOY_BTN = D7;
const int maxJOY     = 2000;
const int THRESHOLD  = 5;

int       biasX      = 2048;
int       biasY      = 2048;

Servo servoX;
Servo servoY;

void setup() {
  Serial.begin();
  Particle.function("testServos", testServos);
  //pinMode(pinJOY_X, INPUT);                               // not required for analogRead()
  //pinMode(pinJOY_Y, INPUT);                               // not required for analogRead()
  pinMode(pinJOY_BTN, INPUT_PULLUP);
  servoX.attach(pinSERVO_X);
  servoY.attach(pinSERVO_Y);
  servoX.write(0);
  servoY.write(0);
  delay(100);
  biasX = analogRead(pinJOY_X);
  biasY = analogRead(pinJOY_Y);
}

void loop() {
  static int prevX = -1;
  static int prevY = -1;
  
  int x = constrain(analogRead(pinJOY_X), biasX - maxJOY, biasX + maxJOY);
  int y = constrain(analogRead(pinJOY_Y), biasY - maxJOY, biasY + maxJOY);
  int X = map(x, biasX - maxJOY, biasX + maxJOY, 0, 90);
  int Y = map(y, biasY - maxJOY, biasY + maxJOY, 0, 90);
  
//  Serial.printlnf("raw: %d/%d, constrained: %d/%d, mapped %d/%d%s"
//                 , analogRead(pinJOY_X), analogRead(pinJOY_Y)
//                 , x, y
//                 , X, Y
//                 , digitalRead(pinJOY_BTN) ? "" : ", BTN");

  if (abs(X - prevX) > THRESHOLD) {   
    servoX.write(X);
    prevX = X;
  }
  if (abs(Y - prevY) > THRESHOLD) {   
    servoY.write(Y);
    prevY = Y;
  }
}

int testServos(const char* arg) {
  int angle = constrain(atoi(arg), 0, 180);
  servoX.write(angle);
  servoY.write(angle);
  delay(1000);
  return angle;
}
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.