Hello everyone,
I hope that someone can help me to figure out what is wrong with my little stepper motors project.
I would like to control via bluetooth 3 steppers BYJ48 (ULN2003 driver).
What I would like them to do is:
- first motor to rotate clockwise when data '1' is received and counterclockwise when data '2' is received;
- second motor to rotate clockwise when data '3' is received and counterclockwise when data '4' is received;
- third motor to rotate clockwise when data '5' is received and counterclockwise when data '6' is received;
I am using the #include <Stepper.h> library
I am testing with the app ArduDroid to send the bluetooth data.
I have two problems:
- when I send any data the first motor myStepper1 always rotates forward, but I would like it to rotate only if I send the data '1' (for forward rotation) and '2' for backward rotation. In addition when I want to rotate another motor this myStepper1 motor always rotate first. For example if I send the data '3' the myStepper1 rotates first and then also myStepper2 rotates;
- all the motors just rotate clockwise even if I send the data to rotate them counterclockwise
I think there is a problem with the code.
Here it is:
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper1(stepsPerRevolution, 5, 4, 3, 2); //definisce i pin per i motori
Stepper myStepper2(stepsPerRevolution, 9, 8, 7, 6);
Stepper myStepper3(stepsPerRevolution, 13, 12, 11, 10);
float delay_time;
char state =0;
void setup()
{
Serial.begin(9600); //bus rate
myStepper1.setSpeed(60); //velocità motori
myStepper2.setSpeed(60);
myStepper3.setSpeed(60);
}
void loop()
{
if(Serial.available()>0) // controlla se c'è dato bluetooth
{
state=Serial.read();
}
if(state== '1')
{
myStepper1.step(stepsPerRevolution);
delay(20);
}
if(state == '2')
{
myStepper1.step(-stepsPerRevolution);
delay(20);
}
if(state == '3')
{
myStepper2.step(stepsPerRevolution);
delay(20);
}
if(state == '4')
{
myStepper2.step(-stepsPerRevolution);
delay(20);
}
if(state == '5')
{
myStepper3.step(stepsPerRevolution);
delay(20);
}
if(state == '6')
{
myStepper3.step(-stepsPerRevolution);
delay(20);
}
}
Thank you in advance.
Bye
Pietro