this code was given to me and i cant seem to make it work. 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? The two devices that i have connected on the same serial line are pololu simple motor controllers. 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
#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
#define Motor1 = 10;
#define 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(19200);
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);
}
#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);
}
but i wanted to rewrite it to control two devices on the same serial line separately.
On page 61, it says that you need to send three or more bytes of data to control multiple devices. The first byte tells the controller that the rest of the data is for a specific device. The second byte defines which device. The rest of the data is the command for that device.
There was nothing on that page that told you to rename the ExitSafeStart() function to have the same name as the EXITSAFESTART command constant. So, why did you do that?
It has one name in the code in your original post, and another name in the code posted in reply #4. If you didn't rename it, you need better control over the access to your code on your computer.
Are you talking about going from ExitSafeStart to EXITSAFESTART? Yes, I did make that change. I just thought that he made a mistake in typing. As you can tell, I am very new to writing complex code.
When you define "void exitSafeStart", you told the compiler that it is a function that doesn't return a value ("void") and takes one unsigned eight bit argument called "device".
When you called "exitSafeStart" in "setup", you didn't provide an argument.
That's what the compiler complained about.
And that fixed that issue. Was that the wrong thing to do? I am not sure what function that line was doing. This is the code so far that completes the verifying without errors. Do you see anything that stands out to you? At this point in time I'm only guessing on what some of these lines of code are doing.
#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(19200);
delay(5);
mySerial.print(0xAA, BYTE);
}
void loop()
{
//int speed = analogRead(A0) * 6 - 3072; // use an LDR or potmeter or ...
setMotorSpeed(Motor1, 3200); // full-speed forward
delay(1000);
// int speed = analogRead(A1) * 6 - 3072;
setMotorSpeed(Motor2, -3200); // full-speed reverse
delay(1000);
}
They are pololu simple motor controllers. http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf. They can be given device numbers by setting them up in their internal firmware. I currently have them numbered 1 and 2. they can be set up to any number from 1-127. page 60 in that link.