if (staplercounter >= 4); { //keep checking till it hits 4stapler.stop();}
if (staplercounter >= 4); //does not actually do anything if the condition is true, because of the ';' at the end
stapler.stop(); //is executed regardless of the if statement above.Hi Gregor,I have been using the accelerate/deccelarate the past few weeks and was converting over to constant speed to try to increase my speed that's why stop() is in there, it worked so I ran with it.I totally missed the semi-colon and have taken it out, must have been from all the copy paste I've been doing.I will add more code for you, I was trying to trim it down because I have 4 steppers and was just trying to make 1 work and apply to other motors. I"m basically turning on the motor and when I pass by the Hall Effect sensor 4 times then everything will stop and go back to their original positions.here is code for sensor and motor with changes recommended by you and I get movement from the motor but it ignores the setSpeed(0) to stop it.
#include <AccelStepper.h>AccelStepper stapler(AccelStepper::DRIVER, 3, 4);
//VALUES FOR BUTTONconst int button = 2; // pin button is onint val = LOW; // current button stateint old_val = LOW;int buttonstate = LOW;unsigned long previousMillis = LOW;//VALUES FOR TURNING STAPLER 4 TURNS(using stapler hall Sensor)int staplercounter = 0;int staplercurrentState = 0;int staplerpreviousState = 0;int staplerSensor = 47;void setup() {pinMode(button, INPUT_PULLUP);pinMode(staplerSensor, INPUT_PULLUP);Serial.begin(9600);}void loop() {readButton();staplefour();}void readButton() {unsigned long currentMillis = millis();if (currentMillis - previousMillis >= 100) {val = digitalRead(button);if ((val == LOW) && (old_val == HIGH)) {buttonstate = HIGH;}previousMillis = currentMillis;old_val = val;Serial.println(buttonstate);}}void staplefour() {stapler.setMaxSpeed(10000.0);
staplercurrentState = digitalRead(staplerSensor); //set state for movementif (buttonstate == HIGH && staplercounter <= 4) {
stapler.setSpeed(-5000);
if (staplercounter >= 4) { //keep checking till it hits 4
stapler.setSpeed(0);
}}if (staplercurrentState != staplerpreviousState) {if (staplercurrentState == 1) {staplercounter = staplercounter + 1;Serial.println(staplercounter);}}staplerpreviousState = staplercurrentState;if (staplercounter >= 4) {buttonstate = LOW; //reset buttonstaplercounter = 0; //reset counter for next button pushstaplercurrentState = 0; //reset state for next button push}
}
if (buttonstate == HIGH && staplercounter <= 4) { stapler.setSpeed(-5000); if (staplercounter >= 4) { //keep checking till it hits 4 stapler.setSpeed(0); } } if (staplercounter >= 4) { buttonstate = LOW; //reset button staplercounter = 0; //reset counter for next button push staplercurrentState = 0; //reset state for next button push }Hi,in this codestapler.setSpeed(0); is only called if staplercounter is exactly 4. however, staplercounter will be reset to 0 once it reaches 4 by this code:if (buttonstate == HIGH && staplercounter <= 4) {stapler.setSpeed(-5000);if (staplercounter >= 4) { //keep checking till it hits 4stapler.setSpeed(0);}}therefore stapler.setSpeed(0) will never be reached.if (staplercounter >= 4) {buttonstate = LOW; //reset buttonstaplercounter = 0; //reset counter for next button pushstaplercurrentState = 0; //reset state for next button push}
Hi gregor,I'm sill having a problem with this part of the code, it worked as written with the access/decell code going by the hall sensor 4 times and then stopping and resetting the sensor and the button and waiting for the next button press.I don't know why it doesn't operate the same way with the constant speed code, if I change the staplercounter to 3 then the button and hall sensor don't reset and if I change both to 3 then I end up with the same situation of the motor not stopping. The code as written say's greater than or equal to 4 not exactly 4 or am I misunderstanding what that means, it works for one code but not the other and not sure why.WORKING CODEif (buttonstate == 1) {
stapler.move(400);
if (hallcounter >= 4) { //keep checking till it hits 4
stapler.stop();and thenif (hallcounter >= 4) {
buttonstate = LOW;
hallcounter = 0;
hallcurrentState = 0;
}
NOW WORKING:
}
if (hallcurrentState != hallpreviousState) {
if (hallcurrentState == 1) {
hallcounter = hallcounter + 1;
Serial.println(hallcounter);
}
}
hallpreviousState = hallcurrentState;
if (hallcounter >= 4) {
buttonstate = LOW; //reset button
hallcounter = 0; //reset counter for next button push
hallcurrentState = 0; //reset state for next button push
}
if (staplercounter >= 4) { gantry.setSpeed(1000); gantry.move(-200); }each time the program executes this code (which can happen several thousand times / second) the target position is shifted by -200 steps.
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(staplerSensor, INPUT_PULLUP);
pinMode(gantrysensor, INPUT_PULLUP);
pinMode(carriagesensor, INPUT_PULLUP);
Serial.begin(9600);
gantry.setMaxSpeed(10000.0);
carriage.setMaxSpeed(10000.0);
stapler.setMaxSpeed(10000.0);
roserotate.setMaxSpeed(1000); //rotate substrate at intervals
gantry.setMaxSpeed(10000.0);
carriage.setMaxSpeed(10000.0);
}
void loop() {
starter();
counter();
gantryhome = digitalRead(gantrysensor);
carriagehome = digitalRead(carriagesensor);
staplercurrentState = digitalRead(staplerSensor); //set state for movement
if (buttonstate == HIGH) {
if (currentMillis - previousMillis3 > pause) { //short pause for slider & gantry move
previousMillis3 = currentMillis;
stapler.setSpeed(-5000);
}
if (gantryhome == HIGH && staplercounter < 3) {
gantry.move(50); //move to sensor for stapling
gantry.setSpeed(1000);
}
if (carriagehome == HIGH && staplercounter < 3) {
carriage.move(-50); //move to sensor for stapling
carriage.setSpeed(2000);
}
if (staplercounter >= 1) {
if (currentMillis - previousMillis2 >= interval) {
previousMillis2 = currentMillis;
roserotate.move(50);
roserotate.setSpeed(800);
if (staplercounter >= 3) {
roserotate.setSpeed(0);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
if (staplercounter >= 4) { //Slide gantry back to start position
stapler.setSpeed(0); //turn motor off
gantry.moveTo(-200);
gantry.setSpeed(1000);
carriage.moveTo(400);
carriage.setSpeed(2000);
buttonstate = LOW; //reset button
staplercounter = 0; //reset counter for next button push
staplercurrentState = 0; //reset state for next button push
}
stapler.runSpeed();
gantry.runSpeedToPosition();
carriage.runSpeedToPosition();
roserotate.runSpeedToPosition();
}Thanks gregor,That is a bit easier to read, one question on the setMaxSpeed in the setup(), do I need to specify the gantry and the carriage speeds twice since they are the same speed?
boolean AccelStepper::runSpeedToPosition(){ if (_targetPos == _currentPos) //if the stepper is already at the target position return false; //do nothing if (_targetPos >_currentPos) //else, set the movement direction accordingly _direction = DIRECTION_CW; else _direction = DIRECTION_CCW; return runSpeed(); //and take a step if a step is due. does nothing if a step is not due.}