Need help with Arduino code for Pro Micro as "HID"

Hi, it`s me again with the Truck Simulator Project:
https://hackaday.io/project/8448-real-dashboard-truck-simulator

I found a suitable code that works for my Truck simulator but it needs modification and I need your help.

This code was ment for a toggle button. It has two functions:

1# Press short and print a given letter (e.g. “g”)
2# Press and hold and print a different letter (e.g. “g”)

What I need is simple: “When the input changes state, send the letter”

Please note: It shouldn’t cause a loop / string of letters. Remember this is a “toggle switch” and not a push button. So with every INPUT change, just send one letter at the time and wait for another INPUT change to send the same letter again without causing a loop.

Thanks for helping!

Here is my file, please have a look at it and please help me to adjust this code so I can use it.

Thanks in advance!

https://mega.nz/#!XRcjGS6C

Switch 1 should input letter “a” when ON
Switch 1 should input letter “a” again when OFF
Switch 2 should input letter “b” when ON
Switch 2 should input letter “b” again when OFF
Etc.

Ok, I found it with help of the Arduino forum. This topic may close.

Code for Arduino IDE to use PIN 2,3,4,5,6,7,8,9 and 10 as an INPUT with a PULL-UP 10k ohm resistor:

const byte switchPinA = 2;
const byte switchPinB = 3;
const byte switchPinC = 4;
const byte switchPinD = 5;
const byte switchPinE = 6;
const byte switchPinF = 7;
const byte switchPinG = 8;
const byte switchPinH = 9;
const byte switchPinI = 10;
byte oldSwitchStateA = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateB = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateC = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateD = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateE = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateF = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateG = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateH = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateI = HIGH;  // assume switch OFF because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds

void setup ()
  {
  Serial.begin (115200);
  pinMode (switchPinA, INPUT_PULLUP);
  pinMode (switchPinB, INPUT_PULLUP);
  pinMode (switchPinC, INPUT_PULLUP);
  pinMode (switchPinD, INPUT_PULLUP);
  pinMode (switchPinE, INPUT_PULLUP);
  pinMode (switchPinF, INPUT_PULLUP);
  pinMode (switchPinG, INPUT_PULLUP);
  pinMode (switchPinH, INPUT_PULLUP);
  pinMode (switchPinI, INPUT_PULLUP);
  Keyboard.begin();
  }  // end of setup

void loop ()
  {
  // see if switch A is OFF or ON
  byte switchStateA = digitalRead (switchPinA);

// has it changed since last time?
if (switchStateA != oldSwitchStateA)
{
oldSwitchStateA =  switchStateA;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateA == LOW)
   {
   Serial.println ("Switch A is ON.");
   Keyboard.press(KEY_LEFT_SHIFT);
   Keyboard.print("a");
   Keyboard.release(KEY_LEFT_SHIFT);
   }  // end if switchStateA is LOW
else
   {
   Serial.println ("Switch A is OFF.");
   Keyboard.print("a");
   }  // end if switchStateA is HIGH
}  // end of state change
 
// see if switch B is OFF or ON
byte switchStateB = digitalRead (switchPinB);

// has it changed since last time?
if (switchStateB != oldSwitchStateB)
{
oldSwitchStateB =  switchStateB;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateB == LOW)
   {
   Serial.println ("Switch B is ON.");
   Keyboard.print("b");
   }  // end if switchStateA is LOW
else
   {
   Serial.println ("Switch B is OFF.");
   Keyboard.print("b");
   }  // end if switchStateB is HIGH
}  // end of state change

// see if switch C is OFF or ON
byte switchStateC = digitalRead (switchPinC);

// has it changed since last time?
if (switchStateC != oldSwitchStateC)
{
oldSwitchStateC =  switchStateC;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateC == LOW)
   {
   Serial.println ("Switch C is ON.");
   Keyboard.print("c");
   }  // end if switchStateC is LOW
else
   {
   Serial.println ("Switch C is OFF.");
   Keyboard.print("c");
   }  // end if switchStateC is HIGH
}  // end of state change

// see if switch D is OFF or ON
byte switchStateD = digitalRead (switchPinD);

// has it changed since last time?
if (switchStateD != oldSwitchStateD)
{
oldSwitchStateD =  switchStateD;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateD == LOW)
   {
   Serial.println ("Switch D is ON.");
   Keyboard.print("d");
   }  // end if switchStateD is LOW
else
   {
   Serial.println ("Switch D is OFF.");
   Keyboard.print("d");
   }  // end if switchStateD is HIGH
}  // end of state change

// see if switch E is OFF or ON
byte switchStateE = digitalRead (switchPinE);

// has it changed since last time?
if (switchStateE != oldSwitchStateE)
{
oldSwitchStateE =  switchStateE;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateE == LOW)
   {
   Serial.println ("Switch E is ON.");
   Keyboard.print("e");
   }  // end if switchStateE is LOW
else
   {
   Serial.println ("Switch E is OFF.");
   Keyboard.print("e");
   }  // end if switchStateE is HIGH
}  // end of state change

// see if switch F is OFF or ON
byte switchStateF = digitalRead (switchPinF);

// has it changed since last time?
if (switchStateF != oldSwitchStateF)
{
oldSwitchStateF =  switchStateF;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateF == LOW)
   {
   Serial.println ("Switch F is ON.");
   Keyboard.print("f");
   }  // end if switchStateF is LOW
else
   {
   Serial.println ("Switch F is OFF.");
   Keyboard.print("f");
   }  // end if switchStateF is HIGH
}  // end of state change

// see if switch G is OFF or ON
byte switchStateG = digitalRead (switchPinG);

// has it changed since last time?
if (switchStateG != oldSwitchStateG)
{
oldSwitchStateG =  switchStateG;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateG == LOW)
   {
   Serial.println ("Switch G is ON.");
   Keyboard.print("g");
   }  // end if switchStateG is LOW
else
   {
   Serial.println ("Switch G is OFF.");
   Keyboard.print("g");
   }  // end if switchStateG is HIGH
}  // end of state change

// see if switch H is OFF or ON
byte switchStateH = digitalRead (switchPinH);

// has it changed since last time?
if (switchStateH != oldSwitchStateH)
{
oldSwitchStateH =  switchStateH;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateH == LOW)
   {
   Serial.println ("Switch H is ON.");
   Keyboard.print("h");
   }  // end if switchStateH is LOW
else
   {
   Serial.println ("Switch H is OFF.");
   Keyboard.print("h");
   }  // end if switchStateH is HIGH
}  // end of state change

// see if switch I is OFF or ON
byte switchStateI = digitalRead (switchPinI);

// has it changed since last time?
if (switchStateI != oldSwitchStateI)
{
oldSwitchStateI =  switchStateI;  // remember for next time 
delay (debounceTime);   // debounce
if (switchStateI == LOW)
   {
   Serial.println ("Switch I is ON.");
   Keyboard.print("i");
   }  // end if switchStateI is LOW
else
   {
   Serial.println ("Switch I is OFF.");
   Keyboard.print("i");
   }  // end if switchStateI is HIGH
}  // end of state change

}  // end of loop

Download .INO File

Wow, that’s a lot of code for something that seems so simple! Glad you found a solution. :slight_smile:

Yes, and that’s just code for 10 switches :smile: .

I also need help with my code to sync my switches with the ETS2 game upon start. I want Arduino to check the switch position of my dashboard and sync that with the game. Example: when my Light switch is on, I want the game to turn on the lights etc.

You should consider using arrays for the PINs so you can iterate over them (saves a lot of code).
You could even consider using one of the many Button libraries available.

Well despite its allot of code for just 9 switches, it does the job very well and reliable.

I started this code by using code from the many push-button with Debounce libraries out there. Please feel free to modify my code above and post it with your suggestion in it and I will try and post the results. I always prefer less code but code isn`t my once of my specialties.