Problem in getting mouse to speed up automatically after 5 seconds

I have a macro key which increases/decreases the speed of the mouse. In void loop() I inserted a check for seeing if the speed is slow, and if it is 5 seconds after the time I last decreased the speed, to restore it.

However, the restore thing seems to get stuck somehow, in that after a while it puts me in a state where I am no longer able to decrease the speed at all! I am not able to specify the exact sequence of events that lead to this but you’ll get there!

To debug this, I made it so that if it is manually decreased, then an LED turns blue, and green if manually increased, but the auto increase will make it cyan. In the “stuck” state, it stays at cyan and neither goes to green nor blue though I press the macro key.

I have made a minimal test example sketch:

#include "Kaleidoscope.h"
#include "Kaleidoscope-MouseKeys.h"
#include "Kaleidoscope-Macros.h"
#include "Kaleidoscope-LEDControl.h"
#include "Kaleidoscope-LEDEffect-SolidColor.h"

enum { M_MOUSE_SPEED_CHANGE };

// *INDENT-OFF*
KEYMAPS(
  [0] = KEYMAP_STACKED(
    XXX,          Key_1, Key_2, Key_3, Key_4, Key_5, XXX,
    Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab,
    Key_PageUp,   Key_A, Key_S, Key_D, Key_F, Key_G,
    Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape,
    Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift,
    ShiftToLayer(1),

    XXX,       Key_6, Key_7, Key_8,     Key_9,      Key_0,         XXX,
    Key_Enter, Key_Y, Key_U, Key_I,     Key_O,      Key_P,         Key_Equals,
               Key_H, Key_J, Key_K,     Key_L,      Key_Semicolon, Key_Quote,
    XXX,       Key_N, Key_M, Key_Comma, Key_Period, Key_Slash,     Key_Minus,

    Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
    ShiftToLayer(1)
  ),

  [1] = KEYMAP_STACKED(
    XXX, XXX, XXX,        XXX,         XXX,        XXX, XXX,
    XXX, XXX, XXX,        Key_mouseUp, XXX,        XXX, Key_mouseBtnL,
    XXX, XXX, Key_mouseL, Key_mouseDn, Key_mouseR, XXX,
    XXX, XXX, XXX,        XXX,         XXX,        XXX, M(M_MOUSE_SPEED_CHANGE),
    ___, ___, ___, ___,
    ___,

    ___, XXX, XXX, XXX, XXX, XXX, XXX,
    ___, XXX, XXX, XXX, XXX, XXX, XXX,
         XXX, XXX, XXX, XXX, XXX, XXX,
    ___, XXX, XXX, XXX, XXX, XXX, XXX,
    ___, ___, ___, ___,
    ___
  ),
)
// *INDENT-ON*

#define MOUSE_SPEED_FAST 15
#define MOUSE_SPEED_SLOW  5
#define MOUSE_INCREASE_SPEED MouseKeys.speed = MOUSE_SPEED_FAST
#define MOUSE_DECREASE_SPEED MouseKeys.speed = MOUSE_SPEED_SLOW
uint16_t mouseSpeedDecreasedTimestamp;

const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
  switch (macroIndex) {
    case M_MOUSE_SPEED_CHANGE:
        if (keyToggledOn(keyState)) {
            if (MouseKeys.speed == MOUSE_SPEED_FAST) {
                MOUSE_DECREASE_SPEED;
                mouseSpeedDecreasedTimestamp = millis();
                ::LEDControl.setCrgbAt(0, CRGB(0, 0, 255));
            } else {
                MOUSE_INCREASE_SPEED;
                ::LEDControl.setCrgbAt(0, CRGB(0, 255, 0));
            }
        }
        break;
  }
  return MACRO_NONE;
}

kaleidoscope::LEDSolidColor LEDSolidYellow(130, 100, 0);

KALEIDOSCOPE_INIT_PLUGINS(
  MouseKeys,
  Macros,
  LEDControl,
  LEDSolidYellow
);

void setup() {
  Kaleidoscope.setup();
  MouseKeys.speed = MOUSE_SPEED_FAST;
  LEDSolidYellow.activate();
}

void loop() {
  Kaleidoscope.loop();
  if (MouseKeys.speed == MOUSE_SPEED_SLOW &&
      millis() - mouseSpeedDecreasedTimestamp > 5000)
      // 5s since we decreased the speed; increase it
  {
      MOUSE_INCREASE_SPEED;
      ::LEDControl.setCrgbAt(0, CRGB(0, 255, 255));
  }
}

Any help in debugging much appreciated!