Needing help with Newsoftserial commands!!!!

I have a prewritten code from the maker of a TTL serial motor controller. The controllers have built in protocols that can number the devices. http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf (page 61). My two motor controllers are wired to the same serial tx line as shown on page 35 http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf
The code will control one of the boards but I am trying to rewrite in to control both separately. They explain how on page 64 http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf but i cant seem to get the code right. I am not sure how to write it so that it runs the same way that the prewritten code does, but controls two controllers.

#include <NewSoftSerial.h>
#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// 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);
}
void setup()
{
// initialize software serial object with baud rate of 38.4 kbps
mySerial.begin(38400);
// the Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms
delay(5);
// if the Simple Motor Controller has automatic baud detection
// enabled, we first need to send it the byte 0xAA (170 in decimal)
// so that it can learn the baud rate
mySerial.print(0xAA, BYTE); // send baud-indicator byte
// next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run
exitSafeStart(); // clear the safe-start violation and let the motor run
}
void loop()
{
setMotorSpeed(3200); // full-speed forward
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
}

Please keep the questions in one thread, ==> - http://arduino.cc/forum/index.php/topic,57291.0.html - I know they differ a bit but they have almost the same subject , same application. Is easier for you too :slight_smile:

Thanks,
Rob

Sorry, I tried making this one earlier but computer died on me and I didnt think this post was saved.

Thanks for removing the other thread,

A quick read saw that controlling one motor uses a different protocol than two motors.

My interpretation of the PDF leads to the following adjustment (note CTRL-T in the IDE gives a nicer indentation ),

give it a try (not tested/compiled)

#include <NewSoftSerial.h>

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

NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

// pululu codes
#define NEWCMD 0xAA
#define EXITSAFESTART 0x03
#define FORWARD 0x05
#define REVERSE 0x06

// device id's
uint8_t Motor1 = 10;
uint8_t Motor2 = 13;


void exitSafeStart(uint8_t device)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);
  mySerial.print(EXITSAFESTART, BYTE);
}


void setMotorSpeed(uint8_t device, int speed)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);

  if (speed < 0)
  {
    mySerial.print(REVERSE, BYTE); // motor reverse command
    speed = -speed; // make speed positive
  }
  else
  {
    mySerial.print(FORWARD, BYTE); // motor forward command
  }
  speed = constrain(speed, 0, 3200);  // speed should be a number between 0 to 3200
  mySerial.print((unsigned char)(speed & 0x1F), BYTE);
  mySerial.print((unsigned char)(speed >> 5), BYTE);
}

void setup()
{
  mySerial.begin(38400);
  delay(5);
  mySerial.print(0xAA, BYTE); 
  exitSafeStart();
}

void loop()
{
  int speed = analogRead(A0) * 6 - 3072;  // use an LDR or potmeter or ...
  setMotorSpeed(motor1, speed); // full-speed forward
  delay(1000);

  int speed = analogRead(A1) * 6 - 3072;
  setMotorSpeed(motor2, -speed); // full-speed reverse
  delay(1000);
}

I placed the code into the ide and verified it. It hangs up on "void EXITSAFESTART(uint8_t device)" with the error of "expected unqualified-id before numeric constant." what does that error mean and what does unit8_t device mean?

How familiar are you with C/C++ programming? You might want to spent some $$ on a Kernigham & Ritchie book about C to get the basics right.

uint8_t device : means that a variable is passed to the function named device (so it can be refered to inside the function) and the type is uint8_t

uint8_t : is a datatype UNsigned INT 8bits meaning a variable with the range of 0 - 255

This is really the only programing that i have done. when i was 10 I taught myself dos and q-basic, but I have forgotten most of that.