I had an elegoo 5v joystick lying around and decided to test it out. It was giving numbers above 2000 and 3000 when it should be giving values from 0-1023. I purchased a pack of similar joysticks (Teyleten on amazon) which give the same result. (I am trying to center these 0-1023 values around the origin by simply subtracting 512)
I am using a 12v 5amp power supply to power 2 stepper motors, and sharing this power with a buck converter to step down to 5v to power my esp32 and joystick. I have checked the voltage on the buck converter's output (5.02v) so I think that is okay.
The last lines of code show how I am printing the joystick values to the serial monitor.
Side note: While I should first tackle the joystick reading issue, I would also appreciate anybody's advice on why my stepper motors move 1 step at a time (about 1-2 times per second). I have tried debugging by printing .speed() of each stepper to the serial monitor. This says they are being fed the correct speeds. However, they still only step about once per second, albeit in the correct direction (CW/CCW depending on my conditional statement).
#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL2WIRE, 19, 18); // left stepper
AccelStepper stepper2(AccelStepper::FULL2WIRE, 17, 16); // right stepper
#define PI 3.14159
//pins
const int x_pin = 36;
const int y_pin = 39;
const int SW = 34;
//vars for analog readings
float x; //left right
float y; //up down
//vars for speeds/directions etc
float hypotenuse;
float p1;
float p2;
float q1;
float q2;
float var1;
const int deadzone = 55;
void setup() {
Serial.begin(115200);
stepper1.setMaxSpeed(512.0);
stepper1.setAcceleration(1000.0);
stepper2.setMaxSpeed(512.0);
stepper2.setAcceleration(1000.0);
}
void loop() {
x = analogRead(x_pin) - 512; // xpin reads 0 to 1023 where 512 is middle (stationary)
y = analogRead(y_pin) - 512; // ypin reads 0 to 1023 where 512 is middle (stationary)
hypotenuse = sqrt((x * x) + (y * y));
if (hypotenuse > deadzone) {
if (x > 0 && y > 0) { // first quadrant
if (x > y) {
p1 = (PI / 4 + atan(y / x)) / (PI / 2); // % for RIGHT motor
p2 = 1 - p1; // % for LEFT motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(-var1 * p1); //negative for counterclockwise
stepper1.setSpeed(-var1 * p2); //negative for counterclockwise
} else {
p1 = (PI / 4 + atan(x / y)) / (PI / 2); // % for RIGHT motor
p2 = 1 - p1; // % for LEFT motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(-var1 * p1); //negative for counterclockwise
stepper1.setSpeed(var1 * p2); //positive for clockwise
}
}
else if (x < 0 && y > 0) { // second quadrant
if (y > -x) {
p1 = (PI / 4 + atan(-x / y)) / (PI / 2); // % for right motor
p2 = 1 - p1; // % for left motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(-var1 * p1); //negative for counterclockwise
stepper1.setSpeed(var1 * p2); //positive for clockwise
} else {
p1 = (PI / 4 + atan(y / -x)) / (PI / 2); // % for RIGHT motor
p2 = 1 - p1; // % for LEFT motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(var1 * p1); //positive for clockwise
stepper1.setSpeed(var1 * p2); //positive for clockwise
}
}
else if (x < 0 && y < 0) { // third quadrant
if (-x > -y) {
p1 = (PI / 4 + atan(-y / -x)) / (PI / 2); // % for RIGHT motor
p2 = 1 - p1; // % for LEFT motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(var1 * p1); //positive for clockwise
stepper1.setSpeed(var1 * p2); //positive for clockwise
} else {
p1 = (PI / 4 + atan(-x / -y)) / (PI / 2); // % for right motor
p2 = 1 - p1; // % for left motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(var1 * p1); //positive for clockwise
stepper1.setSpeed(-var1 * p2); //negative for counterclockwise
}
}
else if (x > 0 && y < 0) { // forth quadrant
if (-y > x) {
p1 = (PI / 4 + atan(x / -y)) / (PI / 2); // % for right motor
p2 = 1 - p1; // % for left motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(-var1 * p1); //negative for counterclockwise
stepper1.setSpeed(var1 * p2); //positive for clockwise
} else {
p1 = (PI / 4 + atan(-y / x)) / (PI / 2); // % for RIGHT motor
p2 = 1 - p1; // % for LEFT motor
q1 = p1 * p1;
q2 = p2 * p2;
var1 = sqrt(hypotenuse * hypotenuse / (q1 + q2));
stepper2.setSpeed(-var1 * p1); //negative for counterclockwise
stepper1.setSpeed(-var1 * p2); //negative for counterclockwise
}
}
else if (x == 0) { // if joystick on y-axis
if (y > 0) {
stepper2.setSpeed(-hypotenuse);
stepper1.setSpeed(hypotenuse);
} else {
stepper2.setSpeed(hypotenuse);
stepper1.setSpeed(-hypotenuse);
}
}
else if (y == 0) { // if joystick on x-axis
if (x > 0) {
stepper2.setSpeed(hypotenuse);
stepper1.setSpeed(hypotenuse);
} else {
stepper2.setSpeed(-hypotenuse);
stepper1.setSpeed(-hypotenuse);
}
}
}
else { // joystick in "deadzone" (no movement)
stepper1.setSpeed(0);
stepper2.setSpeed(0);
}
stepper1.runSpeed();
stepper2.runSpeed();
Serial.print("LR: ");
Serial.print(x);
Serial.print(" UD: ");
Serial.println(y);
Serial.println(hypotenuse);
Serial.println(var1);
Serial.println(stepper1.speed()); // prints correct speed
Serial.println(stepper2.speed()); // prints correct speed
}
