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. JonBakhol

    JonBakhol Active Member

    Joined:
    Mar 16, 2018
    Messages:
    107
    Location:
    Netherlands harderwijk
    Balance:
    462Coins
    Ratings:
    +46 / 1 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    Hi guys. Just want to share my project of a new steering wheel for an Fanatec base. First you have to emulate an Fanatec wheel otherwise you won't have any FFB.
    I used an Teensy 3.1 (arduino board) and followed the description which can be found on thingiverse ( https://www.thingiverse.com/thing:3673632)
    I printed the back of the wheel which contains the Fanatec connection, 2 magnetic shifters and an clutch. (.stl is in the .RAR file)

    I also printed the wheel found on thingiverse (https://www.thingiverse.com/thing:3538862) and made some changes to it.
    Steering wheel thingiverse.jpg

    I found an almost ready fritzing for the steering wheel. I had to put in an extra potentiometer for the clutch.
    Buttonbox steering wheel.jpg
    As you can see there are more buttons and potentiometers on the wheel then there are in the fritzing. This is because I made the wheel symmetric. So the 6 buttons (left and right) on the side have the same function. And also the top encoders and the encoders in the handles have the same function. So there are 15 buttons (2x shifter, 2 times button in handle, 2 buttons of the encoders in the middle, 3 buttons on the bottom and 6 buttons on the side)and 4 encoders (2 in the middle, 1 on top of the handle and 1 in the middle of the handle)

    I used an Arduino Micro (not an pro-micro) for controlling all the buttons, joystick, potmeter (clutch) and the encoders.
    I'm not the best programmer with the arduino, but after some trail and error I finally got it working. (If anyone can simplify the code......be my guest)

    Code:
    //BUTTON BOX
    //Arduino Micro
    //Tested in WIN10 + Assetto Corsa
    
    
    #include <Keypad.h>
    #include <Joystick.h>
    #include <Mouse.h>
    
    #define ENABLE_PULLUPS
    #define NUMROTARIES 4
    #define NUMBUTTONS 15
    #define NUMROWS 3
    #define NUMCOLS 5
    
    int horzPin = A5;  // Analog output of horizontal joystick pin
    int vertPin = A4;  // Analog output of vertical joystick pin
    int selPin = 13;  // select button pin of joystick
    
    int vertZero, horzZero;  // Stores the initial value of each axis, usually around 512
    int vertValue, horzValue;  // Stores current analog output of each axis
    const int sensitivity = 200;  // Higher sensitivity value = slower mouse, should be <= about 500
    int mouseClickFlag = 0;
    
    byte buttons[NUMROWS][NUMCOLS] = {
      {0,1,2,3,4},
      {5,6,7,8,9},
      {10,11,12,13,14},
     
      };
    
    struct rotariesdef {
      byte pin1;
      byte pin2;
      int ccwchar;
      int cwchar;
      volatile unsigned char state;
    };
    
    rotariesdef rotaries[NUMROTARIES] {
      {1,0,15,16,0},
      {2,3,17,18,0},
      {4,5,19,20,0},
      {6,7,21,22,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] = {A3,A2,A1};
    byte colPins[NUMCOLS] = {A8,A9,A10,11,A11};
    
    Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
    
    Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
      JOYSTICK_TYPE_JOYSTICK, 32, 0,
      true, true, true, true, true, true,
      true, true, true, true, true);
    
    void setup() {
    pinMode(A0, INPUT); //Hetzelfde als const int & int pot =
      Joystick.begin();
      rotary_init();
      pinMode(horzPin, INPUT);  // Set both analog pins as inputs
      pinMode(vertPin, INPUT);
      pinMode(selPin, INPUT);  // set button select pin as input
      digitalWrite(selPin, HIGH);  // Pull button select pin high
      delay(1000);  // short delay to let outputs settle
      vertZero = analogRead(vertPin);  // get the initial values
      horzZero = analogRead(horzPin);  // Joystick should be in neutral position when reading these
    
    }
    
    const int pinToButtonMap = A0;
    
    void loop() {
    
      vertValue = analogRead(vertPin) - vertZero;  // read vertical offset
      horzValue = analogRead(horzPin) - horzZero;  // read horizontal offset
    
      if (vertValue != 0)
        Mouse.move(0, vertValue/sensitivity, 0);  // move mouse on y axis
      if (horzValue != 0)
        Mouse.move(horzValue/sensitivity, 0, 0);  // move mouse on x axis
    
      if ((digitalRead(selPin) == 0) && (!mouseClickFlag))  // if the joystick button is pressed
      {
        mouseClickFlag = 1;
        Mouse.press(MOUSE_LEFT);  // click the left button down
      }
      else if ((digitalRead(selPin))&&(mouseClickFlag)) // if the joystick button is not pressed
      {
        mouseClickFlag = 0;
        Mouse.release(MOUSE_LEFT);  // release the left button
      }
    
    int pot = analogRead(A0);
    int mapped = map(pot,0,1023,0,255);
    {Joystick.setThrottle(mapped);}
    
      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);
        };
      }
    }

    Attached Files:

    • Like Like x 2
    Last edited: May 12, 2020
  2. HoiHman

    HoiHman Active Member

    Joined:
    Feb 1, 2015
    Messages:
    211
    Occupation:
    Mechanic
    Location:
    The Netherlands
    Balance:
    330Coins
    Ratings:
    +143 / 1 / -0
    Does anybody have a sketch for the arduino that works with the CTS288 rotaries?


    If i select "half step" i need to need do 2 clicks before one is registered
    If i select "quarter step" i need to do 4 clicks before one is registered


    I need a 'full step" sketch i think:confused:
  3. Chris Indeherberge

    Chris Indeherberge Designer / dreamer

    Joined:
    Aug 1, 2018
    Messages:
    13
    Occupation:
    Graphics Designer
    Location:
    Belgium
    Balance:
    184Coins
    Ratings:
    +3 / 0 / -0
    I use this library/code for CTS288 encoders: https://www.pjrc.com/teensy/td_libs_Encoder.html
    Also take a look at which pins to use as interrupts can be very usefull on encoders.
  4. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    Hello everyone and especially the creator of Thread for his work and because I have been able to see the support he has given to the community and that is always appreciated. I tell you my problem. I have spent many hours searching the internet. And in the end almost all lead to the same places. But I still can't find information for my problem. I am new to Arduino and that doesn't help me. Then I ask for understanding. And I am always open to learn. My project is simple, I think. I wish I could have a keyboard that I have. 16 momentary buttons 8 ON / ON switch, fixed position. So they make a total of 32 buttons. I don't want encoder, I don't want rotary, just buttons and switch. Can anyone help me with wiring and code information? Or what steps do I have to take. Greetings to all and once again congratulations to the creator of the post.
  5. JonBakhol

    JonBakhol Active Member

    Joined:
    Mar 16, 2018
    Messages:
    107
    Location:
    Netherlands harderwijk
    Balance:
    462Coins
    Ratings:
    +46 / 1 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    Hi @tutoveenee ,

    I understand your prob. We all begin somewhere.
    Take a look at my fritzing a few post above. (Here) On right side there are all buttons.
    A simple matrix, (for maybe an Arduino, or some other PCB's who can handle this.) it's easy to program.
    If I understand clearly, you only want buttons.
    Up-Down-Left-Right-Rotating-Always on-Always off. In the end they are all buttons. (I think, kill me if not)
    You can upload my sketch to a Arduino Pro Micro, and only connect the buttons to the right Number on the pro micro.

    My help in the beginning was Amstudio on YouTube. Watch the buttonbox stuff. It will take you by the hand to get your project done.

    Good Luck.

    Ride on
    Bakhol
    • Like Like x 1
  6. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    I have no other words than thanks. With your simple answer you have done more than many forums, in which I have asked. Many places where I have asked. And in the blink of an eye I already have an answer. Simply thanks. I also started with AMstudios, I have followed that project and now I use that base. I have working the 16 momentary buttons. But my problem is that I don't know how to wire the switches. Since I want each of the two positions of the switch to correspond to a button. And so have the 8 switch x2 the 16 buttons that would be missing. More components appear in your sketch that I don't need. And really, I appreciate your help.
  7. JonBakhol

    JonBakhol Active Member

    Joined:
    Mar 16, 2018
    Messages:
    107
    Location:
    Netherlands harderwijk
    Balance:
    462Coins
    Ratings:
    +46 / 1 / -0
    My Motion Simulator:
    2DOF, DC motor, JRK
    @tutoveenee
    Hi Again.
    I think you already answered your own question.
    Like you mentioned the switch is like 2 buttons in 1.
    So treat the switch as there are 2 buttons.
    You have to make an matrix of 4x8 (= 32 buttons).
    And most likely the switch has 3 connection on the underside.
    Button 1 (of the switch) is one outside plus middle. Button 2 of the switch is the other outside and the middle.

    I find it convenient to draw the matrix and number the buttons. It will make the soldering also a lot easier, because in the end it is like a bowl of spaghetti, and then the drawing will give you some thing to hold on to.

    Please feel free to ask if you are stuck

    Succes

    Ride on
    Bakhol
    • Like Like x 2
    Last edited: May 2, 2020
  8. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    As always, thank you for the help you are offering. Well, I am already understanding the steps to follow. But little by little. I ask that you remain just as understandable.

    1. I just followed in your footsteps and have created a sketch. I would like to know if it is correct. Always assuming that the numbers of each pin correspond to this arduino Pro Micro ATmega32U4 5V 16MHz.
    Since my model does not appear in the sketch. There are pins that correspond to other numbers. Changing the number in the sketch, solved, correct me if I'm wrong.

    https://imgur.com/VPfrOa7

    2. I have seen this sketch and correct me if I am wrong again. But the behavior of the 4 encoders is the same as the switch in this case ?. I see it has 3 pins as the switch. But it is so. And this sketch is what I need. I would still be missing inputs on the board since I have 8 switches.

    Greetings and thanks for your help, as always.
  9. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
  10. noorbeast

    noorbeast VR Tassie Devil Staff Member Moderator Race Director

    Joined:
    Jul 13, 2014
    Messages:
    20,460
    Occupation:
    Innovative tech specialist for NGOs
    Location:
    St Helens, Tasmania, Australia
    Balance:
    144,596Coins
    Ratings:
    +10,741 / 52 / -2
    My Motion Simulator:
    3DOF, DC motor, JRK
    • Like Like x 1
  11. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino

    Sorry, but when I wanted to edit the message and translate it into English. I won't let myself do it. Greetings and sorry.
    • Like Like x 1
  12. JonBakhol

    JonBakhol Active Member

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

    Please take a look at attached drawing (matrix 4x8).
    As you can see the switches are made of two buttons.
    matrix 4x8.jpg
    • Like Like x 1
    Last edited: May 4, 2020
  13. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    Now if I have run out of words. I understand what you want to explain to me. The switch is clear to me that there are 2 buttons. But in your sketch, only buttons and wiring appear, I understand but you can change the groups of buttons, for switches. And so you can see the wiring. Why the center pin of each switch how is the wiring done? It does not appear in that sketch. Or is everything central land? is GND?

    Excuse my ignorance. And surely ask silly questions. But as we say in my country. Shoemaker with his shoes.
  14. JonBakhol

    JonBakhol Active Member

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

    Sorry I adjust my drawing to your switches. Please take a look again
    matrix 4x8.jpg
    • Like Like x 1
    Last edited: May 4, 2020
  15. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    Well, as clear as water. Thank you very much and many times more. Already resolved the doubt. Now I want to think that with this sketch. And of all the arduino codes, that I can see in the Post, some in particular, for this sketch to work?
  16. JonBakhol

    JonBakhol Active Member

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

    I would try this code:
    There is a part for encoders. But just ignore that. When not used, it doesn't matter that it is in there.
    Only the red part is for your buttons

    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},

    and

    byte rowPins[NUMROWS] = {21,20,19,18,15,14,16,10};
    byte colPins[NUMCOLS] = {9,8,7,6};


    Solder the wires and upload the whole sketch to your arduino pro micro.

    I think it will work

    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);
        };
      }
    }
    Ride on
    Bakhol
    • Like Like x 1
  17. tutoveenee

    tutoveenee New Member

    Joined:
    May 1, 2020
    Messages:
    9
    Balance:
    51Coins
    Ratings:
    +1 / 0 / -0
    My Motion Simulator:
    Arduino
    Ok now yes, well I hope that everything goes well the first time and as soon as I have finished I upload photos. Greetings and without your help, I would not have been able to with this project. Thank you.
  18. JonBakhol

    JonBakhol Active Member

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

    I tried it at this point with an pro micro and an paperclip. I got all 32 buttons working

    Ride on
    Bakhol
    • Like Like x 1
  19. clabit

    clabit New Member

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

    i have to create botton box with 16 buttons and 4 axis
    I don't know anything about programming
    can you tell me if the code i created is correct
    thank you very much

    Code:
    #include <Keypad.h>
    #include <Joystick.h>
    
    #define NUMBUTTONS 16
    #define NUMROWS 4
    #define NUMCOLS 4
    
    int X1 = A0;
    int Y1 = A1;
    int rudder = A2;
    int throttle = A3;
    
    byte buttons[NUMROWS][NUMCOLS] = {
      {0,1,2,3},
      {4,5,6,7},
      
    };
    
    byte rowPins[NUMROWS] = {6,7,8,9}; 
    byte colPins[NUMCOLS] = {2,3,4,5}; 
    
    Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS); 
    
    Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 12, 0, 
      true, true, false, false, false, false, false, false, false, false, false);
      
    void setup() {
      Joystick.begin();
        Joystick.setXAxisRange(-512, 512);
        Joystick.setYAxisRange(-512, 512);
        Joystick.setRudderRange(0, 1023);
        Joystick.setThrottleRange(0, 1023);
      }
    void JButtonStates() {
        Joystick.setXAxis(analogRead(X1) - 512);
        Joystick.setYAxis(analogRead(Y1) - 512);
        Joystick.setRudder(analogRead(rudder));
        Joystick.setThrottle(analogRead(throttle));
      }
    void loop() {
      JButtonStates();
      delay(50);
    }
    
  20. JonBakhol

    JonBakhol Active Member

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

    What is the purpose of the 4 axis? In which way they are connected? Maybe with potentiometers?
    I'm also not the programmer you are looking for, but maybe I can help.
    Are there 2 sketches you took and put them together?
    If so. Can you link the 2 separate programms?

    I'll try (if I have the time) to check it.

    Let me know.

    Ride on
    Bakhol
    Last edited: May 13, 2020