Push button problem for stepper motor

Hi there
I use a stepper motor with a potentiometer for speed and a push button for direction.
the problem i have because it is a mechanical switch it switch the direction a few times if i press the button one can someone please help.
see code below

// Defin pins
 
int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer
 
// Variables
 
int pd = 500;       // Pulse Delay period
boolean setdir = LOW; // Set Direction
 
// Interrupt Handler
 
void revmotor (){
 
  setdir = !setdir;
  
}
 
 
void setup() {
 
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
  
}
 
void loop() {
  
    pd = map((analogRead(spd)),0,1023,2000,50);
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 
}

mechanical buttons bounce and using an interrupt is not a good approach

consider for button detection

// simple button processing

const byte butPin = A1;
byte       ledPin = 13;

byte butState;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    digitalWrite (ledPin, Off);
    pinMode      (ledPin, OUTPUT);

    pinMode     (butPin, INPUT_PULLUP);
    butState  = digitalRead (butPin);
}

Hi There
Thank you for the information, but I am new with programming and I am not sure how to incorporate the code you send into my code.
Can you please help?
Regards
Johannes

Maybe better to include your code in his.

Substitute the thing you want done when the button is pressed, for the one line LED toggle demo in the sketch. I guess it would be

  setdir = !setdir;

:saluting_face:

Hi, @lappieslatandra
How have you got your button wired?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
Please no Fritzy diagrams.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

In addition to the software debounce shown by @gcjr there is hardware debounce. Hardware debounce is a 0.1uF (or so) ceramic capacitor placed across the switch.

The best way to wire a switch with modern microcontrollers is from ground to an input that is set to pinMode INPUT_PULLUP.

Hi there
I will try a 0.1uf capacitor and see what it does.
thank you for the advice.
Johannes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.