# For function going wrong?

I have made this code to have a easy way to (half)step a stepper
The stepper does 200 steps to make a full circle

If do
stepf(200);It goes around 4 times.

When I do
stepf(50);It does a perfect circle.

Can anyone help?

int stepPin1 = 8;
int stepPin2 = 9;
int stepPin3 = 10;
int stepPin4 = 11;
int val = 10;

void setup()
{
pinMode(stepPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(stepPin4, OUTPUT);
}

void stepf(int steps)
{
for (int i=0; i < steps; i++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(val);
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(val);
}
}

void stepb(int times)
{
for (int i=0; i < times; i++)
{
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
delay(val);
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
delay(val);
}
}

void loop()
{
stepf(200);
stepb(200);
}

It looks like you’re performing eight steps per iteration of the loop. If each is a half-step, 50 * 8 / 2 == 200; 50 times 8 half-steps is one complete revolution of 200 steps.

so
for example
1000
0100
0010
0001

is 4 steps?

because i thought that was 1 step

There’s nothing syntactically wrong with the code, so let’s examine the assumptions you’re making. If indeed four operations are needed for one step, then something would be wrong with either the compiler, the arduino, or the stepper itself, to produce the bad results you are experiencing. If a single operation equates to a single step, then we could expect the behaviour you are seeing.

What leads you to believe that 200 steps are required to perform a complete circle, and that one single step is achieved by eight different operations? I’m not saying you’re incorrect, and it’s been a while since I worked with steppers at all so I may be totally out of whack, but when expectations are not met by empirical observation, it’s a good time to step back and examine one’s assumptions.

Well I know the stepper needs 200 steps for a full revolution (when using the stepper library it also does this)

but the thing is the library doesn't utilize half steps which I need (faster and smoother).

I know the right sequence for the turning and adjusting the normal library would be nice, but I have no idea how.

I guess it is just the 8 steps thing in which case I need to rethink my code :(