Hi,
I connected a joystick to a Leonardo, and it works fine but sometimes i find that it starts sending input as if i was tilting the joystick to the right bottom corner even if i’m not touching anything. Like this:
Here is the code i’m using. It contains also a little macro that is activate by a toggle switch.
#include <Mouse.h>
#include <Keyboard.h>
#include <Joystick.h>
Joystick_ Joystick;
int8_t joystickType = 0x02;
float Multiplier = 1.2;
bool includeXAxis = true;
bool includeYAxis = true;
const int Analog1 = 0;
const int Analog2 = 1;
int XValue = 0;
int YValue = 0;
int inPin = 2; // the number of the input pin
int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
Joystick.begin();
delay(1000);
pinMode(inPin, INPUT_PULLUP);
}
void loop() {
XValue = analogRead(Analog1);
YValue = analogRead(Analog2);
XValue = XValue * Multiplier;
YValue = YValue * Multiplier;
Joystick.setXAxis(XValue); //YAW
Joystick.setYAxis(YValue); // Throttle
reading = digitalRead(inPin);
// if the input just went from HIGH and LOW and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state == LOW)
state = HIGH;
else
state = LOW;
time = millis();
}
previous = reading;
if (state == HIGH) {
uint16_t randomDelay = random(350, 565);
Keyboard.press('y');
delay (randomDelay);
Keyboard.releaseAll();
int32_t randomDelay2 = random(1845684, 2365644);
delay (randomDelay2) ;
}
}
Anyone please?
thanks