Stepper With Button and Bounce2 Library




Here are the photos for the wiring

Tried to implement the second button but i guess i'am lacking the part were i need to understand the coding language on this, since if we have it on loop how can i put a call on the second button in the middle of the loop? How will the arduino recognize it to make it stop?

  //Button1
  button.attach( buttonPin, INPUT ); // USE EXTERNAL PULL-UP
  //Button2
  button1.attach( buttonPin1, 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);
  button1.setPressedState(LOW);
  
  // Set stepper motor speed
  //stepper1.setSpeed(3000);

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

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

As already mentioned, simply draw a schematic that shows all of your wiring with pencil and paper.

If you want the second button to stop the stepper at any time, you have nearly completely to rewrite your sketch. You must not use any blocking functions, because while the function blocks, you cannot capture whether your button gets pressed.

It has been pointed out that your program design for stepper motor movement is what is called "blocking" and it prevents reading of the buttons.

Your step/dir A4988 stepper driver will let you create a new program which can avoid "blocking".

You can find an example here
https://forum.arduino.cc/t/simple-stepper-program/268292

and some good general reference on steppers here to help you understand

https://forum.arduino.cc/t/stepper-motor-basics/275223

ok i will take a look.

BTW i'am guessing my driver is just getting burn, the program does not makes the reverse on teh stepper, can you point a more robust or reliable driver?

Did you adjust the current at the driver accordingly to your stepper? And I still assume that your steprate may be too high. Do you use microstepping?

Yes, i did, on the coding i guess not :smiley:

Ok,

I want to try a different aprouch on this, i want the code on the loop to run for x minutes.

I checked this code out

starttime = millis();
endtime = starttime;
While ((endtime - starttime) <=1000) // do this loop for up to 1000mS
{
// code here
loopcount = loopcount+1;
endtime = millis();
}
serial.print (loopcount,DEC);

But can seem to get it to work since the while is giving me error...

Code for the loop

void singleStep() {
starttime = millis();
endtime = starttime;
While ((endtime - starttime) <=1000){ // do this loop for up to xmS

  randNumber = random(3000, 7000);
    delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);

    loopcount = loopcount+1;
endtime = millis();
  }

Oops, the While was written with a Caps W.......

All sorted out on the while statement now let's see if this thing works.

What kind of motor driver?

It's a NEMA 17, already change the driver since they did not take the punch!!!

Working!!!

Here's the code for who need's it.

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
#include <Stepper.h>

long randNumber;

byte directionPin = 5;
byte stepPin = 4;

byte buttonCWpin = 6;
byte buttonCCWpin = 7;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
unsigned long starttime = 0;
unsigned long endtime = 0;
unsigned long loopcount = 0;

Stepper stepper1(200, directionPin, stepPin);

void setup() { 

     Serial.begin(9600);
     Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(ledPin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     
}

void loop() { 
    
    curMillis = millis();
    readButtons();
    actOnButtons();
    
}

void readButtons() {
    
    buttonCCWpressed = false;
    buttonCWpressed = false;
    
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
}

void singleStep() {

starttime = millis();
endtime = starttime; 
   
long var = 0;
while ((endtime - starttime) <=120000){ // do this loop for up to 1000mS){ // do this loop for up to xmS

    randNumber = random(3000, 7000);
    delay(1000);
    stepper1.setSpeed(randNumber);
    stepper1.step(10000);
    delay(100);
    stepper1.setSpeed(randNumber);
    stepper1.step(-10000);
    
    loopcount = loopcount+1;
    endtime = millis();
  
  }
}

Hi Guys,

Just a small question, is it posible to use this kind of buttons to substitute the Mini PCB Buttons?
How is the connection made?

6650f6834f736b874e67e7ab4796c9b8

Yes, if the switch is momentary and not latching. Do you have a link to the button?

If the switch is monetary, then I would think that you would wire one tab to an input pin set for INPUT_PULLUP and the other tab to ground. The pin would read high when not pressed and low when pressed.

Yes, it's momentary, it always returns to the initial position, they call it Arcade microswitch
Thanks, will test it and give feedback!!!

I did not originally notice, but it looks like there are three blades coming from the microswitch. I believe this is the arrangement

Your two connections to the Arduino pin/ground should be NO and COM if you want to use INPUT_PULLUP and read LOW when pressed.

It worked flawlessly!!!