So here is a code-version that uses the MobaTools-library
for functionalities like yours a state-machine is very good.
a state-machine reduces the number if -if-conditions substantially
Once you have understood how state-machines work you will never go back
to not use them.
In Your case the state-machine has 3 states:
here are the self-explaining names:
// constants used in the state-machine
const byte waitForButtonStart = 0;
const byte StartStepper = 1;
const byte waitForStepperToFinish = 2;
I added serial-output that shows what is going on in the code
I use two macros for that (which are at the top of the code)
At first you can ignore the macros "dbg" and "dgbi"
The more interesting thing is the state-machine itself
the state-machine starts with state waitForButtonStart
and then proceeds as conditions become TRUE
Maybe you have to adapt the speed.
As the MobaTools create the step-pulses "in the backround" other code can execute even while the stepper is running.
I have writen quite a lot of comments to explain
Have fun analysing how it works
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
#include <MobaTools.h>
#define STEPS_per_rotation 2038 // (28BYJ-48)
MoToStepper stepper(STEPS_per_rotation, FULLSTEP); // HALFSTEP ist default
const byte myButton_pin = 7;
const byte myBtnPinArray[] = {myButton_pin};
const byte NrOfButtons = sizeof(myBtnPinArray);
MoToButtons myButtons( myBtnPinArray, NrOfButtons, 50, 500 );
// constants used in the state-machine
const byte waitForButtonStart = 0;
const byte StartStepper = 1;
const byte waitForStepperToFinish = 2;
byte myActualState = waitForButtonStart;
void myStateMachine() {
myButtons.processButtons();
switch (myActualState) {
case waitForButtonStart:
if (myButtons.pressed(0)) { // just a single button index-numbers start at 0
Serial.println("Buttonpressed");
myActualState = StartStepper;
}
break;
case StartStepper:
dbg("right before delay(1000);", 0);
delay(1000);
dbg("start half turn stepper.write(180)", 0);
stepper.write(180); // function write uses angle in degree 180 degree = 1/2 rotation
myActualState = waitForStepperToFinish;
break;
case waitForStepperToFinish:
dbgi("stepper running", stepper.stepsToDo(), 250);
if ( stepper.stepsToDo() == 0 ) {
// set new position as new zero-point. Otherwise MobaTools knows
// that stepper already is at 180 degrees and would not rotate
stepper.setZero();
myActualState = waitForButtonStart;
dbg("stepper finished", stepper.stepsToDo());
Serial.println( F("Press button to start stepper run\n\n") );
}
}
}
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
stepper.attach(8, 10, 9, 11); // four parameters for 4-pin-stepper-drivers
stepper.setSpeed(200); // must be set as real rpm * 10 rpm = 4 parameter = 4 * 10 = 40
stepper.setZero();
Serial.println( F("Press button to start stepper run") );
}
void loop() {
myStateMachine();
}
best regards Stefan