Logic help with code

Hi,
I have a couple stepper motors that I'm trying to make move in a coordinated fashion and can't seem to figure out the correct way to code it.
I have one stepper that just spins when I push a button and when you makes 1 revolution a hall sensor counts the revolutions. I want to move the other stepper a set amount of steps each time the other stepper makes a revolution.

If I turn the power on and pass a magnet across the hall sensor to see if it works it counts in my serial window but once I press the start button and the stapler stepper starts moving it doesn't count for some reason and then the other motor doesn't move because it is waiting for the count to start.

I'm not sure where to change my code to get it to run properly and any help would be appreciated.

I currently have the steppers operating with a millis() delay but with plans for different situations I think making it move with the count would make the code less cumbersome so I thought I would give it a try.
Here's the code I have

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 3, 4);
AccelStepper roserotate(AccelStepper::DRIVER, 12, 13);

//VALUES FOR BUTTON
const int button = 2;            // pin button is on
int val = LOW;                     // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis;

//Hall Sensor Counter
int staplercounter = 0;
int staplercurrentState = 0;
int staplerpreviousState = 0;
int staplerSensor = 47;


void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(staplerSensor, INPUT_PULLUP);

  Serial.begin(115200);

  stapler.setMaxSpeed(10000.0);
  stapler.setAcceleration(10000.0);
  roserotate.setMaxSpeed(1000);
  roserotate.setAcceleration(1000);


}

void loop() {
  starter();
  counter();

  staplercurrentState = digitalRead(staplerSensor);   //set state for movement

  if (buttonstate == HIGH) {
    stapler.setSpeed(-6000);
  }
  if (buttonstate == HIGH && staplercounter == +1) {
    roserotate.move(25);
    roserotate.setSpeed(900);
  }
  if (staplercounter >= 4 ) {         

    stapler.setSpeed(0);                      //turn stapler motor off

    buttonstate = LOW;                   //reset button
    staplercounter = 0;                    //reset counter for next button push
    staplercurrentState = 0;               //reset state for next button push

  }
  stapler.runSpeed();
  roserotate.runSpeedToPosition();
}


void starter() {
  if ( (millis() - previousMillis) >= 50) {    //state machine for button & debounce
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = HIGH;
    }
    previousMillis = millis();
    old_val = val;
  }
}
void counter() {
  staplercurrentState = digitalRead(staplerSensor);     //used to time all events
  if (staplercurrentState != staplerpreviousState) {    //check the count and add 1
    if (staplercurrentState == 1) {
      staplercounter = staplercounter + 1;
      Serial.println(staplercounter);
    }
  }
  staplerpreviousState = staplercurrentState;
}

I recommend adding some Serial.println() statements to appropriate places in your code and then running it with the Serial Monitor open so you can see what's happening.

I suspect your logic is mixed up.

The value of buttonState will remain HIGH for some time and that will cause the same thing to be repeated - for example

if (buttonstate == HIGH) {
    stapler.setSpeed(-6000);
  }

My guess is that that should only happen once.

I think you need to study the concept of a State Machine - which is a fancy name for a variable that keeps track of progress through a series of steps or states. There is a new tutorial about the concept.

...R

ribbonman:
If I turn the power on and pass a magnet across the hall sensor to see if it works it counts in my serial window but once I press the start button and the stapler stepper starts moving it doesn't count for some reason

If, instead of passing a magnet across the hall sensor, you rotate the stepper by hand, does it sense THAT magnet and count?

pert:
I recommend adding some Serial.println() statements to appropriate places in your code and then running it with the Serial Monitor open so you can see what's happening.

I will put some in, I just put it in for the counter to verify counting because I know the button works because it starts moving the motor.

Robin2:
I suspect your logic is mixed up.

The value of buttonState will remain HIGH for some time and that will cause the same thing to be repeated - for example

if (buttonstate == HIGH) {

stapler.setSpeed(-6000);
 }



My guess is that that should only happen once.

I think you need to study the concept of a State Machine - which is a fancy name for a variable that keeps track of progress through a series of steps or states. There is a [new tutorial about the concept](https://forum.arduino.cc/index.php?topic=604505.).

...R

I looked at a state machine when I was first doing the code a while back and thought it was more than I really needed to do but I will look into the post you gave in more detail, not quite sure how to incorporate what I want to do with what has been posted. It looks more like something I should be doing though.

johnwasser:
If, instead of passing a magnet across the hall sensor, you rotate the stepper by hand, does it sense THAT magnet and count?

I did so this morning and now the hall sensor is counting properly. It counts to 4 and it turns the stapler stepper off as coded which is good but it either moves the roserotate stepper continuously with the move() command or only moves once with the moveTo() command and not at each count of the hall sensor. I will have to revisit the Accelstepper library and make sure I'm using the move() commands correctly.