공대생 주석마루/아두이노 UNO

(9) 조이스틱 Joystick

주석마루 2018. 2. 27. 20:47

-조이스틱이란

움직임을 x축, y축으로 나눠 입력 받고

버튼 클릭을 입력 받는 장치를 말한다,

구성품으로는

아두이노 우노, 연결선, 조이스틱 이다.


-코드는

void setup() {
  // put your setup code here, to run once:
pinMode(2,INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
int x = analogRead(A0);
int y = analogRead(A1);
Serial.println("X= ");
Serial.println(x);
Serial.println("Y= ");
Serial.println(y);

int bb =digitalRead(3);
if(bb == HIGH)
{
  Serial.println("button off");
}
else
{
  Serial.println("button on");
}
delay(300);
}

버튼 클릭을 2번 핀으로 입력 받는데

풀업저항 기능을 사용하기 때문에

버튼이 눌리면 LOW

버튼이 눌리지 않으면 HIGH 이다.


아날로그 입력으로 x축(A0핀), y축(A1핀) 입력을 받는다.

이를 시리얼 모니터를 통해 확인 할 수 있다.


-연결

GND = digital GND핀

+5V = +5V핀

VRx VRy = 아날로그핀(0~1023 입력값)

SW = digital I/O 핀


-동영상