I am trying to program a 3 position rotary switch to make a keyboard command for a simulator I am building.
What I am trying to achieve is the following.
The switch has 3 positions forward neutral and reverse clockwise. Top is forward, 3 oclock is neutral and bottom is reverse.
When the switch is moved clockwise from forward to neutral or from neutral to reverse, the code will output the "s" keyboard command.
When the switch is moved anticlockwise from reverse to neutral or from neutral to forward, the code will output the "w" keyboard command.
Ultimately I need the code to read the state of the switch, if it has changes has it changed up or down and then depending on that change output a keyboard command.
If the switch has one common terminal, ground that one, and treat the other three terminals as separate switches, connected to separate digital inputs. Be sure to set pinMode(pinname, INPUT_PULLUP); to enable the internal pullup resistors.
The Arduino IDE has built in examples for reading switches, including a state change example. Files>Examples> ...
The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 functions will continuously return true if the switch is at that position. The last 4 functions return true (once only) if the switch has just transitioned to that position.
Common switches will momentarily have a position BETWEEN your positions that happen as you are moving the switch. Do not be surprised, but continue to read and test for valid positions as one will eventually appear when you find a detente position.
If the switch status is read during the break-before-make (BBM) as the rotary switch transfers position to position, it will not create any issue (fully tested). By default, the Toggle library uses a symmetrical and robust debounce algorithm that prevents up to two bad reads (both on rising and falling) from affecting the debounced output.