Arduino button box for iRacing - control only responding intermittently

Hi All

Pretty new to Arduino, and building myself a button box for iRacing.

I’m using a rotary encoder to (try to) control ABS, TCS and brake bias. My challenge is that it only intermittently increments/decrements the selected control, rather than with each detente.

I have tested the setup on other software (FSX) and each detente sends the command and the sim responds, but in iRacing it only seems to respond randomly

Basically, it has 3 switch modes (toggled with the pushbutton switch of the encoder), and depending on the switch mode when the interrupt procedure is called, it sends a different set of key presses.

the interrupt procedure code is below:

Procedure is called with:

attachInterrupt(0, adjustRE, CHANGE);

And the actual procedure is:

void adjustRE(){
     if(digitalRead(3) == digitalRead(2)){
        if(switchVal==1){
          Keyboard.press(KEY_LEFT_CTRL);
          Keyboard.press(KEY_LEFT_SHIFT);
          Keyboard.press('u');
          delay(100);
          Keyboard.release('u');
          Keyboard.releaseAll();
        }
        
        if (switchVal==2){
          Serial.println("2 +");
          Keyboard.press(KEY_LEFT_ALT);
          Keyboard.press(KEY_LEFT_SHIFT);
          Keyboard.press('u');
          delay(100);
          Keyboard.release('u');
          Keyboard.releaseAll();
       }
       if (switchVal==3){
          Keyboard.press(KEY_LEFT_ALT);
          Keyboard.press(KEY_LEFT_CTRL);
          Keyboard.press('u');
          delay(100);
          Keyboard.release('u');
          Keyboard.releaseAll();
       }
    }
        
   
   else {
        if(switchVal==1){
          Keyboard.press(KEY_LEFT_CTRL);
          Keyboard.press(KEY_LEFT_SHIFT);
          Keyboard.press('b');
          delay(100);
          Keyboard.release('b');
          Keyboard.releaseAll();
        }
        
        if (switchVal==2){
          Keyboard.press(KEY_LEFT_ALT);
          Keyboard.press(KEY_LEFT_SHIFT);
          Keyboard.press('b');
          delay(100);
          Keyboard.release('b');
          Keyboard.releaseAll();
       }
       if (switchVal==3){
          Keyboard.press(KEY_LEFT_ALT);
          Keyboard.press(KEY_LEFT_CTRL);
          Keyboard.press('b');
          delay(100);
          Keyboard.release('b');
          Keyboard.releaseAll();
       }
}
}

Any thoughts/help would be greatly appreciated!

I’ve also attached the full sketch…

THanks

Button_Box_rev_b.ino (7.96 KB)

Does the Keyboard library use interrupts ? delay() does, and as interrupts are disabled when in an ISR this could cause a problem.