본문 바로가기

아두이노-스케치

조이스틱 값 확인


아날로그 조이스틱 모듈을 아두이노에서 사용하려면 어떻게 해야 하는지 알아보자.

조이스틱 모듈은 아래와 같이 생겼다.



keyes 의 조이스틱 모듈이다. 조이스틱 손잡이를 탈착할 수 있다.




아두이노 보드에 조이스틱을 연결하여 전후좌우 움직일 때 아날로그 값을  읽어본다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Arduino pin numbers
const int SW_pin = 2// digital pin connected to switch output
const int X_pin = 0// analog pin connected to X output
const int Y_pin = 1// analog pin connected to Y output
 
void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
}
 
void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}
 
cs