code slows down on button press

Currently, I am making a semi-proportional mechanical joystick that controls mouse movements.
The issue is that the modifier switch, which is the second position to the joystick increases delay when it is active when I essentially want the opposite to occur.

#include <Keyboard.h>
#include "Mouse.h"

// Runescade mini bartop arcade
// set pin numbers for the buttons:

const int upButton = 0;
const int downButton = 1;
const int leftButton = 2;
const int rightButton = 3;
const int mouseMButton = 4;
const int mouseLButton = 5;
const int mouseRButton = 6;
const int shiftButton = 7;
const int escapeButton  = 8;
const int spaceButton = 9;
const int f1Button = 10;
const int f2Button = 16;
const int coinButton = 15;
const int modButton = 14;

int range = 2;              // output range of X or Y movement; affects movement speed
int responseDelay = 3;     // response delay of the mouse, in ms
int responseDelay2 = 1;

void setup() {

  // initialize the buttons' inputs:

  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);
  pinMode(mouseLButton, INPUT_PULLUP);
  pinMode(mouseRButton, INPUT_PULLUP);
  pinMode(mouseMButton, INPUT_PULLUP);
  pinMode(shiftButton, INPUT_PULLUP);
  pinMode(escapeButton, INPUT_PULLUP);
  pinMode(f1Button, INPUT_PULLUP);
  pinMode(f2Button, INPUT_PULLUP);
  pinMode(spaceButton, INPUT_PULLUP);
  pinMode(modButton, INPUT_PULLUP);
  pinMode(coinButton, INPUT_PULLUP);

  // initialize mouse control:

  Mouse.begin();
}

void loop() {

  // read the buttons:
  // read the buttons:


  int upState = digitalRead(upButton);
  int downState = digitalRead(downButton);
  int rightState = digitalRead(rightButton);
  int leftState = digitalRead(leftButton);
  int clickState = digitalRead(mouseMButton);
  int clickMState = digitalRead(mouseMButton);
  int clickRState = digitalRead(mouseRButton);
  int shiftState = digitalRead(shiftButton);
  int spaceState = digitalRead(spaceButton);
  int escapeState = digitalRead(escapeButton);
  int f1State = digitalRead(f1Button);
  int f2State = digitalRead(f2Button);
  int coinState = digitalRead(coinButton);
  int modState = digitalRead(modButton);

  // calculate the movement distance based on the button states:

if (modState == LOW) {
  int range = 2;
} else {
  int range = 1;
       }
  int  xDistance = (leftState - rightState) * range;

  int  yDistance = (upState - downState) * range;

  // if X or Y is non-zero, move:

  if ((xDistance != 0) || (yDistance != 0)) {

    Mouse.move(xDistance, yDistance, 0);

  }

  // Button binds:

  if (clickState == LOW) {
      Mouse.press(MOUSE_LEFT);
  } else {
      Mouse.release(MOUSE_LEFT);
  }
  if (clickRState == LOW) {
      Mouse.press(MOUSE_RIGHT);
  } else {
      Mouse.release(MOUSE_RIGHT);
  }
    if (clickMState == LOW) {
      Mouse.press(MOUSE_MIDDLE);
  } else {
      Mouse.release(MOUSE_MIDDLE);
  }
  if (shiftState == LOW) {

      Keyboard.press(KEY_RIGHT_SHIFT);
            delay(5);
      Keyboard.release(KEY_RIGHT_SHIFT);

  }
  if (escapeState == LOW) {

      Keyboard.press(KEY_ESC);
            delay(5);
      Keyboard.release(KEY_ESC);

  }
  if (spaceState == LOW) {

      Keyboard.press(32);
            delay(5);
      Keyboard.release(32);

  }
  if (f1State == LOW) {

      Keyboard.press(KEY_F1);
            delay(5);
      Keyboard.release(KEY_F1);

  }
  if (f2State == LOW) {

      Keyboard.press(KEY_F2);
            delay(5);
      Keyboard.release(KEY_F2);

  }
  if (coinState == LOW) {
  Keyboard.print("password");
    }
  // a delay so the mouse doesn't move too fast:
  delay(responseDelay);
}

I tried modifying range, delay and an else statement upon pin 14 registering both high or low.
What could be causing the increase of delay? Other button presses do not slow it down.
Edit: Forgot to mention I am using a 32u4 pro micro and I wasn't having this issue before either due to different ardunios or with the addition of the coin door code which seems to be operating just fine.

You have declared local variables here which make this code do nothing:

  if (modState == LOW)
  {
    int range = 2;
  }
  else
  {
    int range = 1;
  }

Delete the two occurrences of the word int.

still doesn't modify range, I tried 10 pixels per cycle with no results.

This is the code that worked fine:

  // a delay so the mouse doesn't move too fast:
if (modState == HIGH) {
  delay(responseDelay);
}
if (modState == LOW) {
  delay(responseDelay2);
  }

But it's causing an additional delay instead of switching to the lower delay. The other 8 keys don't cause this.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.