Good Day
A bit newish to coding. Topic might be a little vague but what I would like to know or get help with is being able to take a code that utilizes the regular Stepper library and replace it with the AccelStepper library. I have a code that allows for the opening and closing of a DIY flat panel for my telescope. The code works fine, I just wanted to be able to set the home position of the panel when it is initially powered up or reset. That part works great. The part I am having trouble with is in the loop. When I press the "Open" button I would like the panel to accelerate to its position and vise versa. Right now, I am kinda of using both libraries but I would to use AccelStepper excusively. I would have attached the .ino but being that I am a new user here, I cannot so I just copied and pasted. Apologies.
#include <Stepper.h>
#include "AccelStepper.h"
#define STEPS 20000 // steps per revolution for the motor
#define RPMS 150 // desired motor speed
#define home_switch 9
Stepper stepper(STEPS, 5, 3); // Blue wire (Arduino D5) goes to the TB6600 stepper driver DIR- pin. RED wire (Arduino D3) goes to PUL- of TB6600 stepper driver. Black wire loops from DIR+ to PUL+ and goes to the 5V of Arduino
AccelStepper stepperX(1, 5, 3);
long initial_homing=-1;
volatile int ledPin = 6; // the pin that the LED is attached to, needs to be a PWM pin.
int brightness = 0;
enum devices {
FLAT_MAN_L = 10,
FLAT_MAN_XL = 15,
FLAT_MAN = 19,
FLIP_FLAT = 99
};
enum motorStatuses {
STOPPED = 0,
RUNNING
};
enum lightStatuses {
OFF = 0,
ON
};
enum shutterStatuses {
NEITHER_OPEN_NOR_CLOSED = 0, // ie not open or closed...could be moving
CLOSED,
OPEN,
TIMED_OUT
};
enum motorDirection {
OPENING = 0,
CLOSING,
NONE
};
int deviceId = FLIP_FLAT; //set this to FLAT_MAN if you want to remove or not use the motor handling
int motorStatus = STOPPED;
int lightStatus = OFF;
int coverStatus = CLOSED;
float targetAngle = 0.0;
float currentAngle = 0.0;
int motorDirection = NONE;
void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
pinMode(home_switch, INPUT_PULLUP);
analogWrite(ledPin, 0);
//stepper.setSpeed(RPMS);
stepperX.setMaxSpeed(6000.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(5000.0); // Set Acceleration of Stepper
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepperX.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperX.run(); // Start moving the stepper
//delay(1);
}
stepperX.setCurrentPosition(0); // Set the current position as zero for now
stepperX.setMaxSpeed(4000.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(4000.0); // Set Acceleration of Stepper
initial_homing=1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepperX.moveTo(initial_homing);
stepperX.run();
initial_homing++;
// delay(1);
}
stepperX.setCurrentPosition(0);
//Serial.println("Homing Completed");
//Serial.println("");
stepperX.setMaxSpeed(5000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepperX.setAcceleration(3000.0); // Set Acceleration of Stepper
//stepper.setSpeed(RPMS);
}
void loop() {
handleSerial();
handleMotor();
}
void handleSerial() {
if ( Serial.available() >= 6 ) { // all incoming communications are fixed length at 6 bytes including the \n
char* cmd;
char* data;
char temp[10];
int len = 0;
char str[20];
memset(str, 0, 20);
Serial.readBytesUntil('\r', str, 20);
cmd = str + 1;
data = str + 2;
// useful for debugging to make sure your commands came through and are parsed correctly.
if ( false ) {
sprintf( temp, "cmd = >%c%s\n", cmd, data);
Serial.write(temp);
}
switch ( *cmd )
{
/*
Ping device
Request: >POOO\r
Return : *PiiOOO\n
id = deviceId
*/
case 'P':
sprintf(temp, "*P%dOOO\n", deviceId);
Serial.write(temp);
break;
/*
Open shutter
Request: >OOOO\r
Return : *OiiOOO\n
id = deviceId
This command is only supported on the Flip-Flat, set the deviceId accordingly
*/
case 'O':
sprintf(temp, "*O%dOOO\n", deviceId);
setShutter(OPEN);
Serial.write(temp);
break;
/*
Close shutter
Request: >COOO\r
Return : *CiiOOO\n
id = deviceId
This command is only supported on the Flip-Flat, set the deviceId accordingly
*/
case 'C':
sprintf(temp, "*C%dOOO\n", deviceId);
setShutter(CLOSED);
Serial.write(temp);
break;
/*
Turn light on
Request: >LOOO\r
Return : *LiiOOO\n
id = deviceId
*/
case 'L':
sprintf(temp, "*L%dOOO\n", deviceId);
Serial.write(temp);
lightStatus = ON;
analogWrite(ledPin, brightness);
break;
/*
Turn light off
Request: >DOOO\r
Return : *DiiOOO\n
id = deviceId
*/
case 'D':
sprintf(temp, "*D%dOOO\n", deviceId);
Serial.write(temp);
lightStatus = OFF;
analogWrite(ledPin, 0);
break;
/*
Set brightness
Request: >Bxxx\r
xxx = brightness value from 000-255
Return : *Biiyyy\n
id = deviceId
yyy = value that brightness was set from 000-255
*/
case 'B':
brightness = atoi(data);
if ( lightStatus == ON )
analogWrite(ledPin, brightness);
sprintf( temp, "*B%d%03d\n", deviceId, brightness );
Serial.write(temp);
break;
/*
Get brightness
Request: >JOOO\r
Return : *Jiiyyy\n
id = deviceId
yyy = current brightness value from 000-255
*/
case 'J':
sprintf( temp, "*J%d%03d\n", deviceId, brightness);
Serial.write(temp);
break;
/*
Get device status:
Request: >SOOO\r
Return : *SidMLC\n
id = deviceId
M = motor status( 0 stopped, 1 running)
L = light status( 0 off, 1 on)
C = Cover Status( 0 moving, 1 closed, 2 open, 3 timed out)
*/
case 'S':
sprintf( temp, "*S%d%d%d%d\n", deviceId, motorStatus, lightStatus, coverStatus);
Serial.write(temp);
break;
/*
Get firmware version
Request: >VOOO\r
Return : *Vii001\n
id = deviceId
*/
case 'V': // get firmware version
sprintf(temp, "*V%d003\n", deviceId);
Serial.write(temp);
break;
}
while ( Serial.available() > 0 ) {
Serial.read();
}
}
}
void setShutter(int val)
{
if ( val == OPEN && coverStatus != OPEN )
{
motorDirection = OPENING;
targetAngle = 4500.0;
}
else if ( val == CLOSED && coverStatus != CLOSED )
{
motorDirection = CLOSING;
targetAngle = 0.0;
}
}
void handleMotor()
{
if (currentAngle < targetAngle && motorDirection == OPENING) {
motorStatus = RUNNING;
coverStatus = NEITHER_OPEN_NOR_CLOSED;
stepper.step(1);
currentAngle = currentAngle + 360.0 / STEPS;
if (currentAngle >= targetAngle) {
motorStatus = STOPPED;
motorDirection = NONE;
coverStatus = OPEN;
}
} else if (currentAngle > targetAngle && motorDirection == CLOSING) {
motorStatus = RUNNING;
coverStatus = NEITHER_OPEN_NOR_CLOSED;
stepper.step(-1);
currentAngle = currentAngle - 360.0 / STEPS;
if (currentAngle <= targetAngle) {
motorStatus = STOPPED;
motorDirection = NONE;
coverStatus = CLOSED;
}
}
}
The motor being used is a https://www.amazon.com/gp/product/B00QEWAL98/ref=ppx_yo_dt_b_asin_title_o09_s00?ie=UTF8&psc=1
I am using a TB6600 stepper driver because happened to have one and I suspect I don't need to #define STEPS. Not quite certain.
Like stated above, what can I do in the loop to get it to use AccelStepper instead of the regular stepper library?
Thanks for taking the time, good, bad or indeiffernt.