Looking for some help setting up an Analog joystick on a Micro as a HID in which upon threshold trigger a translated key press.
It is working for the 4 basic directions but stuck on how to repeat multiple key presses for the diagonals along with flowing between the stick directions.
code is as follows:
/* HID Joystick Mouse Example
by: Jim Lindblom
date: 1/12/2012
*/
#include <Keyboard.h>
#include <Mouse.h>
int horzPin = A1;
int vertPin = A0;
int selPin = 9;
int vertZero, horzZero;
int vertValue, horzValue;
const int sensitivity = 200;
int mouseClickFlag = 0;
//int invertMouse = 1;
int invertMouse = -1;
void setup()
{
pinMode(horzPin, INPUT);
pinMode(vertPin, INPUT);
pinMode(selPin, INPUT);
digitalWrite(selPin, HIGH);
//delay(10);
vertZero = analogRead(vertPin);
horzZero = analogRead(horzPin);
Mouse.begin();
}
void loop()
{
vertValue = analogRead(vertPin) - vertZero;
horzValue = analogRead(horzPin) - horzZero;
if (vertValue > 10 && horzValue == 0) {
Keyboard.press('w');
}
else if (vertValue < -10) {
Keyboard.press('s');
}
else if (horzValue > 10) {
Keyboard.press('d');
}
else if (horzValue < -10) {
Keyboard.press('a');
}
else if (horzValue == 0 && vertValue == 0){
Keyboard.releaseAll();
// delay(1);
}
Serial.println(String(horzValue) + "," + String(vertValue));
}