Stepper -Push Button- Help

Hello, I have this program :

include

int startTl = 2; int rychleTl = 3; int StartBt; int RychlostBt; int Position; int speedS;

define dirPin 6

define stepPin 7

define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() { // maximum speed and acceleration: stepper.setMaxSpeed(20000); stepper.setAcceleration(1000); stepper.setSpeed(6000); }

void loop() { StartBt = digitalRead(startTl); RychlostBt = digitalRead(rychleTl);

if (StartBt == HIGH && RychlostBt == HIGH ) { //speed

stepper.runSpeed();

stepper.move(5000L); } else if (StartBt == HIGH && RychlostBt == LOW ) { //deceleration!!! stepper.stop(); }

else if (StartBt == LOW && RychlostBt == LOW ) { }

stepper.run(); }

I control the stepper motor (nema 23). StartBt is SWITCH button, RychlostBt is PUSH button. What I need to do is: after I switch StartBt to 1 and RychlostBt push (I'll just press one or more times it doesn't matter) motor accelerates and if I switch StartBt to 0, motor declerate. Now I have to hold RychlostBt when I accelerate, and I don't want that.

Is there someone who could help me ?

Thank you !!!

Please correct your post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Do you have pullup or pulldown resistors on input pins to keep the pins from "floating" and causing false HIGHs or LOWs when the button is not pressed? Best way is like S3 in the diagram, no external resistor needed, logic is reversed though (pin is LOW when button is pressed).

|500x449

Look up state machines. Think of your code in discrete chunks. You have a button/switch which can have 2 states 0/1. Put it in its own function (outside loop) and call it in loop. in the function you should read the switch, carry out any debouncing required, and change the state which can be held in a global variable e.g. StartBtState.

Then you make code for what you to do if startBtState is 0 and one for if 1.

Do the same for button 2.

Remember the loop loops so all code should be understood in terms of running over and over again very fast and, as such, you can detect changes and enter different parts of code as required.

user34785412: Now I have to hold RychlostBt when I accelerate, and I don't want that.

Use button to change the state of a variable and use the variable to decide how the motor behaves. Something like this

prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH and prevButtonState == LOW) {
  if (motorState == 'S') {
    motorState = 'A';
  }
 else {
   motorState = 'S';
 }
}

if (motorState == 'S) {
   stepper.stop();
}
if (motorState == 'A') {
  stepper.runSpeed();
}

This should start the motor the first time you press the button and stop it the next time you press it.

...R

You had better to use the internal pull up and debounce for button or use the button library

Ok, thank you guys !