I am new to all of this and have been self-educating.
However, I am unable how to figure something out.
I have been able to get my stepper motor to work with an android app that I created with the code listed below (my layout is attached as well).
I would like to be able to add two limiter switches that, when struck, will have the slider pause 5 seconds and then send it back in the opposite direction. So far I have had no success and am looking for help.
Feel free to explain things as if I am a child (although I am almost 50). Thanks in advance!
#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);
int spd = 1000; // The current speed in steps/second
int sign = 1; // Either 1, 0 or -1
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setSpeed(1000);
Serial.println("App started");
}
void loop()
{
char c;
if(Serial.available()) {
c = Serial.read();
if (c == 'f') { // forward
sign = 1;
Serial.println("Forward");
}
if (c == 'r') { // reverse
sign = -1;
Serial.println("Reverse");
}
if (c == 's') { // stop
sign = 0;
Serial.println("Stopped");
}
if (c == '1') { // Super slow
spd = 10;
Serial.println("Speed: 10");
}
if (c == '2') { // Slow
spd = 100;
Serial.println("Speed: 100");
}
if (c == '3') { // Medium
spd = 300;
Serial.println("Speed: 300");
}
if (c == '4') { // Fast
spd = 500;
Serial.println("Speed: 500");
}
if (c == '5') { // Faster
spd = 700;
Serial.println("Speed: 700");
}
if (c == '6') { // Super Fast
spd = 1000;
Serial.println("Speed: 1000");
}
stepper.setSpeed(sign * spd);
}
stepper.runSpeed();
}
Have a look at how I have reorganized your code into separate single-purpose functions. I reckon this will make it a lot more obvious how to add the extra capability that you want.
I have deliberately left the checkLimits() function empty so you can have a go at figuring out what to put in it.
#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);
int spd = 1000; // The current speed in steps/second
int sign = 1; // Either 1, 0 or -1
char c;
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setSpeed(1000);
Serial.println("App started");
}
void loop() {
readSerial();
checkLimits();
updateSpeedAndDirection();
moveMotor();
}
void checkLimits() {
}
void moveMotor() {
stepper.setSpeed(sign * spd);
stepper.runSpeed();
}
void readSerial() {
if(Serial.available()) {
c = Serial.read();
}
}
void updateSpeedAndDirection() {
if (c == 'f') { // forward
sign = 1;
Serial.println("Forward");
}
if (c == 'r') { // reverse
sign = -1;
Serial.println("Reverse");
}
if (c == 's') { // stop
sign = 0;
Serial.println("Stopped");
}
if (c == '1') { // Super slow
spd = 10;
Serial.println("Speed: 10");
}
if (c == '2') { // Slow
spd = 100;
Serial.println("Speed: 100");
}
if (c == '3') { // Medium
spd = 300;
Serial.println("Speed: 300");
}
if (c == '4') { // Fast
spd = 500;
Serial.println("Speed: 500");
}
if (c == '5') { // Faster
spd = 700;
Serial.println("Speed: 700");
}
if (c == '6') { // Super Fast
spd = 1000;
Serial.println("Speed: 1000");
}
}
I have deliberately made as few changes as possible to your code.
I have not tested this version so if it does not compile let me know.
Velcrobelly:
Thanks for the reply, however, I could not quite get your code to work.
With a little bit of experimenting, I was able to come up with a resolution, and I post it here for others to use.
Glad you found a solution.
If you are considering anything more complex, or are considering extending that program with additional capabilities it will pay to learn how to use functions.