Hi, has anyone used the NewSoftSerial library with the Polulu Micro servo controller? I just changed over from the standard SoftSerial library and as soon as I upload the code I get a fatal error message from the servo controller.
( I'm trying to sync 6 servos to a quicktime playing at 25 fps, as soon as I changed from 3 to 6 servos it appeared that the baud rate was to slow, thus the change to NewSoftSerial.)
Any help would be greatly appreciated. Regards Conor
#include <NewSoftSerial.h>
NewSoftSerial softSerial(2, 3);
// set up a new serial port
void setup()
{
//pinMode(2, INPUT);
digitalWrite(3, HIGH); // keeps pololu board from getting spurious signal
pinMode(3, OUTPUT);
// set the data rate for the hardware serial port
Serial.begin(9600);
// set the data rate for the SoftwareSerial port
softSerial.begin(9600);
set_speed(0, 0);
set_speed(1, 0);
set_speed(2, 0);
set_speed(3, 0);
set_speed(4, 0);
set_speed(5, 0);
}
void loop()
{
Serial.flush();
char inputA[8];
char inputB[8];
char inputC[8];
char inputD[8];
char inputE[8];
char inputF[8];
memset(inputA, '\0', 8);
memset(inputB, '\0', 8);
memset(inputC, '\0', 8);
memset(inputD, '\0', 8);
memset(inputE, '\0', 8);
memset(inputF, '\0', 8);
byte inByte = '\0';
while(inByte != 'A') {
inByte = Serial.read(); // Wait for the start of the message
}
if(inByte == 'A') {
while(Serial.available() < 24) { // Wait until we receive 5 characters
;
}
for (int a=0; a < 24; a++) {
if( a >= 0 && a <= 3){
inputA[a] = Serial.read(); // Read the characters into an array
}
if( a >= 4 && a <= 7){
inputB[(a-4)] = Serial.read(); // Read the characters into an array
}
if( a >= 8 && a <= 11){
inputC[(a-8)] = Serial.read(); // Read the characters into an array
}
if( a >= 12 && a <= 15){
inputD[(a-12)] = Serial.read(); // Read the characters into an array
}
if( a >= 16 && a <= 19){
inputE[(a-16)] = Serial.read(); // Read the characters into an array
}
if( a >= 20 && a <= 23){
inputF[(a-20)] = Serial.read(); // Read the characters into an array
}
}
}
int numberA = atoi(inputA);
int numberB = atoi(inputB);
int numberC = atoi(inputC);
int numberD = atoi(inputD);
int numberE = atoi(inputE);
int numberF = atoi(inputF);
position_absolute(0, numberA);
position_absolute(1, numberB);
position_absolute(2, numberC);
position_absolute(3, numberD);
position_absolute(4, numberE);
position_absolute(5, numberF);
}
void position_absolute(byte servo, int angle)
{
//this function uses pololu mode command 4 to set absolute position
//servo is the servo number (typically 0-7)
//angle is the absolute position from 500 to 5500
unsigned int temp;
byte pos_hi,pos_low;
temp = angle & 0x1f80; //get bits 8 thru 13 of position
pos_hi = temp >> 7; //shift bits 8 thru 13 by 7
pos_low = angle & 0x7f; //get lower 7 bits of position
//Send a Pololu Protocol command
softSerial.print(0x80, BYTE); //start byte
softSerial.print(0x01, BYTE); //device id
softSerial.print(0x04, BYTE); //command number
softSerial.print(servo, BYTE); //servo number
softSerial.print(pos_hi, BYTE); //bits 8 thru 13
softSerial.print(pos_low, BYTE); //bottom 7 bits
}
void set_speed(byte servo, byte speedVal)
{
//this function uses pololu mode command 1 to set speed
//servo is the servo number (typically 0-7)
//speedVal is servo speed (1=slowest, 127=fastest, 0=full)
//set speedVal to zero to turn off speed limiting
speedVal = speedVal & 0x7f; //take only lower 7 bits of the speed
//Send a Pololu Protocol command
softSerial.print(0x80,BYTE); //start byte
softSerial.print(0x01,BYTE); //device id
softSerial.print(0x01,BYTE); //command number
softSerial.print(servo,BYTE); //servo number
softSerial.print(speedVal,BYTE); //speed
}