Hello, I am trying to create a game controller with my Arduino Leonardo. It has 4 capacitive buttons but it also has some problems. One of them is holding multiple keys repeats keys instead of keeping them held down.
Pin 4 is connected to a line on a breadboard, to that line there are 4 1M resistors, each connected at the end to 4 pieces of aluminium foil and separate pins on the arduino (see code).
I am pretty sure this is a problem with either the keyboard library implementation itself or my implementation. Here is the code:
//#define ENCODER_SUPPORT
// Official Arduino libraries
#include <Keyboard.h>
#include <Mouse.h>
// 3rd party libraries. Make sure you have them!
#include <CapacitiveSensor.h>
// Button pins
// Send pin
#define BTAPINA 4
// Receive pins
#define BTAPINB 2
#define BTBPINB 6
#define BTCPINB 8
#define BTDPINB 10
// SWITCH
#define SWITCHPIN 13
// https://github.com/Kvathe/kappa-pad/blob/master/KappaPad/CapacitiveKey.h
class CapacitiveKey {
public:
CapacitiveSensor* sensor;
bool keyReleased = true;
char key;
unsigned int releaseDelay = 20;
unsigned int releaseTimer;
unsigned int threshold;
int led;
unsigned int sample;
CapacitiveKey(uint8_t sendPin, uint8_t receivePin, unsigned int capacitiveThreshold, char keyboardKey) {
sensor = new CapacitiveSensor(sendPin, receivePin);
threshold = capacitiveThreshold;
key = keyboardKey;
}
~CapacitiveKey() {
delete sensor;
}
void keyUpdate(bool keyboardActive) {
sample = sensor->capacitiveSensorRaw(1);
if (sample > threshold) {
if (keyReleased) {
if (keyboardActive) Keyboard.press(key);
keyReleased = false;
}
releaseTimer = releaseDelay;
}
else {
if (!keyReleased) {
if (releaseTimer == 0) {
Keyboard.release(key);
keyReleased = true;
}
else {
releaseTimer--;
}
}
}
}
};
// Encoders
// Pin declarations
#ifdef ENCODER_SUPPORT
#define ENC_1_PIN_A 16
#define ENC_1_PIN_B 17
#define ENC_2_PIN_A 18
#define ENC_2_PIN_B 19
#endif
// Object declarations
#ifdef ENCODER_SUPPORT
Encoder encoder_left(ENC_1_PIN_A, ENC_1_PIN_B);
Encoder encoder_right(ENC_2_PIN_A, ENC_2_PIN_B);
#endif
#define SENSITIVITY 100
#define BTAKEY 'a'
#define BTBKEY 's'
#define BTCKEY 'k'
#define BTDKEY 'l'
CapacitiveKey btna = CapacitiveKey(BTAPINA, BTAPINB, SENSITIVITY, BTAKEY);
CapacitiveKey btnb = CapacitiveKey(BTAPINA, BTBPINB, SENSITIVITY, BTBKEY);
CapacitiveKey btnc = CapacitiveKey(BTAPINA, BTCPINB, SENSITIVITY, BTCKEY);
CapacitiveKey btnd = CapacitiveKey(BTAPINA, BTDPINB, SENSITIVITY, BTDKEY);
void
setup()
{
pinMode(SWITCHPIN, OUTPUT);
bool keyboardEnabled = digitalRead(SWITCHPIN);
if (!keyboardEnabled) {
// Setup mouse and keyboard
Keyboard.begin();
Mouse.begin();
}
#if OUTPUT
// We might print debug information later,
Serial.begin(9600);
#endif
}
void
loop()
{
check_key_events();
#ifdef ENCODER_SUPPORT
encoder_func_left();
encoder_func_right();
#endif
}
void
check_key_events()
{
bool keyboardEnabled = digitalRead(SWITCHPIN);
btna.keyUpdate(!keyboardEnabled);
btnb.keyUpdate(!keyboardEnabled);
btnc.keyUpdate(!keyboardEnabled);
btnd.keyUpdate(!keyboardEnabled);
}
#ifdef ENCODER_SUPPORT
void
encoder_func_left()
{
Mouse.move(encLeft.read(), 0, 0);
encoder_right.write(0);
}
void
encoder_func_right()
{
Mouse.move(0, encRight.read(), 0);
encoder_right.write(0);
}
#endif
Is there a way to fix this issue? Also there's a small delay but I think that deserves a separate thread.