Hello,
I connected a button and a joystick to a Leonardo and i was able to get the joystick running and also a macro on the button . The macro automates a click and a key press every roughly 35 minutes. The button is working as a on and off for the macro. Everything works except i once the macro is running i cant get it to stop. It's not a problem since i can just reset the Leonardo but still, could someone take a look at the code?
#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(50, 70);
Mouse.press();
Keyboard.press(KEY_LEFT_GUI);
delay (randomDelay);
Mouse.release();
Keyboard.releaseAll();
int32_t randomDelay2 = random(1845684, 2365644);
delay (randomDelay2) ;
}
}
}
thanks