-1

I am trying to get my code to show a switch input from a joystick on an Arduino Uno.

When I view it on the serial monitor, I see the x and y axis being displayed when I move the joystick.

When I click it I do not get anything to show up. I have the switch set to pin 2 on the Arduino.

There are no errors when I compile the code.

int pin_x = A0; 
int position_x = 0;
int pin_y = A1;
int position_y = 0;
int pin_z = 2; // switch pin
int position_z = LOW;

void setup() {
    Serial.begin(9600);   // initialize serial communications at 9600 bps
    pinMode(pin_x, INPUT);  // set pin mod as INPUT
    pinMode(pin_y, INPUT);
    pinMode(pin_z, INPUT);  // switch pin
}
void read(){
    position_x = analogRead(pin_x);
    position_y = analogRead(pin_y);
    position_z = digitalRead(pin_z);
}
void show(){
Serial.print(" X:");  //print information to Serial Monitor 
Serial.print(position_x);
Serial.print(" Y:");
Serial.print(position_y);
Serial.print(" Z:");
Serial.print(position_z);
}
void loop() {
  read();
  show();
  delay(500); 
}
BoatingPuppy
  • 31
  • 1
  • 5

2 Answers2

0

There are several methods you could use to find the error, here are a couple suggestions:

  1. read the datasheet of your joystick
  2. use a multimeter to check conductivity of the two switch pins
  3. hook something else to your switch input, check that the input is working

That said, the way your code is written it expects your switch pin to be connected to Vcc, check if that is really the case.

wschopohl
  • 1,567
  • 1
  • 11
  • 18
0

Try this:

void setup() {
   Serial.begin(9600);   // initialize serial communications at 9600 bps
   pinMode(pin_x, INPUT);  // set pin mod as INPUT
   pinMode(pin_y, INPUT);
   pinMode(pin_z, INPUT);  // switch pin
   digitalWrite(pin_z, HIGH);
}

Otherwise code looks correct. The switch on a Joystick is indeed digital, while the others are analog, you got that right.

Here's a video on how to work with joysticks, maybe it helps: https://www.youtube.com/watch?v=MlDi0vO9Evg

JohnnyAwesome
  • 500
  • 3
  • 10