Problem with two serial inputs at same time.

Dear all,

I would like to rotate two stepper motors at a same time and I should able to stop or start either motor. How would I communicate with Arduino UNO R3 to resolve this issue. I pest my code as below. Please kindly help.

----------------------------------------------------Program---------------------------------------------------------------------------------------------------

#include <Stepper.h>

#define command_clockwise 'a'
#define command_anticlockwise 'd'
#define command_upword 'w'
#define command_downword 's'
#define command_azim_stop 'x'
#define command_elvn_stop 'y'
#define command_stop_antanna ' '

int stepsInRev = 200;

int num_of_steps = 1;

Stepper myStepper1(stepsInRev, 4, 5, 6, 7);
Stepper myStepper2(stepsInRev, 8, 9, 10, 11);

char lastCall = ' ';

void clockwiseStep(int steps){
Serial.println("clockwise");
// step one step each clockwise
myStepper1.step(1);
delay(10);
}

void anticlockwiseStep(int steps){
Serial.println("anticlockwise");
// step one step each anticlockwise
myStepper1.step(-1);
delay(10);
}

void m1Stop(){
Serial.println("stop");
//motor-1 stop
PORTD = B00000000; //sets all of the pins 0 to 7 as LOW to power off stepper1
}
void upwordStep(int steps){
Serial.println("upword");
myStepper2.step(1);
delay(10);
}
void downwordStep(int steps){
Serial.println("downword");
// step one step each backward
myStepper2.step(-1);
delay(10);
}
void m2Stop(){
Serial.println("stop");
//motor-2 stop
PORTB = B00000000; //sets all of the pins 8 to 13 as LOW to power off stepper1
}
void allStop(){
Serial.println("stop");
// steppers stop
PORTD = B00000000; //sets all of the pins 0 to 7 as LOW to power off stepper1
PORTB = B00000000; //sets all of the pins 8 to 13 as LOW to power off stepper2
}

void setup() {
Serial.begin(9600);//start the bluetooth serial port - send and recieve at 9600 baud
// set the speed at 60 rpm:
myStepper1.setSpeed(60);
myStepper2.setSpeed(60);
}

void loop() {
if(Serial.available()) {
char data = (char) Serial.read();
switch(data) {
case command_clockwise:
clockwiseStep (num_of_steps);
break;

case command_anticlockwise:
anticlockwiseStep (num_of_steps);
break;

case command_upword:
upwordStep (num_of_steps);
break;

case command_downword:
downwordStep (num_of_steps);
break;

case command_azim_stop:
m1Stop();
break;

case command_elvn_stop:
m2Stop();
break;

case command_stop_antanna:
allStop();
break;
}

lastCall = data;
}

else{

char data = lastCall;
switch(data) {

case command_clockwise:
clockwiseStep (num_of_steps);
break;

case command_anticlockwise:
anticlockwiseStep (num_of_steps);
break;

case command_upword:
upwordStep (num_of_steps);
break;

case command_downword:
downwordStep (num_of_steps);
break;

case command_azim_stop:
m1Stop();
break;

case command_elvn_stop:
m2Stop();
break;

case command_stop_antanna:
allStop();
break;
}
lastCall = data;

}

}

---------------------------------------------------Program End------------------------------------------------------------------------

Here only one loop is going to execute at a time how would I execute two loops at a time to start and stop either of stepper motor.

I'm beginner with Arduino and C++ .
Please kindly help.

I'm very much thankful to you.

How fast do you think the loop is running compared to your human ability to send commands?
Use of delay() when you have timing concerns is usually a bad idea (see the example "Blink Without Delay")

PS: Please read how to use the forum and use [code]// your code here[/code] tags to post your code, should look like this// your code here

How would I communicate with Arduino UNO R3 to resolve this issue

You need to send more data over the Serial link to identify which motor you wish to send the command to.

For instance, sending "0a" could command one motor to turn clockwise whilst "1a" could command the other one. You need a reliable method of parsing the incoming command into its parts to determine which motor and which command is being requested.

See Serial input basics - updated for examples.

So how would I send more than one command ?

gopes:
So how would I send more than one command ?

That depends on how you code it. The format of the command string is up to you.

"<0a,1a>" could mean both motors clockwise
"<0a,1d>" motor 0 clockwise, motor 1 anticlockwise
"<1a>" motor 1 clockwise, no change to motor 0

etc

If you wanted to you could also include the number of steps to move each motor in the message

It would be easier for the Arduino program if you just send two numbers - for example <-100, 87> would mean that the first motor would move 100 steps counter-clockwise and the second motor would move 87 steps clockwise.

...R

Unfortunately he currently appears to have 7 discrete commands

UKHeliBob:
Unfortunately he currently appears to have 7 discrete commands

That may be the result of not understanding alternative ways of doing things.

...R

Dear all,

Anybody can please edit same code. I am not much familiar with programming.
Please help.

Thanks

gopes:
Anybody can please edit same code. I am not much familiar with programming.
Please help.

Have you tried the code in the link in Reply #2?

If you want someone to write code for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

...R

Read the link in reply #2