Newbie question: Delay Rx Pin 4 and Pin 5

Newbie question: Delay Rx Pin 5 and after Pin 4

I want the motor on Pin 5 to start five seconds after the motor on Pin 4

Thank you.

This is the program on the Arduino Uno

#include <SoftwareSerial.h>
#define rxPin 3 // pin 3 connects to smcSerial TX (not used in this example)
#define txPin 4 // pin 4 connects to smcSerial RX
#define txPin 5 // pin 5 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);

// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
smcSerial.write(0x83);
}

// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
smcSerial.write(0x86); // motor reverse command
speed = -speed; // make speed positive
}
else
{
smcSerial.write(0x85); // motor forward command
}
smcSerial.write(speed & 0x1F);
smcSerial.write(speed >> 5 & 0x7F);
}

void setup()
{
// Initialize software serial object with baud rate of 19.2 kbps.
smcSerial.begin(19200);

// The Simple Motor Controller must be running for at least 1 ms
// delay here for 5 ms.
delay(5);

// If the Simple Motor Controller has automatic baud detection
// enabled, send the byte 0xAA (170 in decimal)
// so that it can learn the baud rate.
smcSerial.write(0xAA);

// Exit Safe Start command,
// clears the safe-start violation and lets the motor run.
exitSafeStart();
}

void loop()
{
setMotorSpeed(3200); // full-speed forward
delay(8000);
setMotorSpeed(-1000); // half-speed reverse
delay(4000);
setMotorSpeed(1000); // half-speed forward
delay(4000);
setMotorSpeed(-3200); // full-speed reverse
delay(10000);
setMotorSpeed(3200); // full-speed forward
delay(8000);
setMotorSpeed(-1000); // half-speed reverse
delay(4000);
setMotorSpeed(1000); // half-speed forward
delay(4000);
setMotorSpeed(-3200); // full-speed reverse
delay(10000);

PoloMotor1_modified_Pin4_5_Rx.ino (1.81 KB)

You only have one motor declared... not sure what motors you are using but you declare one using rx and tx pins, but never the second... make a second motor object with the pins the second motor is connected to, then simply insert delay(5000); between the execution of the first and second motors' commands.

What exactly is that you are driving? If you're talking about one of the Polulu Simple Motor Controllers then say which one.

You have given the same name rxpin to both pin 4 and pin 5. That's not going to work. If you are running two SMCs then you'll need separate SoftwareSerial connections for each.

Steve

I would remove all the delays and replace with the "blink without delay" (look at the "demonstration code for several things at the same time" Demonstration code for several things at the same time - Project Guidance - Arduino Forum at the top of this forum)

Delays "freeze" the arduino for the delay time so nothing else will run during that time. As an example, if you delay one motor the second motor will not receive any new commands until the first delay finishes.

Acuitive:
You only have one motor declared... not sure what motors you are using but you declare one using rx and tx pins, but never the second... make a second motor object with the pins the second motor is connected to, then simply insert delay(5000); between the execution of the first and second motors' commands.

Thank you for the replies.

Sorry about my lack of knowledge....

The the code I have on the Arduino Uno

#define txPin 4 // pin 4 connects to smcSerial RX
#define txPin 5 // pin 5 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);

I can run a motor driver "connected" to Pin 4 and another motor driver "connected" to Pin 5

Yes, I am connecting to a Pololu 1365 High-Power Simple Motor Controller G2 24v12

What I wanted the start of motors to be staggered.

The following connections exist between the Arduino and the Simple Motor Controller G2:

Arduino digital pin 4 to the first Simple Motor Controller's RX pin
Arduino digital pin 5 to the second Simple Motor Controller RX pin
(The RX pin is (from the Pololu manual): the RX/SDA pin acts as RX, the TTL serial receive pin, by default. This pin can be connected to the TTL serial output (transmit line) of another device. When I²C is enabled, this pin acts as SDA: an input and open-drain output for sending data. In serial mode, this pin has an internal pull-up resistor enabled, but in I²C mode it does not have any pull-up or pull-down resistors. The RX/SDA pin is 5V-tolerant and protected by a 220Ω series resistor.

and I also have these
Arduino GND to Simple Motor Controller GND

Again, thank you for the replies.

I don't really understand exactly what you're doing but here's my take on it: You want both motors to do the same thing, just spaced 5-seconds in time.

  • Check the RX and TX pin numbers
  • Compiled successfully for a 2560; YMMV
  • might be loaded with bugs. Prepare to pull your hair out...
  • have never used SoftwareSerial; can it do 19200?
#include <SoftwareSerial.h>

#define rxPin1      3  // pin 3 connects to smcSerial TX  (not used in this example)
#define txPin1      4  // pin 4 connects to smcSerial RX
#define rxPin2      5  // pin 5 connects to smcSerial TX
#define txPin2      6  // pin 6 connects to smcSerial RX

#define MOTOR_1     1
#define MOTOR_2     2

//states
#define MOTOR_INIT      0
#define MOTOR_STATE1    1
#define MOTOR_STATE2    2
#define MOTOR_STATE3    3
#define MOTOR_STATE4    4
#define MOTOR_STATE5    5
#define MOTOR_STATE6    6
#define MOTOR_STATE7    7

void ProcessMotorState( byte Motor, 
                         byte *stateVariable, 
                         unsigned long *timeLast, 
                         unsigned long *timeLimit );

byte
    stateMotor1,
    stateMotor2;
unsigned long
    timeMotor1,
    timeLimitMotor1,
    timeMotor2,
    timeLimitMotor2;
    
SoftwareSerial 
    smc1Serial = SoftwareSerial(rxPin1, txPin1);
SoftwareSerial    
    smc2Serial = SoftwareSerial(rxPin2, txPin2);
 
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
    smc1Serial.write(0x83);
    smc2Serial.write(0x83);
  
}//exitSafeStart
 
// speed should be a number from -3200 to 3200
void setMotorSpeed(byte motorNumber, int speed)
{  
    if (speed < 0)
    {
        if( motorNumber == MOTOR_1 )   
            smc1Serial.write(0x86);  // motor reverse command
        else
            smc2Serial.write(0x86);
        
        speed = -speed;  // make speed positive
    }
    else
    {
        if( motorNumber == MOTOR_1 )
            smc1Serial.write(0x85);  // motor forward command
        else
            smc2Serial.write(0x85);
    }//else
    if( motorNumber == MOTOR_1 )
    {  
        smc1Serial.write(speed & 0x1F);
        smc1Serial.write((speed >> 5) & 0x7F);
    }//if
    else
    {
        smc2Serial.write(speed & 0x1F);
        smc2Serial.write((speed >> 5) & 0x7F);        
    }//else
    
}//setMotorSpeed
 
void setup()
{
    // Initialize software serial object with baud rate of 19.2 kbps.
    smc1Serial.begin(19200);
    smc2Serial.begin(19200);
 
    // The Simple Motor Controller must be running for at least 1 ms
    // delay here for 5 ms.
    delay(5);
 
    // If the Simple Motor Controller has automatic baud detection
    // enabled, send the byte 0xAA (170 in decimal)
    // so that it can learn the baud rate.
    smc1Serial.write(0xAA);
    smc2Serial.write(0xAA);
 
    // Exit Safe Start command, 
    // clears the safe-start violation and lets the motor run.
    exitSafeStart();

    stateMotor1 = MOTOR_INIT;
    stateMotor2 = MOTOR_INIT;
    timeMotor1 = millis();
    timeMotor2 = timeMotor1;
    timeLimitMotor1 = 100;  //start in 100mS
    timeLimitMotor2 = 5100; //starts 5-seconds after the 1st
    
}//setup
 
void loop()
{
    ProcessMotorState(MOTOR_1, &stateMotor1, &timeMotor1, &timeLimitMotor1);
    ProcessMotorState(MOTOR_2, &stateMotor2, &timeMotor2, &timeLimitMotor2);

}//loop

void ProcessMotorState( byte Motor, 
                        byte *stateVariable, 
                        unsigned long *timeLast, 
                        unsigned long *timeLimit )
{
    unsigned long
        timeNow;
        
    timeNow = millis();
    
    if( (timeNow - *timeLast) < *timeLimit )
        return;
        
    *timeLast = timeNow;

    switch( *stateVariable )
    {
        case    MOTOR_INIT:
            setMotorSpeed(Motor, 3200);  // full-speed forward
            *timeLimit = 8000L;
            *stateVariable++;
        break;

        case    MOTOR_STATE1:
            setMotorSpeed(Motor, -1000);
            *timeLimit = 4000L;
            *stateVariable++;
        break;
            
        case    MOTOR_STATE2:
            setMotorSpeed(Motor, 1000);
            *timeLimit = 4000L;
            *stateVariable++;
        break;

        case    MOTOR_STATE3:
            setMotorSpeed(Motor, -3200);
            *timeLimit = 10000L;
            *stateVariable++;
        break;

        case    MOTOR_STATE4:
            setMotorSpeed(Motor, 3200);
            *timeLimit = 8000L;
            *stateVariable++;
        break;

        case    MOTOR_STATE5:
            setMotorSpeed(Motor, -1000);
            *timeLimit = 4000L;
            *stateVariable++;
        break;

        case    MOTOR_STATE6:
            setMotorSpeed(Motor, 1000);
            *timeLimit = 4000L;
            *stateVariable++;
        break;

        case    MOTOR_STATE7:
            setMotorSpeed(Motor, -3200);
            *timeLimit = 10000L;
            *stateVariable = MOTOR_INIT;
        break;

        default:
            *timeLimit = 0;
            *stateVariable = MOTOR_INIT;
        break;

    }//switch
     
}//ProcessMotorState

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

gam7:
Thank you for the replies.

Sorry about my lack of knowledge....

#define txPin 4 // pin 4 connects to smcSerial RX
#define txPin 5 // pin 5 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);

I can run a motor driver "connected" to Pin 4 and another motor driver "connected" to Pin 5

Yes, I am connecting to a Pololu 1365 High-Power Simple Motor Controller G2 24v12

What I wanted the start of motors to be staggered.

The following connections exist between the Arduino and the Simple Motor Controller G2:

Arduino digital pin 4 to the first Simple Motor Controller's RX pin
Arduino digital pin 5 to the second Simple Motor Controller RX pin
(The RX pin is (from the Pololu manual): the RX/SDA pin acts as RX, the TTL serial receive pin, by default. This pin can be connected to the TTL serial output (transmit line) of another device. When I²C is enabled, this pin acts as SDA: an input and open-drain output for sending data. In serial mode, this pin has an internal pull-up resistor enabled, but in I²C mode it does not have any pull-up or pull-down resistors. The RX/SDA pin is 5V-tolerant and protected by a 220Ω series resistor.

and I also have these
Arduino GND to Simple Motor Controller GND

Again, thank you for the replies.

Thank you. Your program works! Interestingly, only Pin 4 (motor 1) and Pin 6 (motor 3) works. Motor 2, on Pin 5 does not work.

Also, the speed change and direction change does not work

To get the Pin 5 (motor 2 to work)

I used your code (loaded on the Arduino Uno)

#define txPin1 4 // pin 4 connects to smcSerial RX
#define rxPin2 5 // pin 5 connects to smcSerial TX

Because txPin1 works,

If I change rxPin2 to txPin2 as in

#define txPin2 5 // pin 5 connects to smcSerial RX

I get this error

rxPin2' was not declared in this scope

Please help!

Full error in attachment.

Again, thank you.

full error Pin 5 motor 2 does not work.txt (8.51 KB)

hank you. Your program works! Interestingly, only Pin 4 (motor 1) and Pin 6 (motor 3) works. Motor 2, on Pin 5 does not work.

Also, the speed change and direction change does not work

To get the Pin 5 (motor 2 to work)

Three motors? I thought there was only two. Speed change and direction doesn't work?

You say it works but then describe what sounds like it not working. :confused:

Can you post your entire code here (pls use code tags...) and describe what works and what doesn't?

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Sorry about that. I will format correctly in the future. Attached pictures of the project. Thank you

Pictures of project.pdf (387 KB)

Blackfin:
Three motors? I thought there was only two. Speed change and direction doesn't work?

You say it works but then describe what sounds like it not working. :confused:

Can you post your entire code here (pls use code tags...) and describe what works and what doesn't?

Sorry, it is three motors. I thought once I have two working, the third one, I could fumble my way through.

Also, the program you sent, motor speeds do not change, nor the direction. I have attached the program (attachment 1) where the speed and direction does change...but only for one motor. I am trying to get three to change speed and direction.

Again, thank you for the help.

PoloMotor1.ino (1.84 KB)

Pictures of project.pdf (387 KB)

sketch_from_forum.ino (4.85 KB)

Not clear why you do this:

#define rxPin1      3  // pin 3 connects to smcSerial TX  (not used in this example)
#define txPin1      4  // pin 4 connects to smcSerial RX
#define txPin2      5  // pin 5 connects to smcSerial RX
#define txPin2      6  // pin 6 connects to smcSerial RX

(i.e. define txPin2 twice...)

What does the system do when this is running? Do they both start at full speed in "forward", #2 5-seconds after #1 but they don't change speed or direction? They just keep running?

I added some lines of serial debug messages to the code and saw that it was never leaving INIT state for either motor. I made a change from:

*stateVariable++;

to

*stateVariable = MOTOR_xxxx; //xxxx is the next desired state

and it seemed to walk through the states at the right time. Give this a try:

#include <SoftwareSerial.h>

#define rxPin1      3  // pin 3 connects to smcSerial TX  (not used in this example)
#define txPin1      4  // pin 4 connects to smcSerial RX
#define txPin2      5  // pin 5 connects to smcSerial RX
#define rxPin2      6  // pin 6 connects to smcSerial RX

#define MOTOR_1     1
#define MOTOR_2     2

//states
#define MOTOR_INIT      0
#define MOTOR_STATE1    1
#define MOTOR_STATE2    2
#define MOTOR_STATE3    3
#define MOTOR_STATE4    4
#define MOTOR_STATE5    5
#define MOTOR_STATE6    6
#define MOTOR_STATE7    7

void ProcessMotorState( byte Motor, 
                         byte *stateVariable, 
                         unsigned long *timeLast, 
                         unsigned long *timeLimit );

byte
    stateMotor1,
    stateMotor2;
unsigned long
    timeMotor1,
    timeLimitMotor1,
    timeMotor2,
    timeLimitMotor2;
    
SoftwareSerial 
    smc1Serial = SoftwareSerial(rxPin1, txPin1);
SoftwareSerial    
    smc2Serial = SoftwareSerial(rxPin2, txPin2);
 
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
    smc1Serial.write(0x83);
    smc2Serial.write(0x83);
  
}//exitSafeStart
 
// speed should be a number from -3200 to 3200
void setMotorSpeed(byte motorNumber, int speed)
{  
    if (speed < 0)
    {
        if( motorNumber == MOTOR_1 )   
            smc1Serial.write(0x86);  // motor reverse command
        else
            smc2Serial.write(0x86);
        
        speed = -speed;  // make speed positive
    }
    else
    {
        if( motorNumber == MOTOR_1 )
            smc1Serial.write(0x85);  // motor forward command
        else
            smc2Serial.write(0x85);
    }//else
    
    if( motorNumber == MOTOR_1 )
    {
        //fedcba9876543210
        //           11111
        //xxxxxfedcba98765
        //        01111111
        //
        //speed is stored in 0bxxx c ba98 7654 3210
        //0x1fff = 8191
        smc1Serial.write(speed & 0x1F);
        smc1Serial.write((speed >> 5) & 0x7F);
    }//if
    else
    {
        smc2Serial.write(speed & 0x1F);
        smc2Serial.write((speed >> 5) & 0x7F);        
    }//else
    
}//setMotorSpeed
 
void setup()
{
    Serial.begin(9600);
    while(!Serial);
    
    // Initialize software serial object with baud rate of 19.2 kbps.
    smc1Serial.begin(19200);
    smc2Serial.begin(19200);
 
    // The Simple Motor Controller must be running for at least 1 ms
    // delay here for 5 ms.
    delay(5);
 
    // If the Simple Motor Controller has automatic baud detection
    // enabled, send the byte 0xAA (170 in decimal)
    // so that it can learn the baud rate.
    smc1Serial.write(0xAA);
    smc2Serial.write(0xAA);
 
    // Exit Safe Start command, 
    // clears the safe-start violation and lets the motor run.
    exitSafeStart();

    stateMotor1 = MOTOR_INIT;
    stateMotor2 = MOTOR_INIT;
    timeMotor1 = millis();
    timeMotor2 = timeMotor1;
    timeLimitMotor1 = 100;  //start in 100mS
    timeLimitMotor2 = 5100; //starts 5-seconds after the 1st
    
}//setup
 
void loop()
{
    ProcessMotorState(MOTOR_1, &stateMotor1, &timeMotor1, &timeLimitMotor1);
    ProcessMotorState(MOTOR_2, &stateMotor2, &timeMotor2, &timeLimitMotor2);

}//loop

void ProcessMotorState( byte Motor, 
                        byte *stateVariable, 
                        unsigned long *timeLast, 
                        unsigned long *timeLimit )
{
    unsigned long
        timeNow;
        
    timeNow = millis();
    
    if( (timeNow - *timeLast) < *timeLimit )
        return;
        
    *timeLast = timeNow;

    switch( *stateVariable )
    {
        case    MOTOR_INIT:
            Serial.print("INIT Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 3200);  // full-speed forward
            *timeLimit = 8000L;
            *stateVariable = MOTOR_STATE1;
        break;

        case    MOTOR_STATE1:
            Serial.print("STATE1 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -1000);
            *timeLimit = 4000L;
            *stateVariable = MOTOR_STATE2;
        break;
            
        case    MOTOR_STATE2:
            Serial.print("STATE2 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 1000);
            *timeLimit = 4000L;
            *stateVariable = MOTOR_STATE3;
        break;

        case    MOTOR_STATE3:
            Serial.print("STATE3 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -3200);
            *timeLimit = 10000L;
            *stateVariable = MOTOR_STATE4;
        break;

        case    MOTOR_STATE4:
            Serial.print("STATE4 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 3200);
            *timeLimit = 8000L;
            *stateVariable = MOTOR_STATE5;
        break;

        case    MOTOR_STATE5:
            Serial.print("STATE5 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -1000);
            *timeLimit = 4000L;
            *stateVariable = MOTOR_STATE6;
        break;

        case    MOTOR_STATE6:
            Serial.print("STATE6 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 1000);
            *timeLimit = 4000L;
            *stateVariable = MOTOR_STATE7;
        break;

        case    MOTOR_STATE7:
            Serial.print("STATE7 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -3200);
            *timeLimit = 10000L;
            *stateVariable = MOTOR_INIT;
        break;

        default:
            *timeLimit = 0;
            *stateVariable = MOTOR_INIT;
        break;

    }//switch
     
}//ProcessMotorState

Problem was that the expression:

*stateVariable++;

does one thing while

(*stateVariable)++;

does another (and the desired) thing.

Acuitive:
You only have one motor declared... not sure what motors you are using but you declare one using rx and tx pins, but never the second... make a second motor object with the pins the second motor is connected to, then simply insert delay(5000); between the execution of the first and second motors' commands.

Thank you for the reply.
Please let me know if this makes sense...

#include <SoftwareSerial.h>
#define rxPin 3 // pin 3 connects to smcSerial TX (not used in this example)
#define txPin 5 // pin 5 connects to smcSerial RX
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);

// required to allow motors to move
// must be called when controller restarts and after any error

Motor motor1; // Define motor 1
Motor motor2; // Define motor 2
Motor motor3; // Define motor 3

// Is this what you are suggesting “make a second motor object with the pins the
// second motor is connected to”
// Also, I have three motors.

void setup() {
motor1.attach(10); // Set motor 1 to digital pin 10
motor2.attach(9); // Set motor 2 to digital pin 9
motor3.attach(8); // Set motor 3 to digital pin 8
}

// Is this correct?

Again, thank you.

Blackfin:
Not clear why you do this:

#define rxPin1      3  // pin 3 connects to smcSerial TX  (not used in this example)

#define txPin1      4  // pin 4 connects to smcSerial RX
#define txPin2      5  // pin 5 connects to smcSerial RX
#define txPin2      6  // pin 6 connects to smcSerial RX




(i.e. define txPin2 twice...)

What does the system do when this is running? Do they both start at full speed in "forward", #2 5-seconds after #1 but they don't change speed or direction? They just keep running?

Thank you. I am sorry, that should have said

#include <SoftwareSerial.h>
#define rxPin 3  // pin 3 connects to smcSerial TX  (not used in this example)
#define txPin 4  // pin 4 connects to smcSerial RX
#define txPin 5  // pin 5 connects to smcSerial RX
#define txPin 6  // pin 6 connects to smcSerial RX

When I run this only Pin 4 (motor 1) runs. I tested the other motors, with the code copied below. Individually and they run.

#define txPin 5  // pin 5 connects to smcSerial RX
#define txPin 6  // pin 6 connects to smcSerial RX

I cannot get all three to run and do the speed up- speed down and reverse.

Again, thank you.

Pictures of project.pdf (387 KB)

gam7:
Thank you. I am sorry, that should have said

#include <SoftwareSerial.h>

#define rxPin 3  // pin 3 connects to smcSerial TX  (not used in this example)
#define txPin 4  // pin 4 connects to smcSerial RX
#define txPin 5  // pin 5 connects to smcSerial RX
#define txPin 6  // pin 6 connects to smcSerial RX




When I run this only Pin 4 (motor 1) runs. I tested the other motors, with the code copied below. Individually and they run.


#define txPin 5  // pin 5 connects to smcSerial RX






#define txPin 6  // pin 6 connects to smcSerial RX




I cannot get all three to run and do the speed up- speed down and reverse.

Again, thank you.

Sorry, this still doesn't make sense. You keep using the same name, txPin over and over. Given what you have there I would expect the motor connected to pin 6 to be the one to run, not 4.

You need to have unique identifier names for each txPin; txPin1 for motor 1, txPin2 for motor 2 and txPin3 for motor 3.

What does this do for all three?

#include <SoftwareSerial.h>

const int rxPinDummy = 3;
const int txPin1 = 4;
const int txPin2 = 5;
const int txPin3 = 6;

#define MOTOR_1     1
#define MOTOR_2     2
#define MOTOR_3     3

//states
#define MOTOR_INIT      0
#define MOTOR_STATE1    1
#define MOTOR_STATE2    2
#define MOTOR_STATE3    3
#define MOTOR_STATE4    4
#define MOTOR_STATE5    5
#define MOTOR_STATE6    6
#define MOTOR_STATE7    7

void ProcessMotorState( byte Motor, 
                         byte *stateVariable, 
                         unsigned long *timeLast, 
                         unsigned long *timeLimit );

byte
    stateMotor1,
    stateMotor2,
    stateMotor3;
unsigned long
    timeMotor1,
    timeLimitMotor1,
    timeMotor2,
    timeLimitMotor2,
    timeMotor3,
    timeLimitMotor3;
    
SoftwareSerial 
    smc1Serial = SoftwareSerial(rxPinDummy, txPin1);
SoftwareSerial    
    smc2Serial = SoftwareSerial(rxPinDummy, txPin2);
SoftwareSerial    
    smc3Serial = SoftwareSerial(rxPinDummy, txPin3);

// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
    smc1Serial.write(0x83);
    smc2Serial.write(0x83);
    smc3Serial.write(0x83);
    
}//exitSafeStart
 
// speed should be a number from -3200 to 3200
void setMotorSpeed( byte motorNumber, int speed )
{  
    byte
        low,
        high;

    if( speed < -3200 )
        speed = -3200;
    if( speed > 3200 )
        speed = 3200;
        
    if (speed < 0)
    {
        if( motorNumber == MOTOR_1 )   
            smc1Serial.write(0x86);  // motor reverse command
        else if( motorNumber == MOTOR_2 )
            smc2Serial.write(0x86);
        else
            smc3Serial.write(0x86);
        
        speed = -speed;  // make speed positive
    }
    else
    {
        if( motorNumber == MOTOR_1 )   
            smc1Serial.write(0x85);  // motor reverse command
        else if( motorNumber == MOTOR_2 )
            smc2Serial.write(0x85);
        else
            smc3Serial.write(0x85);
        
    }//else

    low = speed & 0x1f;
    high = (speed >> 5) & 0x7f;
    
    if( motorNumber == MOTOR_1 )
    {
        smc1Serial.write(low);
        smc1Serial.write(high);
    }//if
    else if( motorNumber == MOTOR_2 )
    {
        smc2Serial.write(low);
        smc2Serial.write(high);        
    }//else
    else
    {
        smc3Serial.write(low);
        smc3Serial.write(high);                    
    }//else
    
}//setMotorSpeed
 
void setup()
{
    Serial.begin(9600);
    while(!Serial);
    
    // Initialize software serial object with baud rate of 19.2 kbps.
    smc1Serial.begin(19200);
    smc2Serial.begin(19200);
    smc3Serial.begin(19200);
 
    // The Simple Motor Controller must be running for at least 1 ms
    // delay here for 5 ms.
    delay(5);
 
    // If the Simple Motor Controller has automatic baud detection
    // enabled, send the byte 0xAA (170 in decimal)
    // so that it can learn the baud rate.
    smc1Serial.write(0xAA);
    smc2Serial.write(0xAA);
    smc3Serial.write(0xAA);
 
    // Exit Safe Start command, 
    // clears the safe-start violation and lets the motor run.
    exitSafeStart();

    stateMotor1 = MOTOR_INIT;
    stateMotor2 = MOTOR_INIT;
    stateMotor3 = MOTOR_INIT;
    timeMotor1 = millis();
    timeMotor2 = timeMotor1;
    timeMotor3 = timeMotor1;
    timeLimitMotor1 = 100L;  //start in 100mS
    timeLimitMotor2 = 5100L; //starts 5-seconds after the 1st
    timeLimitMotor3 = 10100L; //starts 5-seconds after the 2nd
    
}//setup
 
void loop()
{
    ProcessMotorState(MOTOR_1, &stateMotor1, &timeMotor1, &timeLimitMotor1);
    ProcessMotorState(MOTOR_2, &stateMotor2, &timeMotor2, &timeLimitMotor2);
    ProcessMotorState(MOTOR_3, &stateMotor3, &timeMotor3, &timeLimitMotor3);

}//loop

void ProcessMotorState( byte Motor, 
                        byte *stateVariable, 
                        unsigned long *timeLast, 
                        unsigned long *timeLimit )
{
    unsigned long
        timeNow;
        
    timeNow = millis();
    
    if( (timeNow - *timeLast) < *timeLimit )
        return;
        
    *timeLast = timeNow;

    switch( *stateVariable )
    {
        case    MOTOR_INIT:
            //Serial.print("INIT Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 3200);  // full-speed forward
            *timeLimit = 8000L;
            (*stateVariable)++;
        break;

        case    MOTOR_STATE1:
            //Serial.print("STATE1 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -1000);
            *timeLimit = 4000L;
            (*stateVariable)++;
        break;
            
        case    MOTOR_STATE2:
            //Serial.print("STATE2 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 1000);
            *timeLimit = 4000L;
            (*stateVariable)++;
        break;

        case    MOTOR_STATE3:
            //Serial.print("STATE3 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -3200);
            *timeLimit = 10000L;
            (*stateVariable)++;
        break;

        case    MOTOR_STATE4:
            //Serial.print("STATE4 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 3200);
            *timeLimit = 8000L;
            (*stateVariable)++;
        break;

        case    MOTOR_STATE5:
            //Serial.print("STATE5 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -1000);
            *timeLimit = 4000L;
            (*stateVariable)++;
        break;

        case    MOTOR_STATE6:
            //Serial.print("STATE6 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, 1000);
            *timeLimit = 4000L;
            (*stateVariable)++;
        break;

        case    MOTOR_STATE7:
            //Serial.print("STATE7 Motor "); Serial.println(Motor);
            setMotorSpeed(Motor, -3200);
            *timeLimit = 10000L;
            *stateVariable = MOTOR_INIT;
        break;

        default:
            *timeLimit = 0;
            *stateVariable = MOTOR_INIT;
        break;

    }//switch
     
}//ProcessMotorState