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;
}