Stepper motor is being weird please help

Hi, I have a problem with my code I am trying to make a stepper motor rotate one way when one of two buttons is pressed and stop if none are pressed if the other button is pressed I want the stepper to rotate the other way. The problem I'm facing is that when I'm not pressing any buttons the stepper is still turning and when I press the buttons it doesn't affect the motor at all. Here is my code:


#include <Stepper.h>

Stepper myStepper(2038, 8, 9, 10, 11);

void setup() {
  myStepper.setSpeed(10);
  
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  while (digitalRead(5) == LOW) {
    myStepper.step(100); 
    Serial.println("left button press registered");
  }
  while (digitalRead(4) == LOW) {
    myStepper.step(-100); 
    Serial.println("right button press registered");
  }
}

As you see from my code if a button is pressed there is a message printed on the serial monitor. The problem is that even when I'm not pressing the button the message still is sent (I checked with multiple components like microcontrollers and such so it's not a hardware issue).

Here is the wiring diagram:

which motor?

Test first things first. Get rid of the motor and its code, then test just the buttons.
Does this code works or not?

//#include <Stepper.h>
//Stepper myStepper(2038, 8, 9, 10, 11);

void setup() {
  //myStepper.setSpeed(10);
  
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  while (digitalRead(5) == LOW) {
    //myStepper.step(100); 
    Serial.println("left button press registered");
  }
  while (digitalRead(4) == LOW) {
    //myStepper.step(-100); 
    Serial.println("right button press registered");
  }
}

Once it works properly, add the motor (which one?) and driver (which one? It looks like an ULN2003...) back again, but remember to connect GND from Arduino to the module and see what happens. And remember you need to power the motor with a separate power supply, not from Arduino.

The motor is the 28BYJ-48 and the driver is the ULN2003

look if this works by you:

const byte Pins[4] = {2, 3, 4, 5}; //  - 28BYJ-48

const int motorSpeed = 3;

int Counter = 0;
const int CountsPerRev = 512;
const byte Bits[8] = {
  B01000,
  B01100,
  B00100,
  B00110,
  B00010,
  B00011,
  B00001,
  B01001
};

//////////////////////////////////////////////////////////////////////////////
void setup() {
  pinMode(Pins[0], OUTPUT);
  pinMode(Pins[1], OUTPUT);
  pinMode(Pins[2], OUTPUT);
  pinMode(Pins[3], OUTPUT);
  //Serial.begin(115200);
}

void loop() {
  if (Counter < CountsPerRev)    clockwise();
  else if (Counter >= CountsPerRev * 2)    Counter = 0;
  else    anticlockwise();
  Counter++;
}

//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
// функция поворачивает мотор против часовой стрелки.
void anticlockwise() {
  for (byte i = 0; i < 8; i ++) {
    setOutput(i);
  }
}

// функция поворачивает мотор по часовой стрелке.
void clockwise() {
  for (byte i = 8; i > 0; i --) {
    setOutput(i - 1);
  }
}

void setOutput(byte out) {
  Serial.println(out);
  digitalWrite(Pins[0], Bits[out]&(1<< 0));
  digitalWrite(Pins[1], Bits[out]&(1<< 1));
  digitalWrite(Pins[2], Bits[out]&(1<< 2));
  digitalWrite(Pins[3], Bits[out]&(1<< 3));
  Serial.print(digitalRead(Pins[0]));
  Serial.print(digitalRead(Pins[1]));
  Serial.print(digitalRead(Pins[2]));
  Serial.println(digitalRead(Pins[3]));
  delay(motorSpeed);
}

should you read pin 6 instead of 4?

That is very true, I tried a lot of different codes and it's just a mix-up but I changed the code as you said and the issue is still present.

which pins are your buttons connected to?

You can see the wiring diagram they are connected to pins 5 and 6

how you power this setup? no GND to Arduino?

My fault I took a bad screenshot of the Arduino in the wiring diagram but the Arduino is powered by the USB port.

I'm so confused the issue is still there. Then maybe it's the hardware but I tried with many different components and microcontrollers.

after what? connected GND between UNO and ULN2003 module?

Yes I did all the things you said could be the problem and yet the motor just keeps spinning freely

As I already said in post#3, you first need to check if the buttons are working as expected.
Have you tested that and the buttons work correctly? And when the answer is "yes", put back the motor, make sure you don't power the motor from Arduino, and connect GND in common and see if it works now.

Are you sure your buttons are wired correctly? What happens if you pull the wires out of pins 5 & 6?

This:

Does not answer this:

Yes, sounds like buttons are wrong..
Use opposite corners..

good luck.. ~q

Opposite corners:

I get spammed with serial monitor messages and the stepper still moves freely