Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Joystick.h>
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, //Set up for joystick
- 1, 0, // button counter values
- true, true, false, // axes (x,y,z)
- false, false, false, // no Rx, Ry, or Rz (rotation around respective axis)
- false, false, // no rudder or throttle
- false, false, false); // no accelerator, brake, or steering
- void setup() {
- // initialize button pins
- pinMode(2, INPUT_PULLUP); // pin 2 = UP
- pinMode(3, INPUT_PULLUP); // pin 3 = RIGHT
- pinMode(4, INPUT_PULLUP); // pin 4 = DOWN
- pinMode(5, INPUT_PULLUP); // pin 5 = LEFT
- Joystick.begin();
- Joystick.setXAxisRange(-1, 1);
- Joystick.setYAxisRange(-1, 1);
- }
- // button state array
- int lastButtonState[4] = {0,0,0,0};
- void loop() {
- // read in pin values
- for (int index = 0; index < 4; index++){
- int currentButtonState = !digitalRead(index + 2);
- if (currentButtonState != lastButtonState[index]){
- switch (index) {
- case 0: // UP
- if (currentButtonState == 1) {
- Joystick.setYAxis(-1);
- } else {
- Joystick.setYAxis(0);
- }
- break;
- case 1: // RIGHT
- if (currentButtonState == 1) {
- Joystick.setXAxis(1);
- } else {
- Joystick.setXAxis(0);
- }
- break;
- case 2: // DOWN
- if (currentButtonState == 1) {
- Joystick.setYAxis(1);
- } else {
- Joystick.setYAxis(0);
- }
- break;
- case 3: // LEFT
- if (currentButtonState == 1) {
- Joystick.setXAxis(-1);
- } else {
- Joystick.setXAxis(0);
- }
- break;
- }
- lastButtonState[index] = currentButtonState;
- }
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement