Hi, I have done some steps forward trying to use a button.
So far the code I have written is this:
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper Stepper1(1,10,6); //use pin 6 and 10 for dir and step, 1 is the "external driver" mode (A4988)
int dir = 1; //used to switch direction
boolean ShortPress;
boolean State;
int ButtonPin = 2;
void setup() {
Stepper1.setMaxSpeed(3200); //set max speed the motor will turn (steps/second)
Stepper1.setAcceleration(200); //set acceleration (steps/second^2)
pinMode(ButtonPin, INPUT_PULLUP);
}
void loop() {
readButtons();
actOnButtons();
}
void readButtons() {
ShortPress = true;
ShortPress = false;
int i = 0;
while (digitalRead(ButtonPin))
{
i++; // how much the button has been held
}
if (i<100)
{ ShortPress=true;}
else{ShortPress = false;}
}
void actOnButtons() {
if (ShortPress == true && State == true){
Stepper1.distanceToGo()==0; { //check if motor has already finished his last move
Stepper1.move(9600*dir);
}
//Stepper1.moveTo(200);
Stepper1.run(); //run the stepper. this has to be done over and over again to continously move the stepper
}
else if (ShortPress == false){
Stepper1.stop();
}
}
Using this code the stepper works only if I keep the button held, while the button is unpressed the motor is quiet.
what I want to do is to use a kind of a code like this
if (ShortPress $ state) { !state-> first press}
elseif {!state ->second press}
if (!ShortPress) {Long Press, reset}
- to start the motor after one single short push
- to stop the motor after a second short push
- to rewind the motor after that the button has been hel for a while
any tips?