Below is my program. Under cutting() and backStep() the stepper motors are supposed to go 1 step forwards or backwards until i hit a button, and the stepper returns to the starting position, which are saved as “long stepCount;” I kept it simple by saying when i hit the button to stop, go in the opposite direction the same amount of steps that were taken. This is a step counter for a stock edge finder for my mill.
This works great until i reach the int limit of 32,767 of steps taken, and then instead of going in the opposite direction, it goes the same direction, but steps less than did at first. Originally i had it set as “int long stepCount” and then removed the int to see if it made a difference, but it didn’t so i’m here. with it listed as long value, i thought it would count up to 2+ million steps if wanted, but it doesn’t. I even tried a couple variations but didn’t have any luck
can someone please tell why it’s doing this? it was by accident i even found this problem. I was counting steps on a stopwatch so i could chart the speed/steps/time ratio and then ran into this issue.
thanks
#include <Stepper.h>
Stepper stepper1(6400, 4, 3);
Stepper stepper2(3200, 12, 11);
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 long toothStep; // steps to next tooth
int stepSpeed;
long stepCount;
void setup() {
Serial.begin(115200);
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
Serial.println(" ");
Serial.println(" Press Forward To Begin");
Serial.println(" ");
while (digitalRead(front_Switch)){ }
Serial.println(" Started Homing");
Serial.println(" ");
while (digitalRead(home_Switch)) { //CCW rotation
// homing code
stepper1.step(1);
delayMicroseconds(2000);
}
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.print(" How many steps is the tooth?");
while (Serial.available()==0) { }
toothStep = Serial.parseInt();
while (Serial.available() != 0 ) {Serial.read();}
Serial.print(" #");
Serial.print(toothStep);
Serial.println(" ");
Serial.println(" ");
Serial.print(" How fast are you going? Fast 1-10 or Slow: 10-200");
while (Serial.available()==0) { }
stepSpeed = Serial.parseInt();
while (Serial.available() != 0 ) {Serial.read();}
Serial.print(" #");
Serial.print(stepSpeed);
Serial.println(" ");
}
void loop() {
checkButtons(); // read buttons status
cutting(); // cutting profile
frontStep(); // step tooth forward
backStep(); // step tooth backward
speedButton();
}
void checkButtons() {
switch1State = digitalRead(cutting_Switch); // reading stepUp button
switch2State = digitalRead(front_Switch); // reading frontStep button
switch3State = digitalRead(home_Switch); //reading home_switch button
switch4State = digitalRead(back_Switch); //reading backStep button
}
void cutting() { //CW rotation step counter
if (switch1State == LOW){ // if cutting_Switch activated
while (digitalRead(home_Switch)){
stepper1.step(1);
stepCount++;
Serial.println(stepCount);
delay(stepSpeed);
}
Serial.println(" ");
Serial.print("Steps: ");
Serial.println(stepCount);
Serial.println(" ");
stepper1.setSpeed(50);
stepper1.step(-stepCount);
delay(500);
stepCount=0;
}
}
void frontStep(){ // center of next tooth profile
if (switch2State == LOW){ // if front_Switch button activated
stepper1.setSpeed(50);
stepper1.step(toothStep);
stepCount=0;
}
}
void backStep(){ //CCW rotation step counting
if (switch4State==LOW) { // if back_Switch button activated
while (digitalRead(home_Switch)){
stepper1.step(-1);
stepCount--;
Serial.println(stepCount);
delay(stepSpeed);
}
Serial.println(" ");
Serial.print("backSteps: ");
Serial.println(stepCount);
Serial.println(" ");
stepper1.setSpeed(50);
stepper1.step(-stepCount);
delay(500);
stepCount=0;
}
}
void speedButton(){
if (switch3State==LOW && switch4State==LOW){
Serial.println(" ");
Serial.print(" Manual Speed Change; 1-200");
while (Serial.available()==0) { }
stepSpeed = Serial.parseInt();
while (Serial.available() != 0 ) {Serial.read();}
Serial.print(" #");
Serial.print(stepSpeed);
Serial.println(" ");
}
}