1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

DIY Arduino Buttonbox

Discussion in 'DIY peripherals' started by TOPMO3, Mar 28, 2016.

  1. clabit

    clabit New Member

    Joined:
    May 12, 2020
    Messages:
    4
    Balance:
    29Coins
    Ratings:
    +1 / 0 / -0

    Thanks
    I am attaching the project and sketch

    Attached Files:

    • Like Like x 1
  2. JonBakhol

    JonBakhol Active Member

    Joined:
    Mar 16, 2018
    Messages:
    107
    Location:
    Netherlands harderwijk
    Balance:
    442Coins
    Ratings:
    +46 / 1 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
  3. clabit

    clabit New Member

    Joined:
    May 12, 2020
    Messages:
    4
    Balance:
    29Coins
    Ratings:
    +1 / 0 / -0
  4. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    Hello everyone. First of all thank a fellow forum member ((JonBakhol)). For all the help you have given me during my project. Thanks to him, I have learned a little more about how the Arduino world works.
    But "Jon" is also human and although we have done many tests, I still have the problem.

    I have a 32 button keypad. Here I leave the drawing.
    https://i.imgur.com/NWP6FwY.png

    And I have loaded on my pro micro board ((leonardo)). This sketch

    Code:
    //BUTTON BOX
    //USE w ProMicro
    //Tested in WIN10 + Assetto Corsa
    //AMSTUDIO
    //20.8.17
    
    #include <Keypad.h>
    #include <Joystick.h>
    
    #define ENABLE_PULLUPS
    #define NUMROTARIES 4
    #define NUMBUTTONS 32
    #define NUMROWS 8
    #define NUMCOLS 4
    
    
    byte buttons[NUMROWS][NUMCOLS] = {
      {0,1,2,3},
      {4,5,6,7},
      {8,9,10,11},
      {12,13,14,15},
      {16,17,18,19},
      {20,21,22,23},
      {24,25,26,27},
      {28,29,30,31},
    
     
    };
    
    struct rotariesdef {
      byte pin1;
      byte pin2;
      int ccwchar;
      int cwchar;
      volatile unsigned char state;
    };
    
    rotariesdef rotaries[NUMROTARIES] {
      {0,1,32,33,0},
      {2,3,34,35,0},
      {4,5,36,37,0},
      {6,7,38,39,0},
    };
    
    #define DIR_CCW 0x10
    #define DIR_CW 0x20
    #define R_START 0x0
    
    #ifdef HALF_STEP
    #define R_CCW_BEGIN 0x1
    #define R_CW_BEGIN 0x2
    #define R_START_M 0x3
    #define R_CW_BEGIN_M 0x4
    #define R_CCW_BEGIN_M 0x5
    const unsigned char ttable[6][4] = {
      // R_START (00)
      {R_START_M,            R_CW_BEGIN,     R_CCW_BEGIN,  R_START},
      // R_CCW_BEGIN
      {R_START_M | DIR_CCW, R_START,        R_CCW_BEGIN,  R_START},
      // R_CW_BEGIN
      {R_START_M | DIR_CW,  R_CW_BEGIN,     R_START,      R_START},
      // R_START_M (11)
      {R_START_M,            R_CCW_BEGIN_M,  R_CW_BEGIN_M, R_START},
      // R_CW_BEGIN_M
      {R_START_M,            R_START_M,      R_CW_BEGIN_M, R_START | DIR_CW},
      // R_CCW_BEGIN_M
      {R_START_M,            R_CCW_BEGIN_M,  R_START_M,    R_START | DIR_CCW},
    };
    #else
    #define R_CW_FINAL 0x1
    #define R_CW_BEGIN 0x2
    #define R_CW_NEXT 0x3
    #define R_CCW_BEGIN 0x4
    #define R_CCW_FINAL 0x5
    #define R_CCW_NEXT 0x6
    
    const unsigned char ttable[7][4] = {
      // R_START
      {R_START,    R_CW_BEGIN,  R_CCW_BEGIN, R_START},
      // R_CW_FINAL
      {R_CW_NEXT,  R_START,     R_CW_FINAL,  R_START | DIR_CW},
      // R_CW_BEGIN
      {R_CW_NEXT,  R_CW_BEGIN,  R_START,     R_START},
      // R_CW_NEXT
      {R_CW_NEXT,  R_CW_BEGIN,  R_CW_FINAL,  R_START},
      // R_CCW_BEGIN
      {R_CCW_NEXT, R_START,     R_CCW_BEGIN, R_START},
      // R_CCW_FINAL
      {R_CCW_NEXT, R_CCW_FINAL, R_START,     R_START | DIR_CCW},
      // R_CCW_NEXT
      {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
    };
    #endif
    
    byte rowPins[NUMROWS] = {21,20,19,18,15,14,16,10};
    byte colPins[NUMCOLS] = {9,8,7,6};
    
    Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
    
    Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
      JOYSTICK_TYPE_JOYSTICK, 32, 0,
      false, false, false, false, false, false,
      false, false, false, false, false);
    
    void setup() {
      Joystick.begin();
      rotary_init();}
    
    void loop() {
    
      CheckAllEncoders();
    
      CheckAllButtons();
    
    }
    
    void CheckAllButtons(void) {
          if (buttbx.getKeys())
        {
           for (int i=0; i<LIST_MAX; i++)
            {
               if ( buttbx.key[i].stateChanged )
                {
                switch (buttbx.key[i].kstate) {
                        case PRESSED:
                        case HOLD:
                                  Joystick.setButton(buttbx.key[i].kchar, 1);
                                  break;
                        case RELEASED:
                        case IDLE:
                                  Joystick.setButton(buttbx.key[i].kchar, 0);
                                  break;
                }
               }
             }
         }
    }
    
    
    void rotary_init() {
      for (int i=0;i<NUMROTARIES;i++) {
        pinMode(rotaries[i].pin1, INPUT);
        pinMode(rotaries[i].pin2, INPUT);
        #ifdef ENABLE_PULLUPS
          digitalWrite(rotaries[i].pin1, HIGH);
          digitalWrite(rotaries[i].pin2, HIGH);
        #endif
      }
    }
    
    
    unsigned char rotary_process(int _i) {
       unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
      rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
      return (rotaries[_i].state & 0x30);
    }
    
    void CheckAllEncoders(void) {
      for (int i=0;i<NUMROTARIES;i++) {
        unsigned char result = rotary_process(i);
        if (result == DIR_CCW) {
          Joystick.setButton(rotaries[i].ccwchar, 1); delay(50); Joystick.setButton(rotaries[i].ccwchar, 0);
        };
        if (result == DIR_CW) {
          Joystick.setButton(rotaries[i].cwchar, 1); delay(50); Joystick.setButton(rotaries[i].cwchar, 0);
        };
      }
    }

    The buttons all work. Windows recognizes the board as a gaming device. Everything is correct, but the buttons do not work well. There are buttons that activate, its corresponding button number, but there are others that activate 2 buttons, other buttons activate 3 buttons. I have reviewed everything and it seems to be fine, the cables correspond to the drawing. There are no cables that are touching each other and make a false connection.
    That is why I ask for help, to see if someone else can help me. Greetings to all and thanks.
  5. rt45433

    rt45433 New Member

    Joined:
    Sep 18, 2013
    Messages:
    4
    Balance:
    291Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    3DOF
    Hi there, I have been having a play with the project and have a basic understanding of this. I have 3d printed a flight sim cessna trim wheel that uses one rotary encoder. Would anyone be kind enough to put together the sketch for one rotary encoder used on a pro micro and show up as a game controller in windows. I am happy to gift £10 for anyone time on this via paypal to say thank you.
  6. dixion

    dixion New Member

    Joined:
    Jun 3, 2020
    Messages:
    2
    Balance:
    61Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    DC motor, Arduino
    Hi, can anyone help me with this code using Arduino IDE for ESP32 development board.

    Code:
    #include <ESP32Encoder.h>     // https://github.com/madhephaestus/ESP32Encoder/
    #include <Keypad.h>           // https://github.com/Chris--A/Keypad
    #include <BleGamepad.h>       // https://github.com/MagnusThome/ESP32-BLE-Gamepad
    
    
    BleGamepad bleGamepad("RaceKeys", "Arduino", 100);
    
    
    ////////////////////// BUTTON MATRIX //////////////////////
    #define ROWS 4
    #define COLS 4
    uint8_t rowPins[ROWS] = {27, 25, 12, 32};
    uint8_t colPins[COLS] = {17, 16, 00, 02};
    uint8_t keymap[ROWS][COLS] = {
      {0,1,2,3},
      {4,5,6,7},
      {8,9,10,11},
      {12,13,14,15}
    };
    Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS);
    
    
    //////////// ROTARY ENCODERS (WITH PUSH SWITCHES) ////////////
    #define MAXENC 5
    uint8_t uppPin[MAXENC] = {36, 22, 39, 34, 19};
    uint8_t dwnPin[MAXENC] = {26, 21, 35, 14, 23};
    uint8_t prsPin[MAXENC] = {18, 15, 33, 13, 05};
    uint8_t encoderUpp[MAXENC] = {16,19,22,25,28};
    uint8_t encoderDwn[MAXENC] = {17,20,23,26,29};
    uint8_t encoderPrs[MAXENC] = {18,21,24,27,30};
    ESP32Encoder encoder[MAXENC];
    unsigned long holdoff[MAXENC] = {0,0,0,0,0};
    int32_t prevenccntr[MAXENC] = {0,0,0,0,0};
    bool prevprs[MAXENC] = {0,0,0,0,0};
    #define HOLDOFFTIME 150   // TO PREVENT MULTIPLE ROTATE "CLICKS" WITH CHEAP ENCODERS WHEN ONLY ONE CLICK IS INTENDED
    
    
    
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
    
    void setup() {
      Serial.begin(115200);
    
      for (uint8_t i=0; i<MAXENC; i++) {
        encoder[i].clearCount();
        encoder[i].attachSingleEdge(dwnPin[i], uppPin[i]);
        pinMode(prsPin[i], INPUT_PULLUP);
      }
      customKeypad.addEventListener(keypadEvent);
      //customKeypad.setHoldTime(1);
      bleGamepad.begin();
      Serial.println("Booted!");
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
    
    void loop() {
    
      unsigned long now = millis();
    
    
      // -- ROTARY ENCODERS : ROTATION -- //
    
      for (uint8_t i=0; i<MAXENC; i++) {
        int32_t cntr = encoder[i].getCount();
        if (cntr!=prevenccntr[i]) {
          if (!holdoff[i]) {
            if (cntr>prevenccntr[i]) { sendKey(encoderUpp[i]); }
            if (cntr<prevenccntr[i]) { sendKey(encoderDwn[i]); }
            holdoff[i] = now;
            if (holdoff[i]==0) holdoff[i] = 1;  // SAFEGUARD WRAP AROUND OF millis() (WHICH IS TO 0) SINCE holdoff[i]==0 HAS A SPECIAL MEANING ABOVE
          }
          else if (now-holdoff[i] > HOLDOFFTIME) {
            prevenccntr[i] = encoder[i].getCount();
            holdoff[i] = 0;
          }
        }
        
      // -- ROTARY ENCODERS : PUSH SWITCH -- //
    
        bool pressed = !digitalRead(prsPin[i]);
        if (pressed != prevprs[i]) {
          if (pressed) {  // PRESSED
            pressKey(encoderPrs[i]);
          }
          else {          // RELEASED
            releaseKey(encoderPrs[i]);
          }
          prevprs[i] = !prevprs[i];
        }
      }
    
      customKeypad.getKey();    // READ BUTTON MATRIX (EVENT CALLBACK SETUP)
    
      delay(10);
     
    }
    
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
    
    void keypadEvent(KeypadEvent key){
      uint8_t keystate = customKeypad.getState();
      if (keystate==PRESSED)  { pressKey(key); }
      if (keystate==RELEASED) { releaseKey(key); }
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
    
    void sendKey(uint8_t key) {
        uint32_t gamepadbutton = pow(2,key);      // CONVERT TO THE BINARY MAPPING GAMEPAD KEYS USE
        Serial.print("pulse\t");
        Serial.println(key);
        if(bleGamepad.isConnected()) {
          bleGamepad.press(gamepadbutton);
          delay(150);
          bleGamepad.release(gamepadbutton);
        }
    }
    
    void pressKey(uint8_t key) {
        uint32_t gamepadbutton = pow(2,key);      // CONVERT TO THE BINARY MAPPING GAMEPAD KEYS USE
        Serial.print("hold\t");
        Serial.println(key);
        if(bleGamepad.isConnected()) {
          bleGamepad.press(gamepadbutton);
        }
    }
    
    void releaseKey(uint8_t key) {
        uint32_t gamepadbutton = pow(2,key);      // CONVERT TO THE BINARY MAPPING GAMEPAD KEYS USE
        Serial.print("release\t");
        Serial.println(key);
        if(bleGamepad.isConnected()) {
          bleGamepad.release(gamepadbutton);
        }
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////
  7. dixion

    dixion New Member

    Joined:
    Jun 3, 2020
    Messages:
    2
    Balance:
    61Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    DC motor, Arduino
  8. Angelo Spano

    Angelo Spano New Member

    Joined:
    Jan 9, 2021
    Messages:
    2
    Balance:
    3Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino

    sorry ... it is possible to insert 2 potentiometers in the project, thanks for the reply
  9. Angelo Spano

    Angelo Spano New Member

    Joined:
    Jan 9, 2021
    Messages:
    2
    Balance:
    3Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino

    I would like to ask if there is anyone who is able to modify the code by adding another potentiometer, unfortunately I am new to arduino ... I have tried but I have not succeeded. thanks to those who want to help me.
  10. Nesquik

    Nesquik New Member

    Joined:
    Jan 28, 2021
    Messages:
    1
    Balance:
    18Coins
    Ratings:
    +0 / 0 / -0
    Hey, friends, I have a poor experience with an Arduino as well as with micro-electronics in general. I made my button-box by this guide successfully, but I would like to use a button with a 5v indicator so I've been trying to connect It to VCC and GND of my Pro Micro to give the button additional voltage, but as soon as It happened the box became working incorrectly and windows assign a few button positions to one physical button of the box. How I could fix It?
  11. dillario

    dillario New Member

    Joined:
    Nov 3, 2021
    Messages:
    1
    Balance:
    17Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    Hello. I am new here and need your help . I downloaded the Buttonbox Sketch from Amstudio. I wanted to ask if anyone can help me change it so that I can connect 2 Hall sensors? Need that for the clutch on the steering wheel. Thank you
  12. Paulinho_mi02

    Paulinho_mi02 New Member

    Joined:
    Dec 1, 2021
    Messages:
    1
    Balance:
    18Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    Arduino
    I tested as a keyboard on XBOX One and the rotating ones worked perfectly in the Dash console menu, but unfortunately the rotating did not work perfectly in the game, I testedf1 2021. I tried to use the rotarys in fuel adjustment and brake and differential bias but the rotarys worked late and with failures. It seems that the game does not communicate properly with the rotating as well as the Dash console menu. Did you take the ps4 test?
  13. tina540

    tina540 New Member

    Joined:
    Apr 6, 2022
    Messages:
    14
    Balance:
    - 44Coins
    Ratings:
    +3 / 1 / -0
    My Motion Simulator:
    2DOF
    Hi guys, here is the drawing of my button box, I wanted to know if the wiring is good and which library and other I must install on the arduino so that it works with my diagram. thank you

    Attached Files:

  14. tina540

    tina540 New Member

    Joined:
    Apr 6, 2022
    Messages:
    14
    Balance:
    - 44Coins
    Ratings:
    +3 / 1 / -0
    My Motion Simulator:
    2DOF
    I don't know if my photo is uploaded
  15. moaale

    moaale IAN MRTIN

    Joined:
    Jun 29, 2011
    Messages:
    168
    Occupation:
    NN
    Location:
    ARGENTINA
    Balance:
    124Coins
    Ratings:
    +41 / 0 / -0
    My Motion Simulator:
    2DOF, DC motor, SimforceGT
    New
    Could someone give me a hand... I want to load this code on an Arduino micro and it gives me this error
    Captura de pantalla 2023-10-28 214605.png
    Last edited by a moderator: Oct 29, 2023
  16. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,097
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    148,264Coins
    Ratings:
    +10,893 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    Try using #include instead.
  17. evolaco

    evolaco Member

    Joined:
    Sep 27, 2015
    Messages:
    260
    Location:
    Spain
    Balance:
    1,070Coins
    Ratings:
    +29 / 0 / -0
    My Motion Simulator:
    3DOF
    Is that code in Spanish? have you translated it? Those orders and libraries seem to be translated from English to Spanish.
  18. moaale

    moaale IAN MRTIN

    Joined:
    Jun 29, 2011
    Messages:
    168
    Occupation:
    NN
    Location:
    ARGENTINA
    Balance:
    124Coins
    Ratings:
    +41 / 0 / -0
    My Motion Simulator:
    2DOF, DC motor, SimforceGT
    They are the ones I copied from page 10 that the creator put.. I don't understand why it gives me this error
  19. moaale

    moaale IAN MRTIN

    Joined:
    Jun 29, 2011
    Messages:
    168
    Occupation:
    NN
    Location:
    ARGENTINA
    Balance:
    124Coins
    Ratings:
    +41 / 0 / -0
    My Motion Simulator:
    2DOF, DC motor, SimforceGT
    Could you give me a hand... because I get this error like the word joystick has something wrong... I want to load the code into my arduino just as you put it in voice Captura de pantalla 2023-10-28 214605.png
  20. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    21,097
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    148,264Coins
    Ratings:
    +10,893 / 54 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    The code on page 10 is in English, the code you posted is not.