Creating a mobile engine for my baby son

Dear Arduino guys,
I’m new in the arduino business and at the moment I’m trying to construct a mobile engine for my baby son.
I used an old stepper motor from a printer and so far the engine is spinning and everything works. I advanced my motor to
turn around after 500 steps and so on. Now I want to add a speed control via a rotary encoder. With the code below this works only eventually.
I attached the outer two pins of the rotary encoder to A8 and A9 with a 10 k resistor in between which is connected to 5V also. The pin in the middle of the encoder is connected to GND. To see if it is working I print the speed to the serial monitor. The speed somehow fluctuates, but I think this does not have anything to do with the rotary encoder, its like as if the speed changes by pure chance. Furthermore I would like the engine to slow down before it is turning, maybe you have an idea for this.
greetings confugere

Here is my programm so far:
#include <Stepper.h>

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, dirA, dirB);

int stepCount = 0; // number of steps the motor has taken

int pos = 0; // variable to store the stepper position
int vspeed = 100;
int ispeed = 10;
const int pin_A = A8;
const int pin_B = A9;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;

void setup() {
// initialize the serial port:
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);

// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);

// Log some shit
Serial.begin(9600);
pinMode(pin_A, INPUT);
pinMode(pin_B, INPUT);
}

void loop()
{
for(pos = 0; pos < 500; pos += 1) // goes from 0 to 500 degrees
{
// step one step:
myStepper.step(1);
stepCount++;
//Serial.print(“Position:” );
//Serial.println(pos);
//Serial.print(“Schritte:” );
//Serial.println(stepCount);
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
// A has gone from high to low
if(encoder_B) {
// B is high so clockwise
// increase the speed via delay, dont go over 255
if(vspeed + ispeed <= 255) vspeed += ispeed;
}
else {
// B is low so counter-clockwise
// decrease the speed, dont go below 0
if(vspeed - ispeed >= 0) vspeed -= ispeed;
}
encoder_A_prev = encoder_A;
delay(vspeed);
Serial.print(vspeed);
}
}
for(pos = 500; pos >= 1; pos -= 1) // goes from 0 to 500 degrees
{
// step one step:
myStepper.step(-1);
stepCount++;
//Serial.print(“Position:” );
//Serial.println(pos);
//Serial.print(“Schritte:” );
//Serial.println(stepCount);
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
// A has gone from high to low
if(encoder_B) {
// B is high so clockwise
// increase the speed via delay, dont go over 255
if(vspeed + ispeed <= 255) vspeed += ispeed;
}
else {
// B is low so counter-clockwise
// decrease the speed, dont go below 0
if(vspeed - ispeed >= 0) vspeed -= ispeed;
}
}
encoder_A_prev = encoder_A;
delay(vspeed);
Serial.println(vspeed);
}
}

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, dirA, dirB);

The code and the comment do not agree. Fix, or delete, the comment.

void setup() {
  // initialize the serial port:
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);

  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);

  // Log some shit
  Serial.begin(9600);
  pinMode(pin_A, INPUT);
  pinMode(pin_B, INPUT);
}

What shield are you using? 90% of what you are doing here is wrong.

Is the rotary encoder attached to the stepper motor? If not, a simple potentiometer would be a far better choice of hardware to select the delay() interval with.

Ah, I’m using a motor shield, I mean, the stepper is working perfectly.

the stepper is working perfectly.

In spite of the do this/no do that code in setup(). Not because of it.

About the encoder?

I just wanted to regulate the delay time with the rotary encoder. So each step the programm reads the rotary encoder and adapts the delay time.

I mean, I'm also a bit new to the business and I worked on some tutorials and combined some things I learned in these. From one tutorial I took the code for controling the stepper engine.