Devices & Components
Arduino Esplora
Hardware & Tools
Arduino Esplora
Joystick Library
Software & Tools
Arduino IDE
Any game with joystick support
Gamepad Tester
Project description
Code
JStest.ino
arduino
Upload it to the Eplora using Arduino IDE
1#include <Joystick.h> 2#include <Esplora.h> 3int lastButtonState[4] = {0, 0, 0, 0}; 4int currentButtonState, valJS, valJSprev, mode, Xprev, Yprev, Xval, Yval; 5String chg; 6boolean hasChanges = true; 7Joystick_ Joystick(0x03, 0x04, 36, 0, true, true, true, true, true, true, false, false, false, false, false);; 8void setup() { 9 // put your setup code here, to run once: 10 Joystick.begin(false); 11 Serial.begin(9600); 12 Esplora.tone(261); 13 delay(200); 14 Esplora.tone(65); 15 delay(400); 16 Esplora.tone(440); 17 delay(200); 18 Esplora.noTone(); 19} 20 21void loop() { 22 // put your main code here, to run repeatedly: 23 Joystick.setXAxisRange(-512, 512); 24 Joystick.setYAxisRange(-512, 512); 25 Joystick.setRxAxisRange(-512, 512); 26 Joystick.setRyAxisRange(-512, 512); 27 Joystick.setRzAxisRange(-512, 512); 28 Joystick.setZAxisRange(0, 5); 29 Xprev = Xval; 30 Yprev = Yval; 31 Xval = Esplora.readJoystickX(); 32 Yval = Esplora.readJoystickY(); 33 mode = map(Esplora.readSlider(), 0, 1023, 1, 3); 34 hasChanges = false; 35 mode = map(Esplora.readSlider(), 0, 1023, 1, 3); 36 if (round(Xval / 2) != round(Xprev / 2) || round(Yval / 2) != round(Yprev / 2)) 37 { 38 if (mode == 1) 39 { 40 Joystick.setXAxis(Xval); 41 Joystick.setYAxis(Yval); 42 hasChanges = true; 43 chg = "Joystick"; 44 } 45 else 46 { 47 Joystick.setRxAxis(Xval); 48 Joystick.setRyAxis(Yval); 49 hasChanges = true; 50 chg = "Joystick"; 51 } 52 } 53 for (int index = 0; index < 4; index++) 54 { 55 if (mode == 1) 56 { 57 currentButtonState = !Esplora.readButton(index + 1); 58 if (currentButtonState != lastButtonState[index]) 59 { 60 hasChanges = true; 61 chg = "BTN"; 62 Joystick.setButton(index, currentButtonState); 63 lastButtonState[index] = currentButtonState; 64 } 65 } 66 if (mode != 1) 67 { 68 currentButtonState = !Esplora.readButton(index + 1); 69 if (currentButtonState != lastButtonState[index]) 70 { 71 hasChanges = true; 72 chg = "BTN"; 73 Joystick.setButton(index + 11, currentButtonState); 74 lastButtonState[index] = currentButtonState; 75 } 76 } 77 } 78 valJSprev = valJS; 79 if (Esplora.readJoystickSwitch() == LOW) 80 { 81 82 valJS = 1; 83 } 84 else 85 { 86 valJS = 0; 87 } 88 if (mode == 1 && valJSprev != valJS) 89 { 90 Joystick.setButton(10, valJS); 91 hasChanges = true; 92 chg = "BTNJS"; 93 } 94 else 95 { 96 if (mode != 1 && valJSprev != valJS) 97 { 98 Joystick.setButton(20, valJS); 99 hasChanges = true; 100 chg = "BTNJS"; 101 } 102 } 103 if (hasChanges) 104 { 105 Joystick.sendState(); 106 if(Serial) 107 { 108 Serial.println(chg); 109 } 110 } 111 if (mode == 1) 112 { 113 Esplora.writeRGB(255, 0, 0); 114 } 115 else 116 { 117 Esplora.writeRGB(0, 255, 0); 118 } 119 delay(50); 120} 121
Comments
Only logged in users can leave comments