Accelstepper help with button controll

Hello I'm currently having a problem here.

I have 3 buttons forward, reverse, and reset. Reset is to stop the run. I'm been trying all different things here, but I'm think I'm on the wrong path.

In my code the buttons are controlled from another arduino. Once either forward or reverse buttons are pushed it flags then to 1. Then reset just sets them back to zero. The code needs to run continuous until button timer from other arduino sets them back to zero or I hit the reset button which sets pins back to zero.

//included libraries

#include <AccelStepper.h>

//Defines

#define DIR_PIN 8//Direction pin on MA860H
#define STEP_PIN 9//Step pin on MA860H
AccelStepper stepperA(1, STEP_PIN, DIR_PIN);

//variables
int cwPin = 2;
int ccwPin = 3;
int motorPin = 4;
int valcwPin = 0;
int valmotorPin = 0;
int valccwPin = 0;
int setMotorSpeed = 200;
int accelSpeed = 50;

int buttonstateForward;
int buttonstateReverse;
void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(cwPin, INPUT);
  pinMode(motorPin, INPUT);
  pinMode(ccwPin, INPUT);

  //set output pins to motor
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);

  // Enable motor variables

  stepperA.setMaxSpeed(setMotorSpeed);
  stepperA.setAcceleration(accelSpeed);

}

void loop() {

  valcwPin = digitalRead(cwPin);
  valmotorPin = digitalRead(motorPin);
  valccwPin = digitalRead(ccwPin);
  
  Serial.println(valcwPin);
  Serial.println(valccwPin);
  
  // put your main code here, to run repeatedly:

  if (valcwPin = 1) {

    stepperA.move (50000);
    stepperA.run();

  }

  if (valccwPin = 1) {

    stepperA.move(-50000);
    stepperA.run();

  }
  
  else (valccwPin < 1 || valcwPin < 1){
    
    stepperA.stop():
  }
}

Ephdup:
The code needs to run continuous until button timer from other arduino sets them back to zero or I hit the reset button which sets pins back to zero.

You have not told us what it actually does.

...R
Stepper Motor Basics
Simple Stepper Code

Oh I'm so sorry Robin lol.

When I power up arduino the servo just seems to start to run, even though both valcwPin and
valmotorPin are equal to 0. It looks like to me in the code it should run unless they equal 1.

if (valcwPin = 1) {

Should be:

if (valcwPin == 1) {
if (valccwPin = 1) {

Should be:

else if (valccwPin == 1) {
else (valccwPin < 1 || valcwPin < 1){

Should be:

else if(valccwPin == 0 && valcwPin == 0){

When I power up arduino the servo just seems to start to run

You're running stepper motor code on a servo? Let's start over.
What kind of motor do you have?

I swear I'm super bad at posting clear and concise information for you guys to follow. I apologies for that and please bear with me. I am currently running a stepper, not a server. I don't know what I was thinking.

I have some update code that works with some kinks. Here is the code

//included libraries

#include <AccelStepper.h>

//Defines

#define DIR_PIN 8//Direction pin on MA860H
#define STEP_PIN 9//Step pin on MA860H
AccelStepper stepperA(1, STEP_PIN, DIR_PIN);

//variables
int cwPin = 2;
int ccwPin = 3;
int motorPin = 4;
int valcwPin = 0;
int valmotorPin = 0;
int valccwPin = 0;
int setMotorSpeed = 200;
int accelSpeed = 50;
int motorForward = 0;
int motorReverse = 0;
void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(cwPin, INPUT);
  pinMode(motorPin, INPUT);
  pinMode(ccwPin, INPUT);

  //set output pins to motor
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);

  // Enable motor variables

  stepperA.setMaxSpeed(setMotorSpeed);
  stepperA.setAcceleration(accelSpeed);

  }

void loop() {
  
  valcwPin = digitalRead(cwPin);
  valmotorPin = digitalRead(motorPin);
  valccwPin = digitalRead(ccwPin);

 
if (valcwPin == 1 && valmotorPin == 1){ //Clock Wise Rotation
  
  stepperA.move(500000);
  stepperA.run();
  }

else if(valccwPin == 1 && valmotorPin ==1){ //Counter Clock Wise Rotation
  
  stepperA.move(-500000);
  stepperA.run();
  
}

else { // Motor Stop
  
  stepperA.move(0);
  stepperA.stop();
  
  
}

}

In this code I watch for valcwPin, calccwPin, and valmotorPin to go to 1. Once one or the other go 1, then the motor starts to spins whichever direction is commanded. In the code when valcwPin, calccwPin, and valmotorPin go to 0 the motor stops. The issues is, it stop abruptly without slowly accelerating to a stop. The moment I command the opposite direction it finishes its previous slowdown acceleration and then starts going the direction commanded.

Ephdup:
The issues is, it stop abruptly without slowly accelerating to a stop.

The moment I command the opposite direction it finishes its previous slowdown acceleration and then starts going the direction commanded.

I don't understand the second sentence. Are you saying that it changes direction abruptly or that is slows to a stop and accelerates smoothly in the other direction?

As far as a smooth stop is concerned you will need to consider carefully how you want to control the motor. If you stop the calls to stepper.run() no steps can happen - hence the abrupt stop.

The AccelStepper library is designed for the case where you know in advance how many steps are required and then it can figure out a pattern of acceleration and deceleration to move that number of steps smoothly.

If you want to have a system in which a motor moves while you have a button pressed then I suspect the AccelStepper library is not suitable.

It is not very difficult to write your own acceleration code (see this link for a suggestion) and then you could arrange things so that when you release the button the motor will continue moving while smoothly slowing to a stop.

...R

I guess its ok if the motor just stops, it does not have to decelerate, but I try to explain this better. When control pins valcwPin and valmotorPin goes to 0 it runs the else condition which abruptly stops the stepper. Now if I go and click the ccw button it then finishes the previous deceleration curve and then begins running the ccw condition. Thanks for your help R....

Thank you for your help. These are things that messing around and talking to people really helps. I'm wouldn't know just by looking at the accelstepper library that what I'm trying to do isn't exactly what its meant to be used for. So thank you once again steering me close to my end goal.

Hello R...

Thank you are posting that link to the stepper with acceleration example. Because I'm newer to all of this its going to take me a bit to understand your code. Normally examples like this one are harder for me to follow because for me its very abstract thinking. For some it's probably pretty easy to follow along, I struggle.

Once again thank you for your help.