Stepper Motor Running on program start

Hi,

Can someone help me out here, since cannot get the picture why is the stepper motor runs, 1s run yes, on the program starts?
It should only move when the button is pressed but cannot understand why it's moving a couple of seconds before and stops and then the button will function ok.

And secondly why ins't the button reseting, meaning, when te button is pressed twice it should stop the motor, or it would be better to have a second button?

#include <Stepper.h>

// Declare the used pins
int dirA = 4;
int dirB = 5;
int pwmA = 2;
int pwmB = 3;

int switchState;
long randNumber;
const int buttonPin = 6;
int counter = 0;

// Declare a Stepper motor with 200 steps 
Stepper stepper1(200, dirA, dirB);

void setup() {
  Serial.begin(9600);
  
  // PWM pins require declaration when used as Digital
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  
  // Set PWM pins as always HIGH
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  
  //Button1
  pinMode(buttonPin, INPUT);
  
  // Set stepper motor speed
  //stepper1.setSpeed(3000);

}
void loop(){
 
  randNumber = random(3000, 7000);
  Serial.println(randNumber); 

  int buttonState;
  buttonState = digitalRead(buttonPin);

 if (buttonState == LOW)
  {
    counter++;
  }
 if (counter == 1){
      // Number of steps for the motors
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
      // Seconds to wait
    delay(10);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
    delay(10);
  } 
 else if (counter == 2)
  {
    stepper1.step(0);
  }
  else {
    counter = 0;
  }
 }

Does your button pin have a pull-up or pull-down resistor to keep it from floating when the button is open? It needs one or the other.

It looks like your sketch is expecting a pull-up resistor. If you didn't add an external one, change:
pinMode(buttonPin, INPUT);
to
pinMode(buttonPin, INPUT_PULLUP);

Hi Jonh,

Nop i added one to it.

Just made this change to the code
but now the code runs only once

and the button read is somehow erratic.

#include <Stepper.h>

// Declare the used pins
int dirA = 4;
int dirB = 5;
int pwmA = 2;
int pwmB = 3;

int switchState;
long randNumber;
const int buttonPin = 6;
int counter = 0;
byte lastPressCount;

// Declare a Stepper motor with 200 steps 
Stepper stepper1(200, dirA, dirB);

void setup() {
    
  // PWM pins require declaration when used as Digital
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  
  // Set PWM pins as always HIGH
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  Serial.begin(9600);
  
  //Button1
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
  
  // Set stepper motor speed
  //stepper1.setSpeed(3000);

}
void loop(){
 
  randNumber = random(3000, 7000);
  //Serial.println(randNumber); 

  int buttonState;
  buttonState = digitalRead(buttonPin);

 if (buttonState == LOW)
  {
    counter++;
    delay(250);
  }

 if (counter == 4) counter = 0; 
 if (lastPressCount != counter){ 
    Serial.print ("Button press count = ");          // out to serial
    Serial.println(counter, DEC);
 if (counter == 1 or counter == 2 or counter == 3){
      // Number of steps for the motors
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
      // Seconds to wait
    delay(10);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
    delay(10);
    lastPressCount = counter;
  } 
  else 
  {
    stepper1.step(0);
  }
   //lastPressCount = counter;
 } 
}