computermuseo

joystick read analog arduino

Apr 16th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. int xPin = A1;
  2. int yPin = A0;
  3. int buttonPin = 2;
  4.  
  5. int xPosition = 0;
  6. int yPosition = 0;
  7. int buttonState = 0;
  8.  
  9. void setup() {
  10. // initialize serial communications at 9600 bps:
  11. Serial.begin(9600);
  12.  
  13. pinMode(xPin, INPUT);
  14. pinMode(yPin, INPUT);
  15.  
  16. //activate pull-up resistor on the push-button pin
  17. pinMode(buttonPin, INPUT_PULLUP);
  18.  
  19. // For versions prior to Arduino 1.0.1
  20. // pinMode(buttonPin, INPUT);
  21. // digitalWrite(buttonPin, HIGH);
  22.  
  23. }
  24.  
  25. void loop() {
  26. xPosition = analogRead(xPin);
  27. yPosition = analogRead(yPin);
  28. buttonState = digitalRead(buttonPin);
  29.  
  30. Serial.print("X: ");
  31. Serial.print(xPosition);
  32. Serial.print(" | Y: ");
  33. Serial.print(yPosition);
  34. Serial.print(" | Button: ");
  35. Serial.println(buttonState);
  36.  
  37. delay(100); // add some delay between reads
  38. }
Add Comment
Please, Sign In to add comment