Hi, I am a beginner in Arduino and I am trying to control a stepper serially. Like sending value of steps and speed through serial monitor, so that i can change while working. This is the program i wrote, which does not work and I can't find a reason. It will be great if someone can help me with it. Thank you!
#include <Stepper.h>
// change this to the number of steps on your motor
int steps;
int Speed;
Stepper stepper(100, 4, 5, 6, 7);
void setup()
{
while (!Serial);
Serial.begin(9600);
Serial.println("Motor Speed? "); //Prompt User for Input
while (Serial.available()==0){ } //Wait for User Input
Speed = Serial.parseInt(); //Read User Input
Serial.println("Steps? "); //Prompt User for Input
while (Serial.available()==0){ } //Wait for Input
steps = Serial.parseInt(); //Read User Input
#include <Stepper.h>
// change this to the number of steps on your motor
int steps;
int Speed;
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(100, 4, 5, 6, 7);
void setup()
{
while (!Serial);
Serial.begin(9600);
Serial.println("Motor Speed? "); //Prompt User for Input
while (Serial.available()==0){ } //Wait for User Input
Speed = Serial.parseInt(); //Read User Input
Serial.println("Steps? "); //Prompt User for Input
while (Serial.available()==0){ } //Wait for Input
steps = Serial.parseInt(); //Read User Input
stepper.setSpeed(Speed);
}
void loop()
{
stepper.step(steps);
}
The code does something. You have not explained what it actually does.
You expect it to do something. It is not clear what you expect it to do.
What is strange is that you take the same number of steps on every iteration of loop(), after reading the speed and number of steps one time. If you expect the stepper to stop after stepping the required number of times, your expectations are wrong.
Thanks for reply. This program doesn't do anything. I expect it to work like if i put 60rpm in speed and 100 in steps by serially, my motor should run at those values.
First get your stepper running with one of the example sketches. I recommend File -> Examples -> Stepper -> stepper_oneRevolution If that doesn't make your stepper run forward one revolution and backward one revolution, repeatedly, you have it wired wrong and that is something you have to fix first.
void loop() {
while (Serial.available()) {
char input = Serial.read();
if (input == "s") {
int speed = Serial.parseInt();
if (speed > 0 && speed < 200)
myStepper.setSpeed(speed);
}
That will change the step speed (RPM) when you send 's' followed by a number. Values less than 1 or greater than 200 are ignored. Of course at slow speeds the rotation will take a long time and during that time there is no opportunity to read the serial port. Rest assured that the input will be read when the movement ends a cycle. You can use other letters to signal other commands.