// define the pins for X and Y axis of the joystick, and the switch pin
int Xpin = A0;
int Ypin = A1;
int Spin = 2;
// variables to store the joystick and switch values
int Xval;
int Yval;
int Sval;
// define the delay time between readings
int dt = 200;
void setup() {
Serial.begin(9600); // initialize the serial monitor
pinMode(Xpin, INPUT); // set Xpin as input
pinMode(Ypin, INPUT); // set Ypin as input
pinMode(Spin, INPUT_PULLUP); // set Spin as input with pull-up resistor
}
void loop() {
// read the values of X and Y axis of the joystick and the switch
Xval = analogRead(Xpin);
Yval = analogRead(Ypin);
Sval = digitalRead(Spin);
// delay for a short time
delay(dt);
// print the values of X and Y axis of the joystick and the switch state to serial monitor
Serial.print("X Value = ");
Serial.print(Xval);
Serial.print(", Y Value = ");
Serial.print(Yval);
Serial.print(", Switch State is ");
Serial.println(Sval);
}