trying to make an increment counter for push buttons

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);
  }
}

if (switch3State=HIGH)

No No No = is not the same as ==

larryd:
if (switch3State=HIGH)

No No No = is not the same as ==

Yes, i know those mean different things, but i believe my application of the = in that line is correct. That is an if statement saying that if (switch3State is HIGH), print me a line saying that i'm at home. if i change that command to ==, it doesn't work

There are two switches that controls the movement, forward and back, and both of them take an equal amount of steps. for example, there are 100 button presses to equal one rotation. and i would like to go back and forth as needed and the counter would tell me which of the 100 stops i'm stopped at.

i believe my application of the = in that line is correct

The reference for the if structure seems to disagree.

This is because C++ evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the (assignment operator)), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

After looking at it. it probably needs to be if(switch3State==LOW) instead of =HIGH. but i'll check that tomorrow.

so any ideas on the counter at the bottom?

I guess it turns out that i don't even need that IF statement where there was an error. So i went ahead and deleted it from the code above since. it already does the Serial.println the after it jumps out of the while loop when it reaches home

I suppose i can call this thread closed. I was able to get my counter all fixed, and it all works wonderfully.

below is my up to date code in case someone else needs part of it for reference, or finds any glaring mistakes let me know.

#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

   byte lastSwitch2State=HIGH;
   byte lastSwitch4State=HIGH;
      
   const int toothStep = 640;    // steps to next tooth
   
   int toothCounter=0;
   int lastToothCounter=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

  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
  counter();                   // tooth counter
}

void checkButtons() {
  
  switch1State = digitalRead(cutting_Switch);   // reading cutting button
  switch2State = digitalRead(front_Switch);  // reading frontStep button
  switch3State = digitalRead(home_Switch);  //reading home  button
  switch4State = digitalRead(back_Switch);  //reading backStep button
}

void cutting() {
       
  if (switch1State == LOW){              // if cutting_Switch 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 front_Switch button activated

     for(int x = 0; x <= toothStep; x++) {           //  go forward to next tooth
        
        digitalWrite(dirPin,LOW);
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      }  
   }
}

void backStep(){

   if (switch4State==LOW && switch3State==HIGH)  {        // if back_Switch button activated

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

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

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;
    
}