9V vs. USB supplied power Affecting Motor on/off

I am currently working on a sketch/project with an Arduino Uno to that first allows the user to control how many times a stepper motor runs through a cycle and then through user input begins the cycles.

When the sketch/circuit is connected to the USB power supply it works as intended, when I plug in a 9V DC power supply without the USB connected, the program just starts and the motor immediately starts running without user input. If I have both the 9V supply and USB plugged in it works as intended.

The code is the same each time, which is why I am having trouble wrapping my head around why the voltage level would cause this.

Thanks.

My code is:

#include <LiquidCrystal.h>
#include <Stepper.h>

#define STEPS 200

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Stepper stepper(STEPS, 7, 8, 9, 10);

//Input & Button Logic
const int numOfInputs = 3;
const int inputPins[numOfInputs] = {0,13,6};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0};
long debounceDelay = 5;

//LCD Menu Logic
const int numOfScreens = 1;
int currentScreen = 0;
String screens[numOfScreens][2] = {{"Cycles",""}};
int parameters[numOfScreens];

int SWITCH = 0;

int cycles = 1;

void setup() {
  for(int i = 0; i < numOfInputs; i++) {
    pinMode(inputPins[i], INPUT);
    //digitalWrite(inputPins[i], HIGH); // pull-up 20k
    
  }
  Serial.begin(9600);
  parameters[0] = (int)50; //Test Type
  lcd.begin(16,2);   //Initialize LCD
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Friction Test");
}

void loop() {
  setInputFlags();
  resolveInputFlags();
  if(SWITCH == 1){
    int sensorReading = analogRead(A0);
    int motorSpeed = map(sensorReading,0,1023,0,100);
    if (motorSpeed > 0){
      stepper.setSpeed(motorSpeed);
    }
    armRotate();
    SWITCH = 0;
    cycles = 1;
  }
}

void setInputFlags() {
  for(int i = 0; i < numOfInputs; i++) {
    int reading = digitalRead(inputPins[i]);
    if (reading != lastInputState[i]) {
      lastDebounceTime[i] = millis();
    }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading != inputState[i]) {
        inputState[i] = reading;
        if (inputState[i] == HIGH) {
          inputFlags[i] = HIGH;
        }
      }
    }
    lastInputState[i] = reading;
  }
}

void resolveInputFlags() {
  for(int i = 0; i < numOfInputs; i++) {
    if(inputFlags[i] == HIGH) {
      inputAction(i);
      inputFlags[i] = LOW;
      printScreen();
    }
  }
}

void inputAction(int input) {
  if(input == 0) {
    parameters[currentScreen]++;;
  }
  else if(input == 1) {
    parameters[currentScreen]--;
  }
  else if(input == 2){
    if (SWITCH == 1){
      SWITCH = 0;
    }
    else if (SWITCH == 0){
      SWITCH = 1;
    }
  }
} 

void printScreen() {
  lcd.clear();
  lcd.print(screens[currentScreen][0]);
  lcd.setCursor(0,1);
  lcd.print(parameters[currentScreen]);
  lcd.print(" ");
  lcd.print(screens[currentScreen][1]);
}

void armRotate(){
  while(cycles<=parameters[0]){
    lcd.clear();
    lcd.setCursor(0,0); lcd.print("Rotation #"); lcd.print(cycles); lcd.print("of"); lcd.print(parameters[0]);
    stepper.step(STEPS/3);
    stepper.step(-STEPS/3);
    cycles++;
  }
  SWITCH = 0;
}

You don't say which Arduino board, but I'll guess the Uno. It's not a good idea to power the Uno with both a USB and an external source. You could damage the on-board voltage regulator. What's likely happening is that the 9V supply does not hold up to the load of the Uno and the stepper motor.

If you look at the +5-v pin when you power the board form the 9V source, you will likely see 4.5V or less.

@SteveMann Thanks, editted the post to reflect that I am indeed using an Uno. And sorry, if I understand correctly, you are saying that the 9V supply on its own is not enough to power the Uno and the Stepper motor?

@Delta_G
I just threw together a schematic of how the buttons are wired currently. To be honest, I go to this configuration through trial and error for what would cause the "menu" to act according to how I wanted it. The button in the middle is the one used to start the cycling, with the 2 outer buttons used for changing the # of cycles up or down.

Buttons.PNG

@Delta_G Not trial and error as the method of wiring originally, just that when I originally had them all grounded, it would also begin running immediately when power was supplied.

Thanks for the link there, I changed the code to have INPUT_PULLUP and will ground all the buttons again.

Do not use digital pins 0 and 1 on the uno, those are already in use by the serial port. That is likely what is causing the problems with your sketch.

You can use the analog pins as digital inputs or outputs, they will work with the same digitalRead and digitalWrite instructions, just need to specify the pins as A0 through A5 instead of the numbers 0 through 13.

Also, all variables used to store the millis() timer need to be "unsigned long".

@David_2018 Thanks for that, I didn't realize I could use the analog pins as digital. I was just unplugging the pin 0 connection while uploading as to not affect the communication.