Hi. I'm writing this program below. I'm using stepper motors to do certain tasks via pushbutton. and i would like to keep track on where i'm at each time i press a button. My full code is below, and at the very bottom is where i'm having issues, and i have the serial monitor open to show you what's it's doing. When i start out pushing the button it keeps track. but when i press the backward button, it doesn't automatically subtract from where i was at. it copies the last number and then starts subtracting.
any ideas of what i'm doing wrong?
#include <AccelStepper.h>
#include <MultiStepper.h>
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
const int toothStep = 640; // steps to next tooth
int toothCount= 1; //var for tooth counter
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
steppers.addStepper(stepper1); // adding stepper1 to multiStepper
steppers.addStepper(stepper2); // adding stepper2 to multiStepper
while (digitalRead(home_Switch)) {
// 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(2000); // delay after homing before able to push any buttons
}
void loop() {
checkButtons(); // read buttons status
cutting(); // cutting profile
frontStep(); // step tooth forward
backStep(); // step tooth backward
toothCounter(); // tooth counter
}
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() {
if (switch1State == LOW){ // if cutting button activated
long positions[2]; // Run these positions
positions[0] = 0;
positions[1] = 0;
steppers.moveTo(positions);
steppers.runSpeedToPosition();
delay(2000);
positions[0] = -1000;
positions[1] = 0;
steppers.moveTo(positions);
steppers.runSpeedToPosition();
delay(2500);
positions[0] = 0;
positions[1] = 1500;
steppers.moveTo(positions);
steppers.runSpeedToPosition();
delay(2500);
positions[0] = 3000;
positions[1] = 0;
steppers.moveTo(positions);
steppers.runSpeedToPosition();
delay(2500);
positions[0] = 0;
positions[1] = 0;
steppers.moveTo(positions);
steppers.runSpeedToPosition();
delay(2500);
}
}
void frontStep(){
if (switch2State == LOW){ // if stepDown button activated
for(int x = 0; x <= toothStep; x++) { // change for tooth spacing
digitalWrite(dirPin,LOW);
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
}
void backStep(){
if (switch4State==LOW && switch3State==HIGH) { // if backStep button activated
for(int x = 0; x <= toothStep; x++) { // change for tooth spacing
digitalWrite(dirPin,HIGH);
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
}
void toothCounter(){
if (switch2State==LOW){
Serial.print("steps: ");
Serial.println(toothCount);
toothCount++;
delay(1);
}
if (switch4State==LOW){
Serial.print("Tooth # ");
toothCount--;
Serial.println(toothCount);
delay(1);
}
}