[Tuto] Use a Toggle...
 
Notifications
Clear all

[Tuto] Use a Toggle switch as Temporary button

14 Posts
5 Users
1 Reactions
4,404 Views
 sgeg
(@sgeg)
Trusted Member
Joined: 3 years ago
Posts: 52
Topic starter  

hello,

i had a problem with 1 toggle switch that i want to use as ignition that stayed ON all the time...in game.

here is how to manage that switch will send a pulse when on and send another one when OFF.

needed:

1 pro micro or leonardo

1 toggle switch connect to a pin (10 here) and to GND

if you already used your arduino as button box check in "windows game controller" which of the 32 buttons is available and remember it.

PLEASE SAVE YOUR SKETCH IN PROGRESS I DONT wANT TO BE INVOLE IF YOU LOOSE YOUR wORK
test the code after a save as in case of issue

1-open Simhub Arduino configuration and reload last setting if you want to keep all previous stuff
2-be sure GAMEPAD is ON
3-select File>Open in Arduino IDE
4-in DisplayClient2 tab look for "Joystick.begin"  with ctrl+f and modify "false" by "true" and save the sketch and close IDE.
5-in c:program Files x86>Simhub>_addon>arduino>DisplayclientV2 open with wordpad "SHCustomProtocol.h"
6-copy this code

void setup and loop are already in place careful to dont double them keep all other void in place (i added my file as attached)

#include  


// Constant that maps the physical pin to the joystick button.
const int toggleSwitchPin = 10;


void setup(){
pinMode(toggleSwitchPin, INPUT_PULLUP);
Joystick.begin();
}

void loop(){

static boolean oldSwitchState = digitalRead(toggleSwitchPin);
boolean newSwitchState = digitalRead(toggleSwitchPin);
if (newSwitchState != oldSwitchState) {
oldSwitchState = newSwitchState;// Switch has changed state. Remember the new state
Joystick.setButton(31, HIGH);// the number is the available button -1, here it's button 32
delay(100);
Joystick.setButton(31, LOW);// the number is the available button -1, here it's button 32
}}

at lines Joystick.setButton put the number of the available button-1: if you want use button 5, put 4...

7-save and open Simhub Arduino configuration and the arduino IDE and Compile to check if there is any error in the code

8-Enjoy


   
Quote
(@ch_andersen)
New Member
Joined: 3 years ago
Posts: 4
 

Hello, 

Do you know if this still work? It work fine for me first time I tried but now I can't get it to work 🙁

When I modify the joystick.begin to "true" and open it again. It has changed to "false"

Is that right? 🙂


   
ReplyQuote
 sgeg
(@sgeg)
Trusted Member
Joined: 3 years ago
Posts: 52
Topic starter  

hello @ch_andersen

did you save the sketch?

you can add  joystick.begin(); in the SHcustomProtocol.h in the void setup too...

keep me in touch if that work...

void setup(){
pinMode(toggleSwitchPin, INPUT_PULLUP);
Joystick.begin();
}

   
ReplyQuote
(@ch_andersen)
New Member
Joined: 3 years ago
Posts: 4
 

@sgeg Thank you!!

 

It worked 🙂


   
sgeg reacted
ReplyQuote
 sgeg
(@sgeg)
Trusted Member
Joined: 3 years ago
Posts: 52
Topic starter  

thanks @ch_andersen
i updated the tutorial...


   
ReplyQuote
(@dzonkins)
New Member
Joined: 2 years ago
Posts: 3
 

@sgeg 

Heya, hope digging out old topics won't make anyone mad, but I can't find any answear to the problem your method should solve. So far I tried to reconfigure the file you uploaded for my use, and also did a "fresh" one, along with trying different setting for my additional button in SimHub sketch setup. Sadly, I'm getting the same result - the switch is always on or off if reversed in sketch setup. Did something change in last few months that would make this method not work now, or am I missing something? 😛

Just to add a bit more info; the switch is connected so it lights up when it's in "ON" position, when I upload sketch with modified SHcustomProtocol.h the light is also on for the whole time, but it gets much brighter in ON position. In config it's set to VCC to pin, the pin to GND turns it off basically.

 

This post was modified 2 years ago by dzonkins

   
ReplyQuote
 sgeg
(@sgeg)
Trusted Member
Joined: 3 years ago
Posts: 52
Topic starter  

hello @dzonkins ,

no problem if i can help...

you dont have to configure the button in SimHub , that can be the issue because you have 2 fonctions on the same pin.

be sure the switch is on the Pin+GND, the sketch is not configure for VCC to pin

check also in "windows game controller" what is your switch number when you use it and be sure to use this number minus 1 for the jostick.setbutton

you can compile the DisplayclientV2 sketch in Arduino IDE to check if there is no errors

keep me in touch


   
ReplyQuote
(@dzonkins)
New Member
Joined: 2 years ago
Posts: 3
 

@sgeg 

Thanks for reply! 🙂

I sent out a sketch with those settings (pic below) to my box now, but as I said earlier with using pin+GND in my case - it disables the switch, doesn't give any signs of life. I set buttons to 0, as my switch (when it sends any signal) always goes as button number 1 - setting different numbers in SHCustomProtocol doesn't seem to have any effect. Is rewiring the switch in order to make pin+gnd work?

 

 

Found a little workaround - using SVMapper i mapped a keyboard button to press and release of the switch, this way I get the button action I wanted, but well, it's a workaround 😀

This post was modified 2 years ago by dzonkins

   
ReplyQuote
 sgeg
(@sgeg)
Trusted Member
Joined: 3 years ago
Posts: 52
Topic starter  

@dzonkins

you must disable the button in simhub, no needed it, that why you have issue..

and yes you must solder the switch to the GND and the Pin


   
ReplyQuote
(@dzonkins)
New Member
Joined: 2 years ago
Posts: 3
 

@sgeg Did that, sadly didn't change a thing - lamp on the switch is faintly glowing, brightens up in ON position, but doesn't output anything. I tried rewiring the switch and uploading sketch with pin to gnd with default settings just to see if it would work, but also no success there - no light, no output. Maybe the switch im using is kinda wonky for this to work, I'll stick with SVMapper fix for now.


   
ReplyQuote
(@blue111)
New Member
Joined: 2 years ago
Posts: 2
 

Posted by: @dzonkins

@sgeg Did that, sadly didn't change a thing - lamp on the switch is faintly glowing, brightens up in ON position, but doesn't output anything. I tried rewiring the switch and uploading sketch with pin to gnd with default settings just to see if it would work, but also no success there - no light, no output. Maybe the switch im using is kinda wonky for this to work, I'll stick with SVMapper fix for now.

how is the file inserted is the file exchanged completely or just appended

 


   
ReplyQuote
(@blue111)
New Member
Joined: 2 years ago
Posts: 2
 

the old file will be replaced "SHCustomProtocol.h"


   
ReplyQuote
 sgeg
(@sgeg)
Trusted Member
Joined: 3 years ago
Posts: 52
Topic starter  

@blue111 

hi, if the code is good for you you can use the attach one, but you can edit directly edit the original shcustomprotocol.h. Just keep the main structure of the file and edit void setup and loop straight 

enjoy


   
ReplyQuote
(@joluga123)
New Member
Joined: 1 year ago
Posts: 1
 

@sgeg 

Hello, I looked everywhere and I found this post about using a switch as a momentary button. It does work great, however I'm having an issue, when I program my arduino the toggle switch works as intended per the custom protocol, but my encoders start acting up. they do work but sporadically, I had to press another button for them to reset. This happens to the 4 encoders I have.

Could it be that the custom protocol is messing up with my encoders? Any help will be appreciated. Thx.

https://postimg.cc/JHQRgw89


   
ReplyQuote
Share: