]> git.piffa.net Git - sketchbook_andrea/blob - hardware/joystick/joystick.ino
Pre Vicenza
[sketchbook_andrea] / hardware / joystick / joystick.ino
1 /* Joystick
2 Utilizzo di un joystick analogico con arduino.
3 da: http://42bots.com/tutorials/arduino-joystick-module-example/
4
5 Il joystich ha 2 potenziometri analogici e un interruttore digitale
6 corrispondente alla pressione dello stick (input momentaneo).
7  
8  */
9 int xPin = A1;
10 int yPin = A0;
11 int buttonPin = 2;
12
13 int xPosition = 0;
14 int yPosition = 0;
15 int buttonState = 0;
16
17 void setup() {
18   // initialize serial communications at 9600 bps:
19   Serial.begin(9600); 
20   
21   pinMode(xPin, INPUT);
22   pinMode(yPin, INPUT);
23
24   //activate pull-up resistor on the push-button pin
25   pinMode(buttonPin, INPUT_PULLUP); 
26   
27   // For versions prior to Arduino 1.0.1
28   // pinMode(buttonPin, INPUT);
29   // digitalWrite(buttonPin, HIGH);
30   
31 }
32
33 void loop() {
34   xPosition = analogRead(xPin);
35   yPosition = analogRead(yPin);
36   buttonState = digitalRead(buttonPin);
37   
38   Serial.print("X: ");
39   Serial.print(xPosition);
40   Serial.print(" | Y: ");
41   Serial.print(yPosition);
42   Serial.print(" | Button: ");
43   Serial.println(buttonState);
44
45   delay(100); // add some delay between reads
46 }