Hi All,
I have an oscillation motion operating on two optical endstops. I also have it wired to startup on with the touch of a button using an ezButton library.
The Endstops work. The Oscillation Works. The Startup button works. However, I noticed that I upon startup, I don’t have acceleration. It just starts off at the set speed I designated. Why is that? I have set the acceleration, so I’m not sure what’s going on.
Any ideas? I’m still fairly new to coding so any help would be so much appreciated.
Thanks!
#include <AccelStepper.h> //accelstepper library
#include <ezButton.h>
#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1
// constants won't change. They're used here to set pin numbers:
const byte limitSwitch_1 = 2; //pin for the microswitch
const byte limitSwitch_2 = 3; //pin for the microswitch
int smDirectionPin = 9; //Direction pin
int smStepPin = 8; //Stepper pin
// variables will change:
int switch1State = 0; // variable for reading the Switch 1 status
int switch2State = 0; // variable for reading the Switch 2 status
boolean ForwardState = 0; // variable for reading the micro-switch status
boolean BackwardState = 0; // variable for reading the microswitch status
//------------------------------------------------------------------------------------
// create ezButton object that attach to pin 7;
ezButton button(7);
int loopState = LOOP_STATE_STOPPED;
//direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 9, 8);
void setup()
{
//Limit Switches
pinMode(limitSwitch_1, INPUT_PULLUP); // internal pullup resistor (debouncing)
pinMode(limitSwitch_2, INPUT_PULLUP); // internal pullup resistor (debouncing)
//---------------------------------------------------------------------------
//Stepper parameters
//setting up some default values for maximum speed and maximum acceleration
stepper.setMaxSpeed(5000); //SPEED = Steps / second
stepper.setSpeed(5000);
stepper.setAcceleration(500);
// set debounce time to 50 milliseconds
button.setDebounceTime(50);
}
void SlideForward()
{
stepper.setSpeed(500);
}
void SlideBackward()
{
stepper.setSpeed(-500);
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
if (loopState == LOOP_STATE_STOPPED)
loopState = LOOP_STATE_STARTED;
else // if(loopState == LOOP_STATE_STARTED)
loopState = LOOP_STATE_STOPPED;
}
if (loopState == LOOP_STATE_STARTED) {
// read the state of the switches value;
ForwardState = digitalRead(limitSwitch_1);
BackwardState = digitalRead(limitSwitch_2);
//step the motor (this will step the motor by 1 step at each loop indefinitely)
stepper.runSpeed();
// check which pin 2 or 3 is HIGH:
if (ForwardState == HIGH && BackwardState == LOW) {
SlideForward();
}
if (BackwardState == HIGH && ForwardState == LOW) {
SlideBackward();
}
}
}