Arduino_JoyStick

The joystick module is an extra module so requires a connection to the Arduino mainboard with wire jumpers. Three pins (pin number 9, 68 and 69) are used and 5V power should be supplied. The joystick module can detect which position the joystick faces (CENTER, LEFT, RIGHT, UP and DOWN).

Predefined Functions in Library
int analogRead(pin);
Set the address/mode (output) and initialize the joystick module.

  • parameter :
    pin : pin number of the module
  • return value : 0~1023 analogue value
/* joystick.ino */
#define JOY_X 68
#define JOY_Y 69
#define JOY_SEL 9

int xCenterPos = 0; 
int yCenterPos = 0; 
int xyTH = 100;

void setup() 
{ 
  pinMode(JOY_X, INPUT); 
  pinMode(JOY_Y, INPUT); 
  pinMode(JOY_SEL, INPUT);
  xCenterPos = analogRead(JOY_X); 
  yCenterPos = analogRead(JOY_Y);
  Serial.begin(115200);
}

void loop() 
{
  if(analogRead(JOY_X) < xCenterPos - xyTH)  { Serial.print("LEFT");} 
  else if(analogRead(JOY_X) > xCenterPos + xyTH) { Serial.print("RIGHT");}   
  else { Serial.print("CENTER");}
  Serial.print(" ");
  if(analogRead(JOY_Y) < yCenterPos - xyTH) { Serial.println("UP");} 
  else if(analogRead(JOY_Y) > yCenterPos + xyTH) { Serial.println("DOWN");} 
  else { Serial.println("CENTER");}

  if(digitalRead(JOY_SEL) == LOW) { Serial.println("Button Pushed");}
  delay(500);
}

你可能感兴趣的:(Arduino_JoyStick)