Need help with AccelStepper.h code. Simple elevator with up and down buttons

This is my first post. If I'm in the wrong place just tell me where to post.

The following code runs CW 2 turns and CCW 2 turns forever.
I'm trying to add Up and Down buttons for a simple elevator. I'm sure its simple if you know the code.
I know electronics but I don't know much about programming. I have commented out my attempt at adding the code for the buttons. I can make them work but not like they should. Any help on this issue would be great.

#include <AccelStepper.h>

#define HALFSTEP 8

#define motorPin1 8 // IN1 on ULN2003 ==> Blue on 28BYJ-48
#define motorPin2 9 // IN2 on ULN2004 ==> Pink on 28BYJ-48
#define motorPin3 10 // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4 11 // IN4 on ULN2003 ==> Orange on 28BYJ-48

const int Down_Button = 2; //set up our button constants
const int Up_Button = 3; //set up our button constants
const int Up_Limit = 4;
const int Down_Limit = 5;
const int DownLED = 12;
const int UpLED = 13;
int endPoint = 8192; // Move this many steps -
//1024 = approx 1/4 turn
//8192 = 2 turns

// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup()
{
Serial.begin(9600);
Serial.println(stepper1.currentPosition());
delay(5000);
pinMode(Down_Button, INPUT); //set up I/O
pinMode(Up_Button, INPUT); //set up I/O
pinMode(DownLED, OUTPUT); //setup our Down LED
pinMode(UpLED, OUTPUT); //setup our Up LED
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);

}

void loop()
{
// if (digitalRead(Down_Button) == LOW) // Lets go down
stepper1.moveTo(endPoint);

// if (stepper1.distanceToGo() == 0 && digitalRead(Up_Button) == LOW) // Lets go up
if (stepper1.distanceToGo() == 0)
{
Serial.println(stepper1.currentPosition());
stepper1.setCurrentPosition(0);
endPoint = -endPoint;
stepper1.moveTo(endPoint);
Serial.println(stepper1.currentPosition());
}
stepper1.run();
}

Here is the same code in code tags - so it is easy to read and copy

#include <AccelStepper.h>  
  
#define HALFSTEP 8  
  
#define motorPin1  8     // IN1 on ULN2003 ==> Blue   on 28BYJ-48  
#define motorPin2  9     // IN2 on ULN2004 ==> Pink   on 28BYJ-48  
#define motorPin3  10    // IN3 on ULN2003 ==> Yellow on 28BYJ-48  
#define motorPin4  11    // IN4 on ULN2003 ==> Orange on 28BYJ-48  
  
const int Down_Button = 2; //set up our button constants
const int Up_Button = 3; //set up our button constants
const int Up_Limit = 4;
const int Down_Limit = 5;
const int DownLED = 12;
const int UpLED = 13;
int endPoint = 8192;        // Move this many steps - 
//1024 = approx 1/4 turn 
//8192 = 2 turns
  
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48  
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);  
  
void setup()  
{ 
  Serial.begin(9600);  
  Serial.println(stepper1.currentPosition());  
  delay(5000);  
  pinMode(Down_Button, INPUT); //set up I/O
  pinMode(Up_Button, INPUT); //set up I/O
  pinMode(DownLED, OUTPUT); //setup our Down LED
  pinMode(UpLED, OUTPUT); //setup our Up LED 
  stepper1.setMaxSpeed(1000.0);  
  stepper1.setAcceleration(100.0);  
  stepper1.setSpeed(200);  
   
}  
  
void loop()  
{  
   // if (digitalRead(Down_Button) == LOW) // Lets go down
stepper1.moveTo(endPoint); 
   
   // if (stepper1.distanceToGo() == 0 && digitalRead(Up_Button) == LOW) // Lets go up
    if (stepper1.distanceToGo() == 0)
   {  
     Serial.println(stepper1.currentPosition());  
     stepper1.setCurrentPosition(0);  
     endPoint = -endPoint;  
     stepper1.moveTo(endPoint);  
     Serial.println(stepper1.currentPosition());  
   }  
    stepper1.run();  
}

code runs CW 2 turns and CCW 2 turns forever.

That's what the program tells it to do.

You should reorganize your code so loop() is like this

void loop() {
   readButtons();
   updateEndPoint();
   moveMotor();
}

and put most of the code in the moveMotor() function. However it will only move to the end-point. The code in the function updateEndPoint() will change the destination for the motor when appropriate - i.e. after the appropriate button has been pressed.

See planning and implementing a program

...R

Thanks for the swift reply. I've been working with electronics since 1976 and computers since the early 80's. I crossed over into the programming world.... well last week. I understand what you're saying about the loop thing a little. I can get it to go where I want it to go when I press the down button and where I want it to go when I press the up button. But... here comes the but. But when I press the down button again it goes twice as far. Oh yea! The part you mentioned about "planning and implementing a program" is the answer. I am working on that. Lots of great information there. I still don't understand the placement of the little {} things but just one of them outta place can cause a major head ache. I'm a geocacher and I want to use these ardurinos for some fancy gadget caches. For starters I want to used the down button to lower the cache and use a servo motor to open the door to the cache. Then, when they replace the cache I want the servo motor to close the door and the elevator to raise the cache up. I have the code working for a keypad to control my servo, and I can make it work with the stepper.h lib. I really like the
AccelSteper.h because it will start off slow, speed up and then slow down just like a real elevator.Anyway, thanks for your help and advice. This is an adventure.
Hollis Morrow

I find it hard to detect a specific question in your rather rambling Reply #2

You need to use the concept of states. States are really just variables with values that record the state of the system.

When you press a button it should set the state of where you want to go. There should be another variable that records the state of where you are. If the state you want to go to is the same as the state of where you actually are there will be no need to move.

The concept of states is all through planning and implementing without being explicitly noted.

...R

Don't call setCurrentPosition, that's fouling up your knowledge of where the motor is.
I think (I'm guessing) that you are trying to do relative moves by changing the motor's
home position with setCurrentPosition and negating endPoint. That's just confused and
messy.

An endPoint is an end point. If you want two end points then declare two end points, don't
change one on the fly by inverting it. How about having endPoint1 and endPoint2?

Just use absolute position values passed to moveTo.

To explain what happens in your original code:

You start at pos 0 and move to 8192. You then reset this as the origin and move to -8192,
which is the same as original pos 0, but called -8192 now. You then move to +8192, which
is 16384 steps... All you need is endPoint1 = 8192 and endPoint2 = 0.

Wow! Thanks for the help. endPoint1 and endPoint2 did the trick.

Y'all are great. This works.

void loop()
{
if (digitalRead(Down_Button) == LOW)
stepper1.moveTo(endPoint1);
if (digitalRead(Up_Button) == LOW)
{
stepper1.moveTo(endPoint2);
}
stepper1.run();
}