Hey all!
I am trying to advance my stepper code and using switch states. The motor goes on when the button is pressed but it does not really move to the pos that I want it to. What can be wrong in this code?
#include <AccelStepper.h>
#define FULLSTEP 4
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int stateS = false;
const int interval = 100;
// IN1-IN3-IN2-IN4
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);
void setup() {
Serial.begin(9600);
myStepper.setMaxSpeed(1000.0);
myStepper.setAcceleration(150.0);
myStepper.setSpeed(200);
myStepper.setCurrentPosition(0);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
switchMachine();
stateS = true;
myStepper.run();
}
lastButtonState = buttonState;
}
}
//Switch Machine
void switchMachine()
{
switch (stateS) {
case 0:
if (myStepper.currentPosition() == 0){
myStepper.moveTo(3000);
Serial.println("Motor moving");
}
break;
case 1:
if (myStepper.currentPosition() == 3000){
myStepper.moveTo(0);
Serial.println("Motor moving back");
break;
case 2:
break;
case 3:
break;
}
}
Once stateS is made true it s never changed.
You have designated stateS as your state and you even have cases 0 -3; however, you are only setting stateS to true or false. Technically, false is 0 and true is non-zero. You need to use 0, 1, 2, or 3 for stateS.
Also, as groundFungus mentioned you set stateS to true and then never set it to anything else.
You are only calling switchMachine() and stepper.run() within a button state change block.
If you want to make a start button, use it to set the first state, or else use a boolean control variable to enable the the switchMachine.
Separate the switch case and stepper control from the button code.
cattledog:
You are only calling switchMachine() and stepper.run() within a button state change block.
If you want to make a start button, use it to set the first state, or else use a boolean control variable to enable the the switchMachine.
Separate the switch case and stepper control from the button code.
Sounds good! Could you specify how startswitch could look like. The only thing I find is buttonstate like I am using right now.
#include <AccelStepper.h>
#define FULLSTEP 4
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int stateS = 0;
const int interval = 100;
// IN1-IN3-IN2-IN4
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);
void setup() {
Serial.begin(9600);
myStepper.setMaxSpeed(1000.0);
myStepper.setAcceleration(150.0);
myStepper.setSpeed(200);
myStepper.setCurrentPosition(0);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if ( buttonState != lastButtonState )
{
if ( buttonState == HIGH )
{
switchMachine();
myStepper.run();
stateS = 1;
}
lastButtonState = buttonState;
stateS = 0;
}
}
//Switch Machine
void switchMachine()
{
switch (stateS) {
case 0: stepperPos1();
break;
case 1: stepperPos2();
break;
}
}
void stepperPos1(){
if (myStepper.currentPosition() == 0){
myStepper.moveTo(3000);
Serial.println("Motor moving");
}
}
void stepperPos2(){
if (myStepper.currentPosition() == 3000){
myStepper.moveTo(0);
Serial.println("Motor moving");
}
}
aarg:
stateS = 0;
You made it confusing by using numbers instead of variable names for the states. Especially because you don't have any internal documentation (comments). It forces us to decode the entire program just to understand one small part.