Help with button and stepper code

I am having a little issue and can not figure it out. I have some code that when I press a button it turns on a stepper motor. I have tested the stepper code and it runs the motor. I have tested the button code for start and stop and it works. But when I put the stepper code in the button code it does not turn on the motor. Any help is greatly appreciated.

const int buttonPin1 = 2;
int buttonState1 = HIGH;
int lastButtonState1 = HIGH;
unsigned long lastDebounceTime1 = 0;

const int buttonPin2 = 3;
int buttonState2 = HIGH;
int lastButtonState2 = HIGH;

unsigned long lastDebounceTime2 = 0;
unsigned long debounceInterval = 50;

#define dirPin 4
#define  stepPin 5

void setup() {
Serial.begin(9600);

pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, LOW);

pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {
int reading1 = digitalRead(buttonPin1);
if (reading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}

int reading2 = digitalRead(buttonPin2);
if (reading2 != lastButtonState2) {
lastDebounceTime2 = millis();
}

if ((millis() - lastDebounceTime1) > debounceInterval) {
if (reading1 != buttonState1) {
buttonState1 = reading1;

if (buttonState1 == HIGH) {
Serial.println("start");
digitalWrite(stepPin, HIGH);
delayMicroseconds(400);
digitalWrite(stepPin, LOW);
delayMicroseconds(400);
}
}
}

lastButtonState1 = reading1;
if ((millis() - lastDebounceTime2) > debounceInterval) {
if (reading2 != buttonState2) {
buttonState2 = reading2;

if (buttonState2 == HIGH) {
Serial.println("stop");
//turn off stepper motor here
}
}
}
lastButtonState2 = reading2;
}

Try this:

#include <AccelStepper.h>
#include <Bounce2.h>

#define NUM_BUTTONS 2

const int dirPin = 4;
const int stepPin = 5;
const int motorInterfaceType = 1;

bool start = false;

const int buttonPin[NUM_BUTTONS] = {2, 3};

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

Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};

void setup()
{
  Serial.begin(9600);

  for (byte i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].attach(buttonPin[i], INPUT_PULLUP);
    button[i].setPressedState(LOW);
    button[i].interval(5);
  }
  myStepper.setMaxSpeed(1000);
}

void loop()
{
  if (start == true)
  {
    myStepper.setSpeed(400);
    myStepper.runSpeed();
  }
  else
  {
    myStepper.stop();
  }

  for (byte i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].update();

    if (button[0].pressed() )
    {
      Serial.println("start");
      start = true;
    }
    else if (button[1].pressed() )
    {
      Serial.println("stop");
      start = false;
    }
  }
}

seems like you made running of the motor conditional on debouncing a button. do you need 2 buttons?

consider

const byte dirPin    = 4;
const byte stepPin   = 5;
const byte buttonPin = 2;

byte butLst;
byte run;

unsigned long usecStep = 400;
#define Delay  delayMicroseconds

// -----------------------------------------------------------------------------
void loop ()
{
    if (run)  {
        digitalWrite      (stepPin, HIGH);
        Delay             (usecStep);
        digitalWrite      (stepPin, LOW);
        Delay             (usecStep);
    }

    byte but = digitalRead (buttonPin);
    if (butLst != but) {
        butLst = but;
        delay (20);     // debounce

        if (LOW == but)  {
            run = ! run;
            Serial.println (run);
        }
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    pinMode      (stepPin, OUTPUT);
    pinMode      (dirPin,  OUTPUT);
    digitalWrite (dirPin,  LOW);

    pinMode (buttonPin, INPUT_PULLUP);
    butLst = digitalRead (buttonPin);
}

That was perfect. Thank you so much.

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