Button Box and rotary encoders

I just had a couple questions about toggle on and off swithch on I am using a pro micro to make my button box what would I code if I wanted the switch to press a keystroke when I flip it on and press a keystroke when I flip it off I was also wondering if I am able to add more then 2 keystrokes for a rotary encoder.

You can make a toggle switch executes some code each time it changes state (OFF > ON or ON > OFF). That code could be different for the 2 state changes. So, yes, if you have configured your PRO Micro to deliver keystrokes to the connected PC, you can do it.

Similarly, with a rotary encoder it would be possible to deliver keystrokes. However, the user would need some sort of indication of which keystroke had been selected in order to press the button on the encoder to send the selected keystroke to the PC.

Do you have any example of what the code would look like

This is just to get you started with the toggle switch. It simply prints out messages. You'll have to find code sample to send key presses to the connected PC. Then you'll have to try to integrate a rotary encoder:

uint8_t togglePin = 2 ;  // your choice of pin. Wire switch between this pin and ground
bool toggleState ;  // true if switch ON , false otherwise.
uint32_t debounceLockoutAtMs ;

void setup() {
   Serial.begin( 115200 ) ;
   while (!Serial) ;  // for Micro / Leonardo
   pinMode( togglePin, INPUT_PULLUP ) ;
   toggleState = digitalRead( togglePin ) ;
   debounceLockoutAtMs = millis() ;

void loop() {
  bool currentToggle =  digitalRead( togglePin ) == LOW ? true : false  ; // inverted logic - Switch between pin and ground.
  if ( currentToggle != toggleState && millis() - debounceLockoutAtMs > 50 ) {  // 50ms debounce
    debounceLockoutAtMs = millis() ;
    Serial.println("Toggle state change detected" ) ;
    if ( currentToggle == true ) Serial.println(" false to true transition" ) ;
    else Serial.println(" true to false transition" ) ;
  toggleState = currentToggle ;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.