title: Splitting a comma-delimited string of integers
body: <p>My background is not in C (it's in Real Studio - similar to VB) and I'm really struggling to split a comma-delimited string since I'm not used to low-level string handling.</p>
  
  <p>I'm sending strings to an Arduino over serial.  These strings are commands in a certain format.  For instance:</p>
  
  <pre><code>@20,2000,5!
  @10,423,0!
  </code></pre>
  
  <p>'@' is the header indicating a new command and '!' is the terminating footer marking the end of a command.  The first integer after '@' is the command id and the remaining integers are data (the number of integers passed as data may be anywhere from 0 - 10 integers).</p>
  
  <p>I've written a sketch that gets the command (stripped of the '@' and '!') and calls a function called <code>handleCommand()</code> when there is a command to handle.  The problem is, I really don't know how to split this command up to handle it!</p>
  
  <p>Here's the sketch code:</p>
  
  <pre><code>String command; // a string to hold the incoming command
  boolean commandReceived = false; // whether the command has been received in full
  
  void setup() {
    // put your setup code here, to run once:
  Serial.begin(9600);
  }
  
  void loop() {
    // main loop
    handleCommand();
  }
  
  void serialEvent(){
    while (Serial.available()) {
    // all we do is construct the incoming command to be handled in the main loop
  
      // get the incoming byte from the serial stream
      char incomingByte = (char)Serial.read();
  
      if (incomingByte == '!')
      {
         // marks the end of a command
         commandReceived = true;
         return;
      }
      else if (incomingByte == '@')
      {
         // marks the start of a new command
         command = "";
         commandReceived = false;
         return;
      }
      else
      {
        command += incomingByte;
        return;
      }
  
    }
  }
  
  void handleCommand() {
  
    if (!commandReceived) return; // no command to handle
  
    // variables to hold the command id and the command data
    int id;
    int data[9];
  
    // NOT SURE WHAT TO DO HERE!!
  
    // flag that we've handled the command 
    commandReceived = false;
  }
  </code></pre>
  
  <p>Say my PC sends the Arduino the string "@20,2000,5!".  My sketch ends up with a String variable (called <code>command</code>) that contains "20,2000,5" and the <code>commandRecieved</code> boolean variable is set to True so the <code>handleCommand()</code> function is called.</p>
  
  <p>What I would like to do in the (currently useless) <code>handleCommand()</code> function is assign 20 to a variable called <code>id</code> and 2000 and 5 to an array of integers called <code>data</code>, i.e: <code>data[0] = 2000</code>, <code>data[1] = 5</code>, etc.</p>
  
  <p>I've read about <code>strtok()</code> and <code>atoi()</code> but frankly I just can't get my head around them and the concept of pointers.  I'm sure my Arduino sketch could be optimised too.</p>