Arduino Micro sketch will not work properly without opening serial

I have a sketch that I am working on that uses the arduinojoysticklibrary that I have uses many times before, and a new library called ErriezRotaryFullStep. My sketch works great, but only when I open the serial monitor. There is no code in the library file of the fullstep library nor the sketch that calls for it to wait for serial. Here is my code:

#include <Joystick.h>
#include <ErriezRotaryFullStep.h>
Joystick_ Joystick = Joystick_(0x05, 0x04,                         // HID ID (if running more than 1 Arduinos that use the Joystick library, you can not have this ID twice. DO NOT USE 1 or 2 as Arduino uses those 2 for the default Mouse and keyboard libraries!!!), joystick type
                               5, 0,                           // Button Count, Hat Switch Count
                               false, false, false,             // No X, Y, and Z Axis
                               false, false, false,             // No Rx, Ry, or Rz
                               false, false,                    // No rudder or throttle
                               false, false, false);            // No accelerator, brake, or steering)
// Connect rotary pins to the DIGITAL or ANALOG pins of the Arduino board
// Use A0..A7 when using analog pins
#define ROTARY1_PIN1        3
#define ROTARY1_PIN2        2
#define ROTARY2_PIN1        6
#define ROTARY2_PIN2        5
#define smallbutton         4

// Initialize half step rotary encoder with internal pull-up pins enabled
// and default sensitivity=100
RotaryFullStep rotary1 = {
  RotaryFullStep(ROTARY1_PIN1, ROTARY1_PIN2)
};
RotaryFullStep rotary2 = {
  RotaryFullStep(ROTARY2_PIN1, ROTARY2_PIN2)
};

// Global variables
int pinStateLast[2];
int lastsmallState;
int prevrotary [2];
int rotaryState1;
int rotaryState2;
int currentrotary [2];

unsigned long startMillisknob;
unsigned long currentMillisknob;
const unsigned long timeframeknob = 15;
unsigned long startMillisrebound;
unsigned long currentMillisrebound;
const unsigned long timeframerebound = 75;
unsigned long startMillisbutton;
unsigned long currentMillisbutton;
const unsigned long timeframebutton = 10;

void setup()
{
  // Enable internal pull-up for the rotary button pins
  pinMode(smallbutton, INPUT_PULLUP);
  startMillisknob = millis();
  startMillisrebound = millis();
  startMillisbutton = millis();
  Joystick.begin();
}

void loop () {
  currentMillisbutton = millis();
  if (currentMillisbutton - startMillisbutton >= timeframebutton) {
    int currentsmallState = !digitalRead(smallbutton);
    if (currentsmallState != lastsmallState) {
      Joystick.setButton(4 , currentsmallState);
      lastsmallState = currentsmallState;
    }
  }
  rotaryState1 = rotary1.read();
  if (rotaryState1 > 0) {
    currentrotary[0]++;
  }
  if (rotaryState1 < 0) {
    currentrotary[0]--;
  }
  rotaryState2 = rotary2.read();
  if (rotaryState2 > 0) {
    currentrotary[1]++;
  }
  if (rotaryState2 < 0) {
    currentrotary[1]--;
  }
  if (currentrotary[0] != prevrotary[0]) {
    if (rotaryState1 > 0) {
      Joystick.setButton(0, HIGH);
    }
    if (rotaryState1 < 0) {
      Joystick.setButton(1, HIGH);
    }
  }
  if (currentrotary[1] != prevrotary[1]) {
    if (rotaryState2 > 0) {
      Joystick.setButton(2, HIGH);
    }
    if (rotaryState2 < 0) {
      Joystick.setButton(3, HIGH);
    }
  }
  prevrotary[0] = currentrotary[0];
  prevrotary[1] = currentrotary[1];
  currentMillisrebound = millis();
  if (currentMillisrebound - startMillisrebound >= timeframerebound) {
    Joystick.setButton(0, LOW);
    Joystick.setButton(1, LOW);
    Joystick.setButton(2, LOW);
    Joystick.setButton(3, LOW);
    startMillisrebound = currentMillisrebound;
  }
}

The code is messy yes but it works flawlessly when I open serial. Does anyone know why this is happening? I have done some searches but have not come up with a good answer.

Belay my last, I must have done something odd to make the sketch not work. All is working now with seemingly no changes. All I did was reboot (again) and reupload (again).

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