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