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