Jump to content

Arduino Pro Micro Simracing wheel

Go to solution Solved by Hackentosher,
7 hours ago, Rothfox said:

I found this code and got lucky. now all the Analogue pins also work for buttons presses. the only thing i am missing now, is to get the CTS-288 Encoders to work propperly. The issue i am having is that both left and right get activated when scrolling it eather way. they activate at an offsett. and stay on for one turn. So, I guess i need some code to tell the arduino to not count left as pressed when scrolling right etc, and also something to make the scrolls count as pulses and not as pressed.
I found this https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/ wich seems helpfull. altho idk how to code that at all. 

Here is the sketch im using. as to Liberys i think i was using ArduinoJoystickLibery and Encoder1.41. altho i myst be honest about not knowing how liberys work at all..

I don’t think you need a library for using a rotary encoder, just two digital pins, power, and ground. Generally, rotary encoders have two pins pulled high, and then turning the encoder pulls the pins down in a certain order. The order changes depending on which way you’re turning so you need some if statements to decide what to do based on what happens. This video explains it better than I did. 

I highly recommend looking at the Arduino reference page, it documents and explains with examples every function and command within arduino and really helps get you off the ground. I also recommend reading up on some basic c++ because that also really helps explain what is going on in your code and helps with the basic syntax. I struggled with that as well when I first started, but once I took a c++ class, it all made sense. 

Heyo! I am in the midst of a Simracing wheel-rim DIY, and my Arduino/Coding skills are lackluster. 

I'm using a ATMEGA32U4 for a Porsche 911 gt3 cup button box + shifters. 10 push buttons, 2 paddle shifters and 2 encoders. = 16 outputs total as the encoders have 2 each.

also want to solder a DIN connection to the board, and havent looked up how to do that yet eather.

where i am stuck at is that i found a sketch that works from pin 2-10, 14-16. so im missing 1,11,12,13. the visible buttons on the board are 2-10,14-16, A0-A3. so im guessing i need some code to set up A0-A3 as normal pins. also i need code for the Encoders to be correctly red. atm they dont work propperly, (dont work as an individual puls every time i switch position). 

here are some pictures to feast your eyes on. 
Any help would be much apprichated! 


Brg Roth
 

113034623_1527178604150165_7386817850610116217_n.jpg

109271816_432728311116911_4334460508227608140_n.png

110147526_319510139195637_4055577373223790663_n.jpg

 CPU: i7 8700k, 4.9ghz 1.29v llc1  Motherboard: Asus Z370-i G  Ram: Trident-Z 3200mhz->3866 cl17,17,37
GPU: Strix Oc 1080ti 2075mhz  Case: aNcase M1
Storage: Samsung 970 Evo 500gb, x2 Raid 0 PSU: Corsair SF600 SFX
Display: Acer Predator x34 100hz 1440p  
Cooling: Celsius C24  Operating System: W10   

 
Link to comment
Share on other sites

Link to post
Share on other sites

Sounds like you have a programming issue, so post your code not your hardware. You didn't specify which Arduino library you're using, so if you're not already, read up on the Arduino Keyboard library and the NicoHood HID library.

ASU

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, Hackentosher said:

Sounds like you have a programming issue, so post your code not your hardware. You didn't specify which Arduino library you're using, so if you're not already, read up on the Arduino Keyboard library and the NicoHood HID library.

I found this code and got lucky. now all the Analogue pins also work for buttons presses. the only thing i am missing now, is to get the CTS-288 Encoders to work propperly. The issue i am having is that both left and right get activated when scrolling it eather way. they activate at an offsett. and stay on for one turn. So, I guess i need some code to tell the arduino to not count left as pressed when scrolling right etc, and also something to make the scrolls count as pulses and not as pressed.
I found this https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/ wich seems helpfull. altho idk how to code that at all. 

Here is the sketch im using. as to Liberys i think i was using ArduinoJoystickLibery and Encoder1.41. altho i myst be honest about not knowing how liberys work at all..

 

// modified sketch
// by Matthew Heironimus
// 2015-11-20

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  // Initialize Button Pins
  // for digitalRead pins A0-A5 = 18-23
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// defining the total [#] of buttons and their pins
const int ButtonToPinMap[19] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,18,19,20,21,22};

int lastButtonState[19] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void loop() {

  for (int index = 0; index < 19; index++)
  {
    int currentButtonState = !digitalRead(ButtonToPinMap[index]);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

 CPU: i7 8700k, 4.9ghz 1.29v llc1  Motherboard: Asus Z370-i G  Ram: Trident-Z 3200mhz->3866 cl17,17,37
GPU: Strix Oc 1080ti 2075mhz  Case: aNcase M1
Storage: Samsung 970 Evo 500gb, x2 Raid 0 PSU: Corsair SF600 SFX
Display: Acer Predator x34 100hz 1440p  
Cooling: Celsius C24  Operating System: W10   

 
Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Rothfox said:

I found this code and got lucky. now all the Analogue pins also work for buttons presses. the only thing i am missing now, is to get the CTS-288 Encoders to work propperly. The issue i am having is that both left and right get activated when scrolling it eather way. they activate at an offsett. and stay on for one turn. So, I guess i need some code to tell the arduino to not count left as pressed when scrolling right etc, and also something to make the scrolls count as pulses and not as pressed.
I found this https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/ wich seems helpfull. altho idk how to code that at all. 

Here is the sketch im using. as to Liberys i think i was using ArduinoJoystickLibery and Encoder1.41. altho i myst be honest about not knowing how liberys work at all..

I don’t think you need a library for using a rotary encoder, just two digital pins, power, and ground. Generally, rotary encoders have two pins pulled high, and then turning the encoder pulls the pins down in a certain order. The order changes depending on which way you’re turning so you need some if statements to decide what to do based on what happens. This video explains it better than I did. 

I highly recommend looking at the Arduino reference page, it documents and explains with examples every function and command within arduino and really helps get you off the ground. I also recommend reading up on some basic c++ because that also really helps explain what is going on in your code and helps with the basic syntax. I struggled with that as well when I first started, but once I took a c++ class, it all made sense. 

ASU

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Rothfox said:


}

// defining the total [#] of buttons and their pins
const int ButtonToPinMap[19] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,18,19,20,21,22};

int lastButtonState[19] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void loop() {

  for (int index = 0; index < 19; index++)
  {
    int currentButtonState = !digitalRead(ButtonToPinMap[index]);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

I was just looking at your code and noticed some things. First, why are you NOTing the digital read with 

 int currentButtonState = !digitalRead(ButtonToPinMap[index]);

?

Also there are some other problems with that line, it's in a for loop, which means it will be executed so long as your variable index is less than 19. But you declare the variable each time which can screw with your program. You don't need "int" every time you use a variable, only when you're creating one. Next, I think it's important for me to point out the difference between "=" and "==" in C++. "='' is known as the assignment operator, it sets two things to be equal to each other. "==" is an equality logical operator, it returns true when two variables are equal to each other. This is a common trap newbies fall into, especially because an assignment operator may "work" at first glance. I mentioned that equality returns true when two values are the same, but assignment also returns true, but for a different reason. Assignment returns true when an assignment has been executed successfully, so that can screw with your logic. Make sure to pay attention to that.

ASU

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Hackentosher said:
1 hour ago, Hackentosher said:

I was just looking at your code and noticed some things. First, why are you NOTing the digital read with 



 int currentButtonState = !digitalRead(ButtonToPinMap[index]);

?

Also there are some other problems with that line, it's in a for loop, which means it will be executed so long as your variable index is less than 19. But you declare the variable each time which can screw with your program. You don't need "int" every time you use a variable, only when you're creating one. Next, I think it's important for me to point out the difference between "=" and "==" in C++. "='' is known as the assignment operator, it sets two things to be equal to each other. "==" is an equality logical operator, it returns true when two variables are equal to each other. This is a common trap newbies fall into, especially because an assignment operator may "work" at first glance. I mentioned that equality returns true when two values are the same, but assignment also returns true, but for a different reason. Assignment returns true when an assignment has been executed successfully, so that can screw with your logic. Make sure to pay attention to that.

Honestly I havent changed that code from its original maker. maby noob in coding and arduino was an understatment. as i dont have any fundementals at all. I have previously red some code here and there, and the codes somewhat felt like they made sense to me  ath the time. 
when i was going to make this one i was hoping it would be more straigt forward, not that it has to be! i thought i would be able to find the code i needed more easly. wich proved to not be the case. 

I have a lot to read up on if i ever want to be able to make this sketch on my own. so atm another option would just be to get a
Leo Bodnar Micro-BBI, wich will be alot easyr for my usecase. alltho triple the price of a Arduino Pro Micro.

Anyway i apprichiate your answers, alltho what they refer to is greek to me for the moment beeing. 
i will defenitly take a further dive in to some coding, c++ as you mentioned and Arduino. alot of fun projects to yet be had with all this. 

brg Roth

 

 CPU: i7 8700k, 4.9ghz 1.29v llc1  Motherboard: Asus Z370-i G  Ram: Trident-Z 3200mhz->3866 cl17,17,37
GPU: Strix Oc 1080ti 2075mhz  Case: aNcase M1
Storage: Samsung 970 Evo 500gb, x2 Raid 0 PSU: Corsair SF600 SFX
Display: Acer Predator x34 100hz 1440p  
Cooling: Celsius C24  Operating System: W10   

 
Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Rothfox said:

Honestly I havent changed that code from its original maker. maby noob in coding and arduino was an understatment. as i dont have any fundementals at all. I have previously red some code here and there, and the codes somewhat felt like they made sense to me  ath the time. 
when i was going to make this one i was hoping it would be more straigt forward, not that it has to be! i thought i would be able to find the code i needed more easly. wich proved to not be the case. 

I have a lot to read up on if i ever want to be able to make this sketch on my own. so atm another option would just be to get a
Leo Bodnar Micro-BBI, wich will be alot easyr for my usecase. alltho triple the price of a Arduino Pro Micro.

Anyway i apprichiate your answers, alltho what they refer to is greek to me for the moment beeing. 
i will defenitly take a further dive in to some coding, c++ as you mentioned and Arduino. alot of fun projects to yet be had with all this. 

brg Roth

I think you could do this on your own pretty easily, it's just some basic digital logic. Here are some introductory C++ resources that were really useful when I was getting started.

http://www.cplusplus.com/

https://www.geeksforgeeks.org/c-plus-plus/?ref=leftbar

https://www.arduino.cc/reference/en/ (this one is still on my bookmarks bar)

sub point, https://www.arduino.cc/reference/en/libraries/ 

other sub point https://www.arduino.cc/reference/en/language/functions/usb/keyboard/

 

I think that program is pretty close, and I like their idea of storing all of the pin states in an array (except for the encoder, that doesn't make much sense) as it will consolidate the logic for reading the pins. Honestly, this is probably a really good beginner Arduino/programming project, but maybe you want to dial back the complexity at first. I would separate the encoder and button reading into two different programs for now so you can get the hang of how they work individually, and then combine them later.

ASU

Link to comment
Share on other sites

Link to post
Share on other sites

It's been awhile but I do remember when I was playing with an Arduino Nano and was looking at a rotary encoder.

 

There were some issues where there was not enough interrupts or the code was too intensive when using polling when people already had a lot of code or were already using interrupts for other things, especially if you need more than one.

 

Eg  https://forum.arduino.cc/index.php?topic=412159.0

 

One option is there are decoders like from ELM or LSI CSI that can offload that task or make it easier to interface to a uc.

 

ELM404

 

LSI CSI Decoders

 

Someone on Hackaday did something similar with a PIC dedicated as a decoder to I2C and that as another option that is used, with a microcontroller dedicated to that task.

 

The ELM IC is actually a preprogrammed PIC I believe.

 

Super foggy so may be slightly wrong and some of the newer faster micros may not have the same issues. But hope it helps.

Link to comment
Share on other sites

Link to post
Share on other sites

It's been awhile but I do remember when I was playing with an Arduino Nano and was looking at a rotary encoder.

 

There were some issues where there was not enough interrupts or the code was too intensive when using polling when people already had a lot of code or were already using interrupts for other things, especially if you need more than one.

 

Eg  https://forum.arduino.cc/index.php?topic=412159.0

 

One option is there are decoders like from ELM or LSI CSI that can offload that task or make it easier to interface to a uc.

 

ELM404

 

LSI CSI Decoders

 

Someone on Hackaday did something similar with a PIC dedicated as a decoder to I2C and that as another option that is used, with a microcontroller dedicated to that task. 

 

https://hackaday.com/2018/04/15/rotary-encoders-become-i2c-devices/

 

The ELM IC is actually a preprogrammed PIC I believe.

 

Super foggy so may be slightly wrong and some of the newer faster micros may not have the same issues. But hope it helps.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×