Hi, I recently started using Arduino since I want to make a submarine. But I come into some issues using the code for a joystick which controls two servos. the Joystick outputs 3 axis to the servo. But I only want two. is there a way to get rid of the corner axis in the code and make it so the servos go left and right separately depending which way you move the joystick? " I only want two axis potentiometer" and the joysticks are very sensitive is there a way to implement some dead zone within the joystick? I am using SG90 servos and the default Arduino joystick module, thanks. here is the code I was using.
#include <Servo.h>
//Servo objects created to control the servos
Servo myServo1;
Servo myServo2;
int servo1 = 3; //Digital PWM pin used by the servo 1
int servo2 = 5; //Digital PWM pin used by the servo 2
int joyX = 0; //Analog pin to which the joystick (X) is connected
int joyY = 1; //Analog pin to which the joystick (Y) is connected
const byte JoystickCount = 4;
const byte AxisCount = JoystickCount * 2;
void setup(){
myServo1.attach(servo1);
myServo2.attach(servo2);
}
void loop(){
int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)
valX = map(valX, 5, 1023, 10, 170); //Scale the joystick X value to use it with the servo
valY = map(valY, 5, 1023, 10, 170); //Scale the joystick X value to use it with the servo
//Sets the servo position according to the scaled values.
myServo1.write(valX);
myServo2.write(valY);
delay(5);
}
Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask like a link to your joystick, description of your circuit etc).
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s barely readable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)
i understand that but when you move the joystick to the corners both of the servos move to certain positions. but i don't want the servos to move when you put the joystick at the corners not for them to move together when you move the knob to the corner of the joystick.
If the Y position is greater or less than zero by an amount "m", then the X position should be set to X, or m, whichever is lesser. Or, -X or -m, whichever is greater, for the negative values of X.