Arcade joystick problem

Can some help with this code...

#include <Stepper.h>

const int joyGroundPin = A0;  // Analog pin for joystick ground
const int joyLeftPin = 2;     // Digital pin for left
const int joyRightPin = 3;    // Digital pin for right
const int joyUpPin = 4;       // Digital pin for up
const int joyDownPin = 5;     // Digital pin for down

int joyLeftValue, joyRightValue, joyUpValue, joyDownValue;

// Define the number of steps per revolution
const int stepsPerRevolution = 200;

// Initialize Stepper objects with the corresponding pins on the motor driver
Stepper motorLR(stepsPerRevolution, 8, 10, 9, 11);  // Adjust pin numbers as needed
Stepper motorUD(stepsPerRevolution, 12, A1, 13, A2);  // Adjust pin numbers as needed

void setup() {
  pinMode(joyLeftPin, INPUT);
  pinMode(joyRightPin, INPUT);
  pinMode(joyUpPin, INPUT);
  pinMode(joyDownPin, INPUT);

  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  // Read joystick values
  joyLeftValue = digitalRead(joyLeftPin);
  joyRightValue = digitalRead(joyRightPin);
  joyUpValue = digitalRead(joyUpPin);
  joyDownValue = digitalRead(joyDownPin);

  // Print joystick values
  Serial.print("Left: ");
  Serial.print(joyLeftValue);
  Serial.print("\tRight: ");
  Serial.print(joyRightValue);
  Serial.print("\tUp: ");
  Serial.print(joyUpValue);
  Serial.print("\tDown: ");
  Serial.println(joyDownValue);

  // Map joystick values to motor steps
  int stepsLR = (joyLeftValue == HIGH) ? stepsPerRevolution : ((joyRightValue == HIGH) ? -stepsPerRevolution : 0);
  int stepsUD = (joyUpValue == HIGH) ? stepsPerRevolution : ((joyDownValue == HIGH) ? -stepsPerRevolution : 0);

  // Move the motors based on joystick input
  motorLR.step(stepsLR);
  motorUD.step(stepsUD);

  delay(100); // Adjust delay as needed for stability
}

I gives wrong results. It is just printing values without touching the joystick.



Random values like this and I didn't touch the joystick

You need pull-down resistors on each of the joystick direction pins. 1k ohm should be enough.

I added 1k resitor, restarted and it is still throwing values on the monitor. I am now thinking I make have connections wrong or the joystick is not working. I don't know exactly how to test it.

Post a wiring diagram please

I would use the internal pullup resisitors by setting the pinModes to INPUT_PULLUP and add a 0.1uF ceramic cap from the input to ground. The cap will bypass noise to ground and provide some hardware debounce.

If the readings are still not right, you may need stronger pullup. Stsrt with 10K external pullup and go lower from there. 1K minimum.


This may sound stupid to ask but I will ask anyways.. could you make a diagram, I am thinking now I may have the wiring wrong

Those resistors are not doing anything, they need to go from the joystick pin to ground. You have both sides of the resistor on the same rail. It’s as though the resistor isn’t even there.

Correction: you have a few mistakes… the Arduino needs to be connected to the ground rail along with those stepper modules. They need a common ground otherwise you will get proper readings or functionality.

I highly doubt that 9V battery will be enough for both servos. I don’t know if it’s even enough for one.


If this is what you mean..it is still giving the same results random values. Even a common ground. At the moment my focus is on getting the joystick to read when it is moved

Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Topic has been moved.

Your joystick wires are not in the correct pins. Your code says 2,3,4,5 but you have them in 9,10,11,12.

All your wires are incorrect actually. Is the code you posted the correct code or are the pictures old?

Apologies

I am very sorry. I did rearrange the pins but posted the old code. The code is the same I only changed the pins. My question now is, is this a digital joystick or analog. I got from AliExpress a long time ago not sure it came with a datasheet. I am confused right now because I can't find any helpful or working information online.

It looks like an old rocker switch joystick.

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