pro micro input lag

hi,

I'm currently working on a project for ETS2. I'm trying to connect my indicator switches to my pro micro board and it was a success. I managed to code it so everything i used on the switches send out an key press. But when i went in-game and used my switches, every time i pressed it it happened like a second after i pressed it. But in the Arduino program it happened instantly

This is the code i'm using.

#include <Keyboard.h>

const byte switchPin1 = 2;
const byte switchPin2 = 3;
const byte switchPin3 = 4;
const byte switchPin4 = 5;
const byte switchPin5 = 6;
const byte switchPin6 = 7;
const byte switchPin7 = 8;
const byte switchPin8 = 9;
byte oldSwitchState1 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState2 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState3 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState4 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState5 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState6 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState7 = HIGH;  // assume switch open because of pull-up resistor
byte oldSwitchState8 = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds

void setup ()
  {
  Serial.begin (9600);
  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);
  }  // end of setup

void loop ()
  {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState1 = digitalRead (switchPin1);
  
  // has it changed since last time?
  if (switchState1 != oldSwitchState1)
    {
    oldSwitchState1 =  switchState1;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState1 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('q');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       Keyboard.press('q');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change
  {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState2 = digitalRead (switchPin2);
  
  // has it changed since last time?
  if (switchState2 != oldSwitchState2)
    {
    oldSwitchState2 =  switchState2;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState2 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('w');
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change
  }
 {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState3 = digitalRead (switchPin3);
  
  // has it changed since last time?
  if (switchState3 != oldSwitchState3)
    {
    oldSwitchState3 =  switchState3;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState3 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('e');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       Keyboard.press('e');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change    
 }     
 {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState4 = digitalRead (switchPin4);
  
  // has it changed since last time?
  if (switchState4 != oldSwitchState4)
    {
    oldSwitchState4 =  switchState4;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState4 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('r');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       Keyboard.press('r');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change    
 }     
 {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState5 = digitalRead (switchPin5);
  
  // has it changed since last time?
  if (switchState5 != oldSwitchState5)
    {
    oldSwitchState5 =  switchState5;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState5 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('t');       
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change    
 }     
 {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState6 = digitalRead (switchPin6);
  
  // has it changed since last time?
  if (switchState6 != oldSwitchState6)
    {
    oldSwitchState6 =  switchState6;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState6 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('y');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is LOW
    }  // end of state change    
 }     
 {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState7 = digitalRead (switchPin7);
  
  // has it changed since last time?
  if (switchState7 != oldSwitchState7)
    {
    oldSwitchState7 =  switchState7;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState7 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('u');
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change    
 }     
 {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState8 = digitalRead (switchPin8);
  
  // has it changed since last time?
  if (switchState8 != oldSwitchState8)
    {
    oldSwitchState8 =  switchState8;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState8 == LOW)
       {
       Serial.println ("Switch closed.");
       Keyboard.press('i');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is LOW
    }  // end of state change    
}     
  // other code here ...
   
}

DAF_Switch.ino (6.39 KB)

All those delays() means the processor is held up for a long time doing nothing.

Look at the 'blink without delay' example in the IDE

Allan

The delays don't sum up, a delay only occurs when a button state changes.

But the debouncing is poor for other reasons, have a look at the StateChangeDetection example.

Also consider to use a subroutine for input checking. This will simplify general updates of the code.

DrDiettrich:
The delays don't sum up, a delay only occurs when a button state changes.

But the debouncing is poor for other reasons, have a look at the StateChangeDetection example.

Also consider to use a subroutine for input checking. This will simplify general updates of the code.

what do you mean with subroutine for input checking?

See the essentially same code for every button. You can put that code once into a function, and pass the pin number as an argument. The result can go into an array, or can be returned as function result.

There is no need Keyboard.begin multiple tines in loop(); call it once in setup().

This coding is just to hard for me to understand. can't i simplify it like when i flip the switch to blink left it presses a key and when i flip it back it presses the same key again? and when i press the horn key it presses a key continuously?

When you find yourself writing code like this

const byte switchPin1 = 2;
const byte switchPin2 = 3;
const byte switchPin3 = 4;
const byte switchPin4 = 5;
const byte switchPin5 = 6;
const byte switchPin6 = 7;
const byte switchPin7 = 8;
const byte switchPin8 = 9;

it is time to learn how to use arrays. The code will be shorter and with much less scope for typos.

I am not going to read any more of your code until you present it properly using the code button </> as I have done

...R

pinMode (2, INPUT_PULLUP);
  pinMode (3, INPUT_PULLUP);
  pinMode (4, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP);

You already gave the pins nice names; why not use them? (But see the comments above about arrays)

Robin2:
When you find yourself writing code like this

const byte switchPin1 = 2;

const byte switchPin2 = 3;
const byte switchPin3 = 4;
const byte switchPin4 = 5;
const byte switchPin5 = 6;
const byte switchPin6 = 7;
const byte switchPin7 = 8;
const byte switchPin8 = 9;



it is time to learn how to use arrays. The code will be shorter and with much less scope for typos.

I am not going to read any more of your code until you present it properly using the code button </> as I have done

...R

sorry, i have fixed my post using the code button.
about the arrays, is it possible using it with const byte like for example "const byte switchPinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};

Yes - a good approach.

You can then index through them all with eg a for loop.

If you then make a second identical array of bools containing 'state last time round ' you needn't use delays at all.

Allan

i fixed the problem by removing every Serial.println in the code and adjusted a few debounce delays