How to control stepper motor's steps with Arduino Uno ?

I use Arduino Uno with that code to control steps of a stepper motor (4 wires), but the motor moves only one step in clockwise direction and the second step in counterclockwise and so on ( 1 clockwise and 1 counterclockwise ). I don't know what's wrong with the code. I need your Help

int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int count;
int steps =20;
int delayTime = 1000;

int p1 ;
int p2; 
int p3;
int p4 ;
void setup()
{
  Serial.begin(9600);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  count=0;

}

void loop() {
  
  
  
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  
  p1=digitalRead(motorPin1);
  p2=digitalRead(motorPin2);
  p3=digitalRead(motorPin3);
  p4=digitalRead(motorPin4);
  Serial.println(p1);
  Serial.println(p2);
  Serial.println(p3);
  Serial.println(p4);
  Serial.println("----");
  
  
  count++;
  if(count==steps)
  {
  while(1)
  {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  }
  }
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
    count++;

  p1=digitalRead(motorPin1);
  p2=digitalRead(motorPin2);
  p3=digitalRead(motorPin3);
  p4=digitalRead(motorPin4);
  Serial.println(p1);
  Serial.println(p2);
  Serial.println(p3);
  Serial.println(p4);
  Serial.println("----");
  
  if(count==steps)
  {
  while(1)
  {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  }
  }
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
    count++;

  p1=digitalRead(motorPin1);
  p2=digitalRead(motorPin2);
  p3=digitalRead(motorPin3);
  p4=digitalRead(motorPin4);
  Serial.println(p1);
  Serial.println(p2);
  Serial.println(p3);
  Serial.println(p4);
  Serial.println("----");
  
  if(count==steps)
  {
  while(1)
  {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  }
  }
  
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
    count++;
    
      p1=digitalRead(motorPin1);
  p2=digitalRead(motorPin2);
  p3=digitalRead(motorPin3);
  p4=digitalRead(motorPin4);
  Serial.println(p1);
  Serial.println(p2);
  Serial.println(p3);
  Serial.println(p4);
  Serial.println("----");
  
  if(count==steps)
  {
  while(1)
  {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  }
  }
  delay(delayTime);
  
  
}

What are you using for a motor driver and how is it wired to the Arduino? As you probably know, Arduino outputs cannot provide power to motors.

If you use the tools/autoformat menu option in the Arduino IDE it will layout your code in a more readable style.

However that's a very minor point.

Without spending a long time I can't make sense of your code. It may be best to start over.

Think about it this way ... What code do you need to get the motor to make one step?

Put that code in a function

Call that function for as many steps as you need.

Things in your code like

 count++;
  if(count==steps)
  {
  while(1)
  {

don't make any sense.

"if (count == steps)" will only work once

"while(1)" just duplicates what loop() is already doing.

...R

Hi, Sounds like your stepper needs a different wiring sequence. See some examples on the ArduinoInfo.Info WIKI HERE: See "Step Sequences".