I changed the code in function of the example of StateChangeDetection:
#include "AccelStepper.h"
AccelStepper stepper (1, 9 , 8);
#define home_switch 4
#define buttonState 7
long initial_homing = -1;
int x;
int sensorPushCounter = 0;
int sensorState = 0;
int lastsensorState = 0;
void setup() {
pinMode(home_switch, INPUT_PULLUP);
delay(150);
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(1.0);
stepper.setAcceleration(1500.0);
initial_homing=1;
while (digitalRead(home_switch)) {
stepper.moveTo(initial_homing);
initial_homing++;
stepper.run();
delay(5);
}
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(1.0);
stepper.setAcceleration(1500.0);
initial_homing=1;
while (!digitalRead(home_switch)) {
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(5);
}
stepper.setCurrentPosition(0);
x == 0;
stepper.setMaxSpeed(30.0);
stepper.setAcceleration(40.0);
stepper.setSpeed(20);
}
void loop()
{
sensorState = digitalRead(buttonState);
if (sensorState != lastsensorState){
if (buttonState == HIGH){
sensorPushCounter++;
}
}
if (sensorPushCounter % 9 == 0) {
stepper.runSpeed();
} else {
stepper.stop();
}
if (digitalRead(home_switch) == HIGH) {
stepper.stop();
sensorPushCounter = 0;
}
}
Now the stepper runs like crazy without stopping. It doesn't respond to the sensor or to the home_switch.
Any idea of why??