#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper = AccelStepper(1, D6, D7); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
const int MS2pin = D4; //pin to change the steps per revolution
int buttonState = HIGH; //button is pulled HIGH so it's initalized as HIGH
int motorPos; //set motor position to 0 at start
int max_speed = 800;
int acceleration = 400;
int lastButtonState = 1;
void setup() {
Serial.begin(9600);
pinMode(MS2pin, OUTPUT);
pinMode(D11, INPUT_PULLUP); // Button 1 pin mode so that the default pin is at 3.3V
stepper.setMaxSpeed(max_speed);
stepper.setAcceleration(acceleration); }
void loop(){
buttonState = digitalRead(D11);
digitalWrite(MS2pin, HIGH);
if ((buttonState != lastButtonState) && buttonState == LOW){
if (motorPos == 0){
motorPos = 500;
stepper.runToNewPosition(motorPos);
}
else{
motorPos = 0;
stepper.runToNewPosition(motorPos);
}
delay(50);
}
lastButtonState = buttonState;
stepper.setMaxSpeed(max_speed);
stepper.setAcceleration(acceleration);
}
So right now I would like the motor to run to position 500 when the button is pressed and the motor is at 0 and then run to 0 when the motor is at 500. Right now it will run to 500 as if it is starting from 0 with every button press, regardless of what it did the last time.
The only way it will go from 0 to 500 is if I hold down the button.
I'm thoroughly confused at the moment. Any help would be appreciated. Let me know if you need any additional information!
i modified it below to separately recognize a button change which is captured a a short delay() performed and the test for a press (LOW == but)
#include "AccelStepper.h"
#define D4 4
#define D6 6
#define D7 7
#if 0
#define D11 11
#else
#define D11 A1
#endif
// Define a stepper and the pins it will use
AccelStepper stepper = AccelStepper(1, D6, D7);
// Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
const int MS2pin = D4;
//pin to change the steps per revolution
int buttonState = HIGH;
//button is pulled HIGH so it's initalized as HIGH
int motorPos;
//set motor position to 0 at start
int max_speed = 800;
int acceleration = 400;
int lastButtonState = 1;
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(MS2pin, OUTPUT);
pinMode(D11, INPUT_PULLUP);
// Button 1 pin mode so that the default pin is at 3.3V
stepper.setMaxSpeed(max_speed);
stepper.setAcceleration(acceleration);
}
// -----------------------------------------------------------------------------
void loop(){
buttonState = digitalRead(D11);
digitalWrite(MS2pin, HIGH);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == LOW) {
if (motorPos == 0){
motorPos = 500;
stepper.runToNewPosition(motorPos);
}
else{
motorPos = 0;
stepper.runToNewPosition(motorPos);
}
}
delay(10);
}
#if 0
stepper.setMaxSpeed(max_speed);
stepper.setAcceleration(acceleration);
#endif
}
It's telling me A1 was not declared in the scope. If I comment out that line, then nothing happens at all when I press the button. When I comment out that entire section, then I get the same results that I did from the start unfortunately.