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)