With the help of some examples, I wrote a program to map switches to keyboard strokes.
It's aimed for an arduino micro that has five footswitches connected to the pins 2 to 6. The program works, but I need a delay in the loop otherwise keystrokes are triggered more then once. I tested how low can I set that delay and found 12 as working value. While the program does work, I still wonder, why I need that delay.
// Including the keyboard library
#include <Keyboard.h>
// Including the mouse library
#include <Mouse.h>
// The Values for my Keys
const char keyValueA = 'w';
const char keyValueB = 's';
const char keyValueC = 'q';
const char keyValueD = 'e';
const char keyValueE = KEY_SCROLL_LOCK;
// Time for delay. The script needs a delay, otherwise it triggers over
// and over again. Values that worked:
// 50 - OK
// 40 - OK
// 30 - OK
// 20 - OK
// 12 - OK
// 11 - triggers double extremly rarely
// 10 - triggers double rarely
// 9 - triggers double sometimes
// 8 - triggers double often
// 7 - triggers double
// 6 - triggers double
// 5 - triggers double
const int delayValue = 12;
// Declaring variables for the pins
const int pinA = 2;
const int pinB = 3;
const int pinC = 4;
const int pinD = 5;
const int pinE = 6;
// Variables for the previous status of the switch
int previousSwitchStateA = HIGH;
int previousSwitchStateB = HIGH;
int previousSwitchStateC = HIGH;
int previousSwitchStateD = HIGH;
int previousSwitchStateE = HIGH;
void setup()
{
// Setting up the internal pull-ups resistors and also setting the
// pins to inputs.
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
pinMode(pinC, INPUT_PULLUP);
pinMode(pinD, INPUT_PULLUP);
pinMode(pinE, INPUT_PULLUP);
// as seen in this example:
// https://www.instructables.com/Arduino-Programmable-Button-Panel-As-Keyboard/
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
// start the keyboard
Keyboard.begin();
}
// the permanent running loop
void loop()
{
//checking the states of the switches
int switchStateA = digitalRead(pinA);
int switchStateB = digitalRead(pinB);
int switchStateC = digitalRead(pinC);
int switchStateD = digitalRead(pinD);
int switchStateE = digitalRead(pinE);
// Checking if the first switch has been pressed and the variable for a
// pressed key not already set
if ( (switchStateA == LOW) && (switchStateA != previousSwitchStateA) )
{
// press the key
Keyboard.press(keyValueA);
delay(delayValue);
}
if ( (switchStateA == HIGH) && (switchStateA != previousSwitchStateA) )
{
// release the key
Keyboard.release(keyValueA);
delay(delayValue);
}
// Checking if the second switch has been pressed and the variable for a
// pressed key not already set
if ( (switchStateB == LOW) && (switchStateB != previousSwitchStateB) )
{
// and it's currently pressed:
Keyboard.press(keyValueB);
delay(delayValue);
}
if ( (switchStateB == HIGH) && (switchStateB != previousSwitchStateB) )
{
// and it's currently released:
Keyboard.release(keyValueB);
delay(delayValue);
}
// Checking if the third switch has been pressed and the variable for a
// pressed key not already set
if ( (switchStateC == LOW) && (switchStateC != previousSwitchStateC) )
{
// and it's currently pressed:
Keyboard.press(keyValueC);
delay(delayValue);
}
if ( (switchStateC == HIGH) && (switchStateC != previousSwitchStateC) )
{
// and it's currently released:
Keyboard.release(keyValueC);
delay(delayValue);
}
// Checking if the fourth switch has been pressed and the variable for a
// pressed key not already set
if ( (switchStateD == LOW) && (switchStateD != previousSwitchStateD) )
{
// and it's currently pressed:
Keyboard.press(keyValueD);
delay(delayValue);
}
if ( (switchStateD == HIGH) && (switchStateD != previousSwitchStateD) )
{
// and it's currently released:
Keyboard.release(keyValueD);
delay(delayValue);
}
// Checking if the fifth switch has been pressed and the variable for a
// pressed key not already set
if ( (switchStateE == LOW) && (switchStateE != previousSwitchStateE) )
{
// and it's currently pressed:
Keyboard.press(keyValueE);
delay(delayValue);
}
if ( (switchStateE == HIGH) && (switchStateE != previousSwitchStateE) )
{
// and it's currently released:
Keyboard.release(keyValueE);
delay(delayValue);
}
// save the states
previousSwitchStateA = switchStateA;
previousSwitchStateB = switchStateB;
previousSwitchStateC = switchStateC;
previousSwitchStateD = switchStateD;
previousSwitchStateE = switchStateE;
}
