title: Extracting info from a string
body: <p>I have been trying to actuate an Arduino Servo with the speed of the car from the game "Assetto Corsa". I have managed to extract the live data from the game using Shared Memory and am trying to send it to the Arduino over the Serial port.</p>
  
  <p>I have not yet managed to represent the actual speed of the car on the Servo, and I am wondering why. I'm pretty sure the problem comes from the Arduino Code, I can extract the speed of the car but I'm unsure how to use it properly. Maybe you could help?</p>
  
  <p>This is the Arduino code extract : </p>
  
  <pre><code>char inData[8];    
  char inChar; // Where to store the character read
  byte index = 0; // Index into array; where to store the character
  int angle = 0;
  int newAngle = 0;
  int MaxChars = sizeof(inData);
  char ch;
  
  
        while (Serial.available())
        {
          char ch = Serial.read();
          Serial.write(ch);
          if (index &lt; MaxChars &amp;&amp; isDigit(ch))
          {
            inData[index++] = ch;
          }
          else
          {
            inData[index] = 0;
            newAngle = atoi(inData);
            Serial.write(newAngle);
            if (newAngle &gt; 0 &amp;&amp; newAngle &lt; 180)
            {
              if (newAngle &lt; angle)
                for (; angle &gt; newAngle; angle -= 1)
                {
                  myServo.write(angle);
                }
              else
                for (; angle &lt; newAngle; angle += 1)
                {
                  myServo.write(angle);
                }
            }
            index = 0;
            angle = newAngle;
            delay(1000);
          }
        }
  </code></pre>
  
  <p>And this is the code I am using to extract info from the game and send it to the Arduino : </p>
  
  <pre><code>while (true)
  {
  
      snprintf(Data, sizeof(Data)-1, "%3.3f", pf-&gt;speedKmh);
  
      SP-&gt;WriteData(Data, sizeof(Data));              
  
      std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  
      int readResult = SP-&gt;ReadData(inData, sizeof(inData));
  
         printf("\t Bytes read:  %i\n",readResult);
  
         for (int i = 0; i &lt; sizeof(inData); i++)
         {
             printf("%i", inData[i]);
             inData[i] = 0;
         }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Erq5L.png" rel="nofollow noreferrer">Output screenshot</a>:
  <img src="https://i.stack.imgur.com/Erq5L.png" alt="1"></p>
  
  <p>This is a screenshot of the echo coming from</p>
  
  <pre><code>Serial.write(NewAngle);
  </code></pre>
  
  <p>I have outlined in yellow the speed of the car. My question is : <strong>How do I extract this speed from the data I receive to use it with the Arduino Servo?</strong></p>
  
  <p>Thank you very much!</p>
  
  <p><strong>----------UPDATE----------</strong></p>
  
  <p>It works! Thank you very much, this is what I changed : </p>
  
  <pre><code>snprintf(Data, sizeof(Data)-1, "%3.3f", (int)(pf-&gt;speedKmh)*1.0);
  </code></pre>
  
  <p>Casted the speed to an int and multiplied by 1.0. That seems to have done the trick! It's another case of "Don't know why it doesn't work, change something, don't know why it works!" </p>