Hi Guys,
Could someone help me please with the following code, the code is supposed to move a motor forward then backward after it detects the change state of a microswitch, it does this as expected but it keeps on repeating while the switch is closed, I only want it to cycle once after the change state and then wait for it to go from low to high again before another cycle?
#include <AFMotor.h>
const int stopLightPin = 2; // input from "STOP LIGHT" output
const int startPin = 9; // output via relay to start cycle
const int homePin = 11; // microswitch for home position
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int markState = 0; // variable for reading the pushbutton status
AF_Stepper motor(200, 2);
void setup() {
// initialize the LED pin as an output:
pinMode(startPin, OUTPUT);
pinMode(stopLightPin, INPUT); //Maybe change this to pulldown if erratic
pinMode(homePin, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(stopLightPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
motor.setSpeed(100);
motor.step(2450, BACKWARD, DOUBLE); // DOUBLE or INTERLEAVE or MICROSTEP!
delay(100);
motor.setSpeed(100);
motor.step(2450, FORWARD, DOUBLE);
} else {
// in other words do nothing
//digitalWrite(startPin, LOW);
}
delay(100);
}
}
regards
Andrew