Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <Joystick.h>
  2.  
  3. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, //Set up for joystick
  4. 1, 0, // button counter values
  5. true, true, false, // axes (x,y,z)
  6. false, false, false, // no Rx, Ry, or Rz (rotation around respective axis)
  7. false, false, // no rudder or throttle
  8. false, false, false); // no accelerator, brake, or steering
  9.  
  10. void setup() {
  11. // initialize button pins
  12. pinMode(2, INPUT_PULLUP); // pin 2 = UP
  13. pinMode(3, INPUT_PULLUP); // pin 3 = RIGHT
  14. pinMode(4, INPUT_PULLUP); // pin 4 = DOWN
  15. pinMode(5, INPUT_PULLUP); // pin 5 = LEFT
  16.  
  17. Joystick.begin();
  18. Joystick.setXAxisRange(-1, 1);
  19. Joystick.setYAxisRange(-1, 1);
  20. }
  21.  
  22. // button state array
  23. int lastButtonState[4] = {0,0,0,0};
  24.  
  25. void loop() {
  26. // read in pin values
  27. for (int index = 0; index < 4; index++){
  28. int currentButtonState = !digitalRead(index + 2);
  29. if (currentButtonState != lastButtonState[index]){
  30. switch (index) {
  31. case 0: // UP
  32. if (currentButtonState == 1) {
  33. Joystick.setYAxis(-1);
  34. } else {
  35. Joystick.setYAxis(0);
  36. }
  37. break;
  38. case 1: // RIGHT
  39. if (currentButtonState == 1) {
  40. Joystick.setXAxis(1);
  41. } else {
  42. Joystick.setXAxis(0);
  43. }
  44. break;
  45. case 2: // DOWN
  46. if (currentButtonState == 1) {
  47. Joystick.setYAxis(1);
  48. } else {
  49. Joystick.setYAxis(0);
  50. }
  51. break;
  52. case 3: // LEFT
  53. if (currentButtonState == 1) {
  54. Joystick.setXAxis(-1);
  55. } else {
  56. Joystick.setXAxis(0);
  57. }
  58. break;
  59. }
  60. lastButtonState[index] = currentButtonState;
  61. }
  62. }
  63. delay(10);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement