Stepper With Button and Bounce2 Library

Checked the Bounced2 Library and tested the example button that the library have and it worked perfectly.
Tried to adapt to our code, which consists in having a stepper motor to run front and back with a randNumber run in loop with the start of the button pressed and it only have to stop when we pressed a second button or the same button, but noting is happening, can anyone please help me out with the code? Or maybe having some ideias to make a better one.

#include <Stepper.h>
#include <Bounce2.h>

// Declare the used pins
int dirA = 4;
int dirB = 5;
int pwmA = 2;
int pwmB = 3;

long randNumber;
const int buttonPin = 6;
const int buttonPin1 = 7;


// Declare a Stepper motor with 200 steps 
Stepper stepper1(200, dirA, dirB);

Bounce2::Button button = Bounce2::Button();

void setup() {
    
  // PWM pins require declaration when used as Digital
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  
  // Set PWM pins as always HIGH
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  Serial.begin(9600);
  
  //Button1
  button.attach( buttonPin, INPUT ); // USE EXTERNAL PULL-UP

  // DEBOUNCE INTERVAL IN MILLISECONDS
  button.interval(5); 

  // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
  button.setPressedState(LOW);
  
  // Set stepper motor speed
  //stepper1.setSpeed(3000);

}
void loop(){

  button.update();

  randNumber = random(3000, 7000);
  //Serial.println(randNumber); 

 
  if ( button.pressed() ) {
  delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
 }
 else {
    
      stepper1.step(0);
     
    
  }
 
}

I haven't used that particular library. Try button2.h. It is easy to set up.

If the library example worked correctly, then the issue is with your stepper control replacing he led toggle of the example.

randNumber = random(3000, 7000);
 stepper1.setSpeed(randNumber);

setSpeed is rpm and its not clear that you stepper can run at the speed you are asking. They generally run at 1200 rpm or lower.

Try you code with a significantly lower speed.

The main problem is not on the motor running, that part is working perfect the proble is with the button, one press and it doens't do anything, with the led it worked maybe i'am missing something?

No one to help???

How did you wire your button? Did you use an external pullup?

  button.attach( buttonPin, INPUT ); // USE EXTERNAL PULL-UP

or use INPUT_PULLUP instead. I tried your sketch and it worked.
Because your sketch is blocking, the button will not be recognized while the motor is spinning.

And what do you mean by 'one press' ?
button.pressed() does edge detection, so you must release and press again for the next spinning of the motor.

Yes i did use a 100 resistor.

The idea is to press the button one time, press and depress, and the motor will do the turns until we press and depress the second button to stop it.

100 Ohm? Why such a low resistance? 1kOhm should be sufficent. The internal pullup is even much higher.

I don't see anything about your second button in your code. How does the sketch behave as it is now?
If you want to react to the second button while the motor is spinning you must write non blocking code.

The second button would be added after i could make the first one to work, which i can't and i can't understand why since i make the led to work. But yes i'am new to all this stuff so.

Do you think it should do the trick changing the resistor?

The resistor is unnecessarily low, but it should work.

'Which I can't' is a bad descrition of what happens if you press the button ( and what you expected to happen with this one button code ).
I tried your code and it worked as I expected. (When you press the button, the stepper goes back and forth once. When you press again there is another pass and so on )

Please post a schematic and a photo how you wired it. Maybe there is a problem.

Here's the wiring

Regarding the code yes it's true it only works once but what i wanted to to it's to keep running back and forth until with a second button i can stop it.

BTW thanks for all the help you are giving me

Do you want to stop it at any time, or only if it has reached it's end positions? You must remember where you stopped it, otherwise you will run in trouble with the next start.

What components are you using? Stepper and stepper driver ) . Please post links to the components.
I would be very surprised that the stepper works with these high RPM as @cattledog already has written.

Yes, the ideia is to stop at any time and it can start in any position no problem with that.
I'am using Steeper Nema 17 and Stepper Driver - https://www.amazon.es/gp/product/B07MXXL2KW/ref=ppx_yo_dt_b_asin_title_o03_s01?ie=UTF8&psc=1

if ( button.pressed() ) {
  delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
 }
 else {    
      stepper1.step(0);    
  }

The state of button.pressed() changes to false when you release, and the stepper section is not executed.

Add a new boolean variable called runStepper. Initialize to false. Set it to true when button.pressed() and then use it as a control for the stepper. You can set it false when the second button is pressed.

button.update();
randNumber = random(3000, 7000);
  //Serial.println(randNumber);  
  //if ( button.pressed() ) {
if ( button.pressed() )  runStepper = true;

if(runStepper){
  delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
 }
 else {    
      stepper1.step(0);    
  }

Your circuit diagram is not consistent with your code. Which pin is connected t the button. Pin1 is a Serial pin and should not be used.

Even worse. button.pressed() only becomes true at the moment the button is pressed, not while it is hold pressed.
The advise with the flag to start and sop the stepper is shurely the right way to do it.
But if he wants to stop the stepper at any time, thr blocking delay() and the blocking stepper movement doesn't work.

already tried the code and it does not work, the stepper only runs once

void loop(){
  bool runStepper = false;
  button.update();
  randNumber = random(3000, 7000);
  //Serial.println(randNumber); 
  
  //if ( button.pressed() ) {
if ( button.pressed() )  runStepper = true;

if(runStepper){
  delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
 }
 else {
    
      stepper1.step(0);
     
    
  }
 
}
  

Regarding the schemtic yes, it's true, i made a mistake in the drawing, it's start's on the 2 pin and goes out to 7

You must not do that in loop. Define runStepper as a global variable.

The link to your stepper driver shows something completely different from your fritzing. We need to know exactly which components you use and how they are wired, Not some arbitrary fritzing pictures similar to your wiring.
You can draw a schematic with pencil and paper and take a photo. And show every connection you made. And also take a photo of your actual wiring.
Nema17 doesn't tell anything about the electrical data of your motor. Please provide a link to your motor too.

void loop(){
  //bool runStepper = false;
static bool runStepper = false;

You are resetting the variable every pass of the loop and it will go false when the button is released. Declare as static bool to kept its value.

EDIT: Or make it global as suggested.

Will do, will take a picture of it but yes fritzing do not have the type o stepper driver i use.

YES!!!! it worked like a charm, it's stays running, now let's see if i can get the second button to work!!

Thank you so much for all the help you are giving me!!!