two devices on one serial line, softwareserial

I am trying to control two dc motors using two pololu simple high-power motor controller (http://www.pololu.com/catalog/product/1377) with their serial ttl interface. I have the code to control one of them, but how do i control both of them at the same time? I am connecting them to the arduino duemilanove. the controllers have an internal txin to daisy chain multiple controllers.

#include <NewSoftSerial.h>
#define rxPin 10 // pin 10 connects to SMC TX
#define txPin 9 // pin 9 connects to SMC RX
#define resetPin 11 // pin 11 connects to SMC nRST
#define errPin 12 // pin 12 connects to SMC ERR
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// some variable IDs
#define ERROR_STATUS 0
#define LIMIT_STATUS 3
#define TARGET_SPEED 1000
#define INPUT_VOLTAGE 23
#define TEMPERATURE 24
// some motor limit IDs
#define FORWARD_ACCELERATION 0
#define REVERSE_ACCELERATION 0
#define DECELERATION 0
// read a serial byte (returns -1 if nothing received after 500 milliseconds)
int readByte()
{
unsigned long time = millis();
while (!mySerial.available())
{
if (millis() - time > 500)
return -1;
}
return mySerial.read();
}
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}
unsigned char setMotorLimit(unsigned char limitID, unsigned int limitValue)
{
mySerial.print(0xA2, BYTE);
mySerial.print(limitID, BYTE);
mySerial.print((unsigned char)(limitValue & 0x7F), BYTE);
mySerial.print((unsigned char)(limitValue >> 7), BYTE);
return readByte();
}
// returns the specified variable as an unsigned integer.
// if the requested variable is signed, the value returned by this function
// should be typecast as an int.
unsigned int getVariable(unsigned char variableID)
{
mySerial.print(0xA1, BYTE);
mySerial.print(variableID, BYTE);
return readByte() + 256 * readByte();
}
void setup()
{
Serial.begin(9600); // for debugging (optional)
mySerial.begin(38400);
// briefly reset SMC when Arduino starts up (optional)
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, LOW); // reset SMC
delay(1); // wait 1 ms
pinMode(resetPin, INPUT); // let SMC run again
// must wait at least 1 ms after reset before transmitting
delay(5);
// this lets us read the state of the SMC ERR pin (optional)
pinMode(errPin, INPUT);
mySerial.print(0xAA, BYTE); // send baud-indicator byte
setMotorLimit(FORWARD_ACCELERATION, 4);
setMotorLimit(REVERSE_ACCELERATION, 10);
setMotorLimit(DECELERATION, 20);
// clear the safe-start violation and let the motor run
exitSafeStart();
}
void loop()
{
setMotorSpeed(3200); // full-speed forward
// signed variables must be cast to ints:
Serial.println((int)getVariable(TARGET_SPEED));
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
Serial.println((int)getVariable(TARGET_SPEED));
delay(1000);
// print input voltage (in millivolts) to the serial monitor
Serial.print("VIN = ");
Serial.print(getVariable(INPUT_VOLTAGE));
Serial.println(" mV");
// if an error is stopping the motor, print the error status variable
// and try to re-enable the motor
if (digitalRead(errPin) == HIGH)
{
Serial.print("Error Status: ");
Serial.println(getVariable(ERROR_STATUS));
// once all other errors have been fixed,
// this lets the motors run again
exitSafeStart();
}
}

alphamike88:
I am trying to control two dc motors using two pololu simple high-power motor controller (http://www.pololu.com/catalog/product/1377) with their serial ttl interface. I have the code to control one of them, but how do i control both of them at the same time? I am connecting them to the arduino duemilanove. the controllers have an internal txin to daisy chain multiple controllers.

#include <NewSoftSerial.h>

#define rxPin 10 // pin 10 connects to SMC TX
#define txPin 9 // pin 9 connects to SMC RX
#define resetPin 11 // pin 11 connects to SMC nRST
#define errPin 12 // pin 12 connects to SMC ERR
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// some variable IDs
#define ERROR_STATUS 0
#define LIMIT_STATUS 3
#define TARGET_SPEED 1000
#define INPUT_VOLTAGE 23
#define TEMPERATURE 24
// some motor limit IDs
#define FORWARD_ACCELERATION 0
#define REVERSE_ACCELERATION 0
#define DECELERATION 0
// read a serial byte (returns -1 if nothing received after 500 milliseconds)
int readByte()
{
unsigned long time = millis();
while (!mySerial.available())
{
if (millis() - time > 500)
return -1;
}
return mySerial.read();
}
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}
unsigned char setMotorLimit(unsigned char limitID, unsigned int limitValue)
{
mySerial.print(0xA2, BYTE);
mySerial.print(limitID, BYTE);
mySerial.print((unsigned char)(limitValue & 0x7F), BYTE);
mySerial.print((unsigned char)(limitValue >> 7), BYTE);
return readByte();
}
// returns the specified variable as an unsigned integer.
// if the requested variable is signed, the value returned by this function
// should be typecast as an int.
unsigned int getVariable(unsigned char variableID)
{
mySerial.print(0xA1, BYTE);
mySerial.print(variableID, BYTE);
return readByte() + 256 * readByte();
}
void setup()
{
Serial.begin(9600); // for debugging (optional)
mySerial.begin(38400);
// briefly reset SMC when Arduino starts up (optional)
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, LOW); // reset SMC
delay(1); // wait 1 ms
pinMode(resetPin, INPUT); // let SMC run again
// must wait at least 1 ms after reset before transmitting
delay(5);
// this lets us read the state of the SMC ERR pin (optional)
pinMode(errPin, INPUT);
mySerial.print(0xAA, BYTE); // send baud-indicator byte
setMotorLimit(FORWARD_ACCELERATION, 4);
setMotorLimit(REVERSE_ACCELERATION, 10);
setMotorLimit(DECELERATION, 20);
// clear the safe-start violation and let the motor run
exitSafeStart();
}
void loop()
{
setMotorSpeed(3200); // full-speed forward
// signed variables must be cast to ints:
Serial.println((int)getVariable(TARGET_SPEED));
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
Serial.println((int)getVariable(TARGET_SPEED));
delay(1000);
// print input voltage (in millivolts) to the serial monitor
Serial.print(“VIN = “);
Serial.print(getVariable(INPUT_VOLTAGE));
Serial.println(” mV”);
// if an error is stopping the motor, print the error status variable
// and try to re-enable the motor
if (digitalRead(errPin) == HIGH)
{
Serial.print("Error Status: ");
Serial.println(getVariable(ERROR_STATUS));
// once all other errors have been fixed,
// this lets the motors run again
exitSafeStart();
}
}

  • First you need to define the set of pins used for your second motor just like how the pins were declared on your first motor as stated…

#define rxPin 10 // pin 10 connects to SMC TX
#define txPin 9 // pin 9 connects to SMC RX
#define resetPin 11 // pin 11 connects to SMC nRST
#define errPin 12 // pin 12 connects to SMC ERR

but of course change the defined variable and values. The numbers/values must correspond to the pins you are using on your second motor.

  • Second, create a new object to identity your second motor just like how your first motor was defined.

NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

and use that new object to drive your 2nd motor.

  • ben10

Thank you for your reply. This is the first time that I have ever used serial devices. This code that I have came from the manufacturer, and for the most part, I dont know what most of the serial commands mean, most of the simple code I under stand. Am I able to wire them up to the same tx pin, like a dasiy chain? if so, how do you do that? Is it the same way that you just told me? I am wanting to control these unit totally seperately.