mrtunes:
Thanks - do you think I would still be able to find a way to end the burst of keystrokes with the method i've used above?
Sure. I think the problem is that you send a keystroke whenever the value is between 300 and 600. Typically you would want a dead zone between "Pressed" and "Released". I could not find the MovingAverageFilter library in the library manager so I installed "movingAvg" and used that instead.
#include <Keyboard.h>
#include <movingAvg.h>
// Original values were 200 and then 600
const int PressedMaxThreshold = 412;
const int ReleasedMinThreshold = 612;
const byte PinCount = 2;
const byte InputPins[PinCount] = {A0, A1};
const char KeyCodes[PinCount] = {'d', 's'};
struct TouchInput
{
byte analogPin;
char keycode;
movingAvg filter = movingAvg(20);
boolean wasPressed = false;
};
TouchInput Pins[PinCount];
void setup()
{
Serial.begin(115200);
for (int i = 0; i < PinCount; i++)
{
Pins[i].analogPin = InputPins[i];
Pins[i].keycode = KeyCodes[i];
Pins[i].filter.begin();
}
}
void loop()
{
for (int i = 0; i < PinCount; i++)
{
float currentAverage = Pins[i].filter.reading(analogRead(Pins[i].analogPin));
boolean previousState = Pins[i].wasPressed;
boolean currentState = previousState; // Default if in the dead zone
if (currentAverage < PressedMaxThreshold)
{
currentState = true; // Pressed
}
else if (currentAverage > ReleasedMinThreshold)
{
currentState = false; // Released
}
if (currentState != previousState)
{
if (currentState)
Keyboard.press(Pins[i].keycode);
else
Keyboard.release(Pins[i].keycode);
}
Pins[i].wasPressed = currentState;
}
}