1. #include 2. #include 3. #include 4. 5. #define SW_PIN 5 6. #define VRX_PIN A1 7. #define VRY_PIN A0 8. 9. char salto = KEY_UP_ARROW; 10. char abajo = KEY_DOWN_ARROW; 11. char izquierda = KEY_LEFT_ARROW; 12. char derecha = KEY_RIGHT_ARROW; 13. char botonSpacio = 0x7A;//0x20 14. char botonX = 0x78; 15. char botonY = 0x0D; 16. int cont = 0; 17. Joystick* joystic; 18. 19. 20. void setup() { 21. Serial.begin(9600); 22. //Flechas de direccion 23. pinMode(2, INPUT); 24. pinMode(3, INPUT); 25. pinMode(4, INPUT); 26. pinMode(5, INPUT); 27. 28. //Botones de acción (espacio,X,Y) 29. pinMode(10, INPUT); 30. pinMode(6, INPUT); 31. pinMode(7, INPUT); 32. joystic = new AxisJoystick(SW_PIN, VRX_PIN, VRY_PIN); 33. Keyboard.begin(); 34. } 35. 36. void loop() { 37. //Eventos para botones de direccion 38. botonesMovimiento(); 39. //Eventos para botones de acción 40. botonesAccion(); 41. } 42. 43. void botonesMovimiento() { 44. 45. if (digitalRead(2) == LOW || joystic->isUp()) { 46. Keyboard.press(salto); 47. } 48. if (digitalRead(5) == LOW || joystic->isDown()) { 49. Keyboard.press(abajo); 50. } 51. if (digitalRead(3) == LOW || joystic->isLeft()) { 52. Keyboard.press(izquierda); 53. } 54. if (digitalRead(4) == LOW || joystic->isRight()) { 55. Keyboard.press(derecha); 56. } 57. 58. 59. 60. if (digitalRead(2) == HIGH && !joystic->isUp()) { 61. Keyboard.release(salto); 62. } 63. if (digitalRead(5) == HIGH && !joystic->isDown()) { 64. Keyboard.release(abajo); 65. } 66. if (digitalRead(3) == HIGH && !joystic->isLeft()) { 67. Keyboard.release(izquierda); 68. } 69. if (digitalRead(4) == HIGH && !joystic->isRight()) { 70. Keyboard.release(derecha); 71. } 72. } 73. 74. void botonesAccion() { 75. if (digitalRead(10) == LOW) { 76. Keyboard.press(botonSpacio); 77. } 78. 79. if (digitalRead(6) == LOW) { 80. Keyboard.press(botonX); 81. } 82. 83. if (digitalRead(7) == LOW) { 84. Keyboard.press(botonY); 85. } 86. 87. if (digitalRead(10) == HIGH) { 88. Keyboard.release(botonSpacio); 89. } 90. if (digitalRead(6) == HIGH) { 91. Keyboard.release(botonX); 92. } 93. if (digitalRead(7) == HIGH) { 94. Keyboard.release(botonY); 95. } 96. 97. }