# step counting question for stepper.h

Ok. so i've been testing this program for a step counter and ran into something that doesn't make sense, and it's not the first time i've seen this. my stepper driver is set to 6400 microsteps per rev. In the section of frontStep() i can say i want the stepper to step 640.... 1/10 of a revolution, and it works fine. But when i frontStep 640, and then use the backStep() to count the steps one step at a time to the starting point. it shows there are 2560 steps.... so it's multiplying the steps by 4.

it doesn't matter if i change frontStep() to 100; the individual step counter shows 400. so no matter how many steps i take at one time, the single step counter multiplies it by 4. And i don't know why. I tried changing the steps per revolution in the setup at the top, but the end result still multiplies it by 4.

any idea on why it does that? earlier when i first started playing with the stepper.h library it was doing the same thing and when normally one rotation should be 6400, it takes 25,600 steps to do 1 rev.

thanks

``````/// tooth profile setup for counting steps

//#include <AccelStepper.h>
//#include <MultiStepper.h>

#include <Stepper.h>

Stepper stepper1(6400, 4, 3);

//AccelStepper stepper1(AccelStepper::DRIVER, 4, 3);
//AccelStepper stepper2(AccelStepper::DRIVER, 12, 11);

// MultiStepper steppers;

const int dirPin  = 3;        // stepper1 direction pin
const int stepPin = 4;      // stepper1 step pin
const int enPin = 5;        // stepper1 enable pin
const int enPin2 =13;         // stepper2 enable pin

int home_Switch = 10;          // homing switch
const byte back_Switch = 7;     // backStep switch
const byte cutting_Switch = 8;   // cutting switch
const byte front_Switch = 9;    // frontStep switch

byte switch1State;              // cutting switch state
byte switch2State;             // frontStep switch state
byte switch3State;            // homeSwitch state
byte switch4State;           // backStep state

byte lastSwitch2State=HIGH;
byte lastSwitch4State=HIGH;

int toothStep=640 ;    // steps to next tooth

int toothCounter=0;
int lastToothCounter=0;

int stepCount = 0;

void setup() {

Serial.begin(9600);

pinMode(enPin, OUTPUT);                // stepper1 enable output
pinMode(enPin2, OUTPUT);               // stepper2 enable output
pinMode( home_Switch, INPUT_PULLUP);   // home switch mode
pinMode( cutting_Switch, INPUT_PULLUP);     // cutting switch mode
pinMode( front_Switch, INPUT_PULLUP);     // frontStep switch mode
pinMode( back_Switch, INPUT_PULLUP);  // backStep switch mode
digitalWrite(enPin, HIGH);             // stepper1 enable = high
digitalWrite(enPin2, HIGH);            // stepper2 enable = high

//stepper1.setMaxSpeed(1000);          // stepper1 maxSpeed
// stepper2.setMaxSpeed(1000);            // stepper2 maxSpeed

// homing code
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(100);               // homing speed
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}

Serial.println("You are at home");      //letting me know you are at home
delay(1000);        // delay after homing before able to push any buttons

//    Serial.println(" ");
//    Serial.println(" How many steps is the tooth?");
//    while (Serial.available()==0) {   }
//     toothStep = Serial.parseInt();

}

void loop() {

cutting();                   // cutting profile
frontStep();                 // step tooth forward
backStep();                  // step tooth backward
//counter();                   // tooth counter
}

void checkButtons() {

}

void cutting() {

if (switch1State == LOW){              // if cutting_Switch activated

stepper1.step(1);
stepCount++;
delay(5);
}
Serial.print("Steps: ");
Serial.println(stepCount);
}
}

void frontStep(){

if (switch2State == LOW){                           // if front_Switch button activated

for(int x = 0; x <= toothStep; x++) {           //  go forward to next tooth

digitalWrite(dirPin,LOW);
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}

}

}

void backStep(){

if (switch4State==LOW)  {        // if back_Switch button activated

stepper1.step(-1);
stepCount--;
delay(5);
}
Serial.print("backSteps: ");
Serial.println(stepCount);
}

}

void counter(){

if (switch2State != lastSwitch2State){

if (toothCounter < 100)                // Total number of teeth
toothCounter += 1;                 // add 1 to total
else
toothCounter = 0;

Serial.print(" Tooth # ");
Serial.println(toothCounter);
}

switch2State = lastSwitch2State;

if (switch4State != lastSwitch4State){

if (toothCounter < 100)                // Total number of teeth
toothCounter -= 1;                 // subtract 1 from total
else
toothCounter = 0;

Serial.print(" Tooth # ");
Serial.println(toothCounter);
}

switch4State = lastSwitch4State;

}
``````

(deleted)

my stepper driver is set to 1/32 microstepping, which is 6400 steps per rev

I note that you are generating pulses directly using digitalWrite() in frontStep(), but then you're using the Stepper library in backStep(). I'd guess that if you use the same method in both functions, you'd get the same step count.

The Stepper library is not intended to be used with a step/direction type of stepper driver like the one you're using. It is intended for this sort of usage:

That it works at all for your application is purely by chance.

If you want a library for your application, they are available. I see you had used AccelStepper at one time, which is suitable.