String as variable (?)

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"); 
}

I realize that strings are arrays of characters and strings are not accepted as a variable type in Arduino.

I am not clear what you mean by this.

As to creating a string, part of which is the value of a variable, have a look at the snprintf() function

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 :wink:

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:

}

Some old test code I used to control an ssc-32 from a web page via an arduino with an ethernet shield.

//zoomkat 5-24-10
// http://www.arduino.cc/en/Tutorial/TextString for WString.h

#include <WString.h>
#include <Ethernet.h>

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString = String(100); //string for fetching data from address

///////////////////////
String teststring = String(100);
String finalstring = String(100);
String flag = String(2);
int ind1 = 0;
int ind2 = 0;
int pos = 0;
//////////////////////

void setup(){

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600); 
}

void loop(){
  // Create a client connection
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString.append(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          //Serial.println(readString);
          //readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"

          if(readString.contains("-")) { //test for servo control sring
            readString.replace('-', '#');
            pos = readString.length(); //capture string length
            //find start of servo command string (#)
            ind1 = readString.indexOf('#');
            //capture front part of command string
            teststring = readString.substring(ind1, pos);
            //locate the end of the command string
            ind2 = teststring.indexOf(' ');
            //capturing the servo command string from readString
            finalstring = readString.substring(ind1, ind2+ind1);
            //print "finalstring" to com port;
            Serial.println(finalstring); //print string with CR
          }
          ////////////////////////
          //GET /?Slidervalue0=1800&Submit=Sub+0 HTTP/1.1
          if(readString.contains("Slidervalue")) {
            ind1 = readString.indexOf('u');
            ind2 = readString.indexOf('&');
            finalstring = readString.substring(ind1+1, ind2);
            finalstring.replace('e', '#');
            finalstring.replace('=', 'p');
            Serial.println(finalstring);
          }
          ///////////////////

          //now output HTML data header
          client.println("HTTP/1.1 204 Zoomkat");
          client.println();
          client.println();
          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          //clearing string for next read
          readString="";
          teststring="";
          finalstring="";

        }
      }
    }
  }
}

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.