I have a problem using acceleration. Everything works nicely except when I come to the point where the motor changes direction. The motor runs for a brief period in the wrong direction before running nicely in the correct direction.
The code below works fine if I edit it for constant speed. However, I'm using this library because I want a nicer look to how it runs smoothly with acceleration.
I am new to this and I have tried looking through this group for similar questions/answer to no avail.
I've stripped down my code to the bare bones below. Basically the program homes and then opens a curtain until a limit switch triggers and then closes a curtain until a limit switch triggers. Thanks for any help/thoughts.
#include "AccelStepper.h"
AccelStepper stepperX(1, 9, 8);
#define home_switch 2 // Pin 2 connected to Curtain Closed
#define open_switch 3 // Pin 3 connected to Curtain Open Switch
long TravelX = 0; //
long position;
int incomingByte = 0;
void setup() {
Serial.begin(9600);
pinMode(home_switch, INPUT_PULLUP);
pinMode(open_switch, INPUT_PULLUP);
delay(5);
stepperX.setAcceleration(100.0);
stepperX.setMaxSpeed(200.0);
stepperX.setSpeed(100.0);
while (digitalRead(home_switch)) {
stepperX.runSpeed();
delay(5);
}
stepperX.setCurrentPosition(0);
Serial.println("Curtain Homed and Closed");
}
void loop() {
while (Serial.available() > 0) { // Check if values are available in the Serial Buffer
Serial.println("Curtain Open Start");
incomingByte = Serial.read();
while (digitalRead(open_switch)) { // Make the Stepper move CCW to open curtain until the switch is activated
stepperX.moveTo(-99999);
stepperX.run();
}
Serial.println("Curtain Open Finished");
//PUT PLAY CODE HERE
Serial.println("Show is on");
delay(2000);
Serial.println("Curtain Close Start");
while (digitalRead(home_switch)) { // Make the Stepper move CW to close curtain until the switch is activated
stepperX.moveTo(99999);
stepperX.run();
}
Serial.println("Curtain Closed Finished");
}
}