I am having trouble with the seemingly simple task of getting a stepper motor to rotate constantly with no jumps or jitters.
What I would like to happen: The motor sits at rest until a button is pressed. Once the button is pressed, the motor spins up to speed with a very smooth acceleration profile (rates and speed easily adjustable in code). The motor spins indefinitely until I press the button once again, where it follows a smooth deceleration profile down to rest.
I have commented out the LCD functionality because it appears to introduce serious code hangups that cause non-smooth rotation. (question for another day?)
I tried to program the ability to change the speed on the fly with two additional buttons (speed increment and speed decrement). I will play with these once the overarching problem of just spinning the motor has been solved.
The code is pretty non-functional, I have been losing track of what works together in this library. All of the constant speed examples I have been able to find do not implement accelerations.
// LIBRARIES
#include <Bounce.h> // debouncing
#include <serLCD.h> // serial LCD from SparkFun
#include <SoftwareSerial.h> // Software Serial (for LCD)
#include <AccelStepper.h> // accelleration based stepper library
#include <Wire.h> // ????????
// Variables
int MotorRun = 0; // TRUE or FALSE for if the mnotor is moving or not
int MotorSpeed = 0; // integer RPM (rotations per minute)
int SetMotorSpeed = 1600; // The motor speed that is desired (SETPOINT)
int RateInc = 5; // Amount (in RPMs) that the motor speed is incremented each time a decrement button is pressed
int StartUp = 1; // TRUE or FALSE if it is starting up
int MaxAccel = 1000; // Used by AccelStepper for accelleration
// INPUTS
#define PowerButt 2 // On/Off button
#define IncButt 3 // increment button
#define DecButt 4 // decrement button
Bounce bouncerON = Bounce( PowerButt,5 ); // debouncing
Bounce bouncerINC = Bounce( IncButt,1 );
Bounce bouncerDEC = Bounce( DecButt,1 );
// OUTPUTS
#define LitePin 5 // pin for light-up button LEDs
#define DisplayPin 6 // pin for Rx on lcd display
// AccelStepper
#define DIR_PIN 7 // direction pin on Big EasyDriver from SparkFun
#define STEP_PIN 9 // Step pin on Big EasyDriver
AccelStepper stepperB(1, STEP_PIN, DIR_PIN);
// Lcd Screen
serLCD lcd(DisplayPin);
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(PowerButt, INPUT);
pinMode(IncButt, INPUT);
pinMode(DecButt, INPUT);
Serial.begin(38400);
lcd.clear(); lcd.print("WELCOME"); delay(1000);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
bouncerON.update ( );
int valuePow = bouncerON.read(); // read the power button
if (valuePow == LOW) { // button is pressed (low because of PULLLUP resistor)
if (MotorRun == 0) { // motor is off
MotorRun = 1; // turn on the motor
stepperB.setMaxSpeed(1600);
stepperB.setAcceleration(100);
}
else {
MotorRun = 0; // turn off the motor
}
}
if (MotorRun == 1) { // are we supposed to be spinning???????????????????????????????????????????????????????????????????????????????????
digitalWrite(LitePin, HIGH); // Leds on buttons are on
//lcd.clearLine(2); lcd.print("RPM = "); lcd.print(SetMotorSpeed);
stepperB.run();
}
// stop rotating smoothly
else { // if motor has been turned off, slow it down gradually ???????????????????????????????????????????????????????????????????????????????
digitalWrite(LitePin, LOW); // Leds on buttons are off
//lcd.print("Motor OFF"); lcd.selectLine(2); lcd.print("set RPM "); lcd.print(SetMotorSpeed); lcd.selectLine(1);
stepperB.setMaxSpeed(0); // stop the motor
stepperB.setAcceleration(MaxAccel);
stepperB.run();
}
bouncerINC.update ( );
int valueInc = bouncerINC.read(); // read the power button
bouncerDEC.update ( );
int valueDec = bouncerDEC.read(); // read the power button
if (valueInc == LOW || valueDec == LOW) { // if either of the increment buttons have been pressed
if (valueInc == LOW) {
SetMotorSpeed = SetMotorSpeed + RateInc;
}
else {
SetMotorSpeed = SetMotorSpeed - RateInc;
}
}
Serial.println(MotorRun);
} // end of LOOP