Debugging millis code stepper motor

Hey, I am doing a project where I am controlling a rollerblind using a pushbutton, I am using the stepper motor 28byj-48 with the ULN2003 driver.

The first code is just running motor from UPpos to DownPos when a button is pressed and back when pressed again.
Working code I have done is

#include <AccelStepper.h>

#define UPpos 0
#define DOWNpos 3000
#define FULLSTEP 4
const int buttonPin = 2; 
int buttonState = 0; 
const int interval = 100;

//  IN1-IN3-IN2-IN4
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);

void setup() {

  myStepper.setMaxSpeed(1000.0);
  myStepper.setAcceleration(150.0);
  myStepper.setSpeed(200);
  myStepper.setCurrentPosition(0);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  buttonState = digitalRead(buttonPin);
 
 
 stepper1pos();
 stepper2pos();
 myStepper.run(); 
 
}

void stepper1pos()
{
   if (buttonState == LOW && (myStepper.currentPosition() == UPpos))
 
    myStepper.moveTo(DOWNpos);

    else if (interval >= 100 && myStepper.currentPosition() == DOWNpos)
    myStepper.disableOutputs();
    
  }

void stepper2pos()
{
  if (buttonState == LOW && (myStepper.currentPosition() == DOWNpos))
     
    myStepper.moveTo(UPpos);

    else if (interval >= 100 && myStepper.currentPosition() == UPpos)
    myStepper.disableOutputs();
    
  }

and it is working fine, How ever I want to advance it with first off all when motor is running from UPpos to DOWNpos and button is pressed while motor is running the motor should stop, and also next time I press it I want it to go back to latest position.
When this is done I would like to add some learningfunctions like saving positions to program when holding down button etc. Not so relevant atm.
What approach should I go with here and since it is going to be on for days and days shold I use something like deepsleep and waking it up everytime?
Thanks

maker-20_: How ever I want to advance it with first off all when motor is running from UPpos to DOWNpos and button is pressed while motor is running the motor should stop, and also next time I press it I want it to go back to latest position.

I'm not sure what you mean by "latest position" - but that is just a detail.

I think you need to change the style of the program. At the moment loop() calls two functions for stepper movement in each of which the button state is checked.

I reckon you need a function that checks the buttonState and decides what should happen. It should set a variable the identifies the state of the system - let's call it stepperPosition. The possible states might be 'T' (moving To Top), 'B' (moving To Bottom), 'S' (stopped), 'U' (up at Top), 'D' (down at bottom) and 'M' somewhere in the middle.

This might do the job, or at least give you some ideas.

void loop() {
    checkButton();
    moveMotor();
}
  
  
void checkButton() {
    prevButtonState = buttonState;
    buttonState = digitalRead(buttonPin);
    if (buttonState == LOW and prevButtonState == HIGH) {
        if (stepperPosition == 'B' or stepperPosition == 'T') { // going to top or to bottom
            myStepper.stop();
            systemState = 'S';
        }
        else if (stepperPosition == 'U' or stepperPosition == 'M') {
            myStepper.moveTo(DOWNpos);
            stepperPosition = 'B';
        }
        else if (stepperPosition == 'D') {
            myStepper.moveTo(UPpos);
            stepperPosition = 'T';
        }
    }
}

void moveMotor() {
    myStepper.run();
    if (myStepper.distanceToGo() == 0) {
        if (myStepper.currentPosition() == DOWNpos) {
            stepperPosition = 'D';
        }
        else if (myStepper.currentPosition() == UPpos) {
            stepperPosition = 'U';
        }
        else {
            stepperPosition = 'M'; // somewhere in the middle
        }
    }
}

...R

Robin2: I'm not sure what you mean by "latest position" - but that is just a detail.

I think you need to change the style of the program. At the moment loop() calls two functions for stepper movement in each of which the button state is checked.

I reckon you need a function that checks the buttonState and decides what should happen. It should set a variable the identifies the state of the system - let's call it stepperPosition. The possible states might be 'T' (moving To Top), 'B' (moving To Bottom), 'S' (stopped), 'U' (up at Top), 'D' (down at bottom) and 'M' somewhere in the middle.

This might do the job, or at least give you some ideas.

void loop() {
    checkButton();
    moveMotor();
}
  
  
void checkButton() {
    prevButtonState = buttonState;
    buttonState = digitalRead(buttonPin);
    if (buttonState == LOW and prevButtonState == HIGH) {
        if (stepperPosition == 'B' or stepperPosition == 'T') { // going to top or to bottom
            myStepper.stop();
            systemState = 'S';
        }
        else if (stepperPosition == 'U' or stepperPosition == 'M') {
            myStepper.moveTo(DOWNpos);
            stepperPosition = 'B';
        }
        else if (stepperPosition == 'D') {
            myStepper.moveTo(UPpos);
            stepperPosition = 'T';
        }
    }
}

void moveMotor() {    myStepper.run();    if (myStepper.distanceToGo() == 0) {        if (myStepper.currentPosition() == DOWNpos) {            stepperPosition = 'D';        }        else if (myStepper.currentPosition() == UPpos) {            stepperPosition = 'U';        }        else {            stepperPosition = 'M'; // somewhere in the middle        }    } }




...R

Thank you Robin! It was something like this I was thinking!

Okey so I used @Robin2 s approach and now got this

#include <AccelStepper.h>

#define UPpos 0
#define DOWNpos 5000
#define FULLSTEP 4
int systemState;
int stepperPosition;
const int buttonPin = 2;  
byte buttonState;
byte prevButtonState;
//const int interval = 100;

int latest;


//  IN1-IN3-IN2-IN4
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);

void setup() {

  myStepper.setMaxSpeed(800.0);
  myStepper.setAcceleration(300.0);
  myStepper.setSpeed(200);
  myStepper.setCurrentPosition(0);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
    checkButton();
    moveMotor();
}
 
void checkButton() {
    prevButtonState = buttonState;
    
    buttonState = digitalRead(buttonPin);
    
    if (buttonState == LOW and prevButtonState == HIGH) {
        if (stepperPosition == 'B' or stepperPosition == 'T') { // going to top or to bottom
            myStepper.stop();
            systemState = 'S';
        }
        else if (stepperPosition == 'U') {
            myStepper.moveTo(DOWNpos);
            stepperPosition = 'B';
            latest = UPpos;
        }
        else if (stepperPosition == 'D') {
            myStepper.moveTo(UPpos);
            stepperPosition = 'T';
            latest = DOWNpos;
        }
        else if (stepperPosition == 'M'){
          myStepper.moveTo(latest);
        }
    }
}

void moveMotor() {
    myStepper.run();
    if (myStepper.distanceToGo() == UPpos) {
        if (myStepper.currentPosition() == DOWNpos) {
            stepperPosition = 'D';
            myStepper.disableOutputs();
            
        }
        else if (myStepper.currentPosition() == UPpos) {
            stepperPosition = 'U';
            myStepper.disableOutputs();           
        }
        else {
            stepperPosition = 'M'; // somewhere in the middle
            myStepper.disableOutputs();
        }
    }
}

This code is working great, I would like to add a learning program for it where I can hold down the button 5 sec and it goes into programming/learning mode. Assuming the rollerblind is at the highest point and you hold down the button until it reaches the DOWNpos. Maybe saving this to the arduinos memory so that the device can be turned off and still know the latest pos and also the range that it should be. Is this possible?

maker-20_: This code is working great, I would like to add a learning program for it where I can hold down the button 5 sec and it goes into programming/learning mode. Assuming the rollerblind is at the highest point and you hold down the button until it reaches the DOWNpos. Maybe saving this to the arduinos memory so that the device can be turned off and still know the latest pos and also the range that it should be. Is this possible?

I would strongly recommend a separate button (or better still a switch) to select programming mode.

You could save values to the Arduino's EEPROM memory.

You must also keep in mind that the Arduino has no idea where the stepper motor is when the program starts. The usual thing is to have a limit switch and move the motor until it triggers the limit switch to identify the ZERO or HOME position. The simplest thing might be to have limit switches at the top and the bottom.

...R