Help with my code

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

Everything works except i once the macro is running i cant get it to stop.

When do you want it to stop?

Simply make a note of the millis timer value in setup. Then in the loop function see if the time you want it to run has beed exceeded and if so just drop into an infinite loop:-

while (1) ;

[b]time[/b] should be unsigned long. Although that doesn't entirely explain the problem. It should work for a few days.

Thanks for replies. The idea was that the switch would both turn on and off the macro. The macro should run only as long as the switch is on.

The code you have seems suitable for a momentary button.

What kind of "switch" do you have?

The idea was that the switch would both turn on and off the macro.

First a niggle, it is not a macro. A macro is a series of statements that is placed when a key word is encountered.

Try adding this line:-

 time = millis();

after the

delay (randomDelay2) ;

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Grumpy_Mike:
First a niggle, it is not a macro. A macro is a series of statements that is placed when a key word is encountered.

Try adding this line:-

 time = millis();

after the

delay (randomDelay2) ;

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Hi ok sorry i changed it from quote to code.
I tried adding that line but it didn't work. Actually i was able to stop the automation with the button before i added the 35 minute delay. Somehow this seems to block the code.

Actually i was able to stop the automation with the button before i added the 35 minute delay

Ok that is good.
However that very long delay is a bad thing as the code is blocked from doing anything for a long time. If that is what you want then fine but normally it is not.

The way to avoid the blocking code is to use again the technique you used for the debounce but with different variables to stop certain aspects of your code during this time. It would also be possible to cancel this delay during it again by actions outside the delay.

This is the basis of multitasking.