Hi All,
I am programming a Hexapod (18 servos) using Arduino to run a Servo Board by Lynxmotion.
(the SSC-032U)
The servos take a Serial.println command string to set their position.
ie:
Serial.println("#0 P1500 T500");
here #0 is the servo, P1500 is the position, and T500 is the time to arrive at the position.
Can anyone help me come up with a way to use variables that I can drop into the string.
I realize that strings are arrays of characters and strings are not accepted as a variable type in Arduino.
but, maybe you have a suggestion.
here is an example of the function I created to set the pod into a neutral stance:
as you can see variables would be an enormous help!
Thanks for any suggestions.
Jinda
void Neutral(){
//-------------------------------NEUTRAL--------------
// L F neutral
Serial.println("#0 P1500 T500");
Serial.println("#1 P1900 T500");
Serial.println("#2 P2050 T500");
//L M neutral
Serial.println("#8 P1570 T500");
Serial.println("#9 P2020 T500");
Serial.println("#10 P2170 T500");
//L R neutral
Serial.println("#13 P2200 T500");
Serial.println("#14 P1170 T500");
Serial.println("#15 P980 T500");
//R F neutral
Serial.println("#16 P1500 T500");
Serial.println("#17 P1200 T500");
Serial.println("#18 P1050 T500");
//R M neutral
Serial.println("#24 P1640 T500");
Serial.println("#21 P1050 T500");
Serial.println("#22 P1120 T500");
// R R neutral
Serial.println("#28 P600 T500");
Serial.println("#29 P2000 T500");
Serial.println("#30 P1680 T500");
}
There's also this sort of thing - sometimes this can be more flash-efficient than pulling in something like snprintf().
void servoCommand(byte servonum, int pos, int time) {
Serial.print('#');
Serial.print(servonum);
Serial.print(" P");
Serial.print(pos);
Serial.print (" T");
Serial.println(time); //println to get the CRLF sequence
}
It's ugly, but it's easy and it works, and if it's the only place you'd do weird string formatting, it should be smaller
Jinda2001:
Hi All,
I am programming a Hexapod (18 servos) using Arduino to run a Servo Board by Lynxmotion.
(the SSC-032U)
The servos take a Serial.println command string to set their position.
ie:
Serial.println("#0 P1500 T500");
here #0 is the servo, P1500 is the position, and T500 is the time to arrive at the position.
Can anyone help me come up with a way to use variables that I can drop into the string.
I realize that strings are arrays of characters and strings are not accepted as a variable type in Arduino.
but, maybe you have a suggestion.
here is an example of the function I created to set the pod into a neutral stance:
as you can see variables would be an enormous help!
Thanks for any suggestions.
Jinda
I would try sprintf()
void setup()
{
Serial.begin(9600);
int motor = 14;
int pos = 1500;
int wait = 500;
char motorMessage[32] = "";
sprintf(motorMessage, "#%d P%d T%d", motor, pos, wait);
Serial.println(motorMessage);
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Wow, So helpful
snprintf(); is the tool!
but,
for the immediate future thank you DrAzzy, this is the breakdown I was hoping to work with.
thanks for your time.