Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:53:29+00:00 2026-06-10T07:53:29+00:00

I’m writing an app to control my robot with my Android phone over Bluetooth,

  • 0

I’m writing an app to control my robot with my Android phone over Bluetooth, everything is goes well, data is echoed and verified, but I’m having some trouble with the protocol, specifically I want my robot’s wheels to turn when I send a command such as s,10,100 or s,-30,-10… (values in percent).

My problem is that when I want to parse my wheel speed command on my Arduino I must parse from up to 4 separate bytes to int, for example s,-100,-100 makes my robot go backwards at full speed, but how do I parse this so I can call setSpeed(left, right); with leftand right equal to -100?

I know I can separately analyse every byte and put them together to get an integer, but it’s not very elegant and there’s probably a better solution to all this already, unfortunately I haven’t found it yet.

EDIT

Here’s my Arduino function for parsing my commands:

void parseCommand(char* command, int* returnValues)
{
  // parsing state machine
  byte i = 2, j = 0, sign = 0;
  int temp = 0;
  while(*(command + i) != '\0')
  {
    switch(*(command + i))
    {
      case ',':
        returnValues[j++] = sign?-temp:temp;
        sign = 0;
        temp = 0;
        break;
      case '-':
        sign = 1;
        break;
      default:
        temp = temp * 10 + *(command + i) - 48;
    }
    i++;
  }
  // set last return value
  returnValues[j] = sign?-temp:temp;
}

You call it this way when parsing something like s,100,-100 (must be \0 terminated):

char serialData[16];
void loop()
{
  if(Serial.available() > 0)
  {
    Serial.readBytesUntil('\0', serialData, 15);
    switch(serialData[0])
    {
      case 's':
        int speed[2];
        parseCommand(serialData, speed);
        setSpeed(speed[0], speed[1]);
        break;
    }
    // always echo
    Serial.write(serialData);
    // end of message is maked with a \0
    Serial.print('\0');

    // clear serialData array
    memset(serialData, 0, sizeof(serialData));
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:53:31+00:00Added an answer on June 10, 2026 at 7:53 am

    Just read character by character into a state machine. It’s simple and efficient.

    To read in a number digit by digit, do this: Start with zero. For each digit, multiply the number by ten and add the value of the digit. So, for example, reading 97 would work like this:

    1. You read in a digit with no prior digit, you start with 0.

    2. You read in 9 and compute (0*10)+9 -> 9

    3. You read in 7 and compute (9*10)+7 -> 97

    4. You read in a non-digit, you output the 97.

    Here’s a fuller example s,10,100:

    1. You start in the “ready to read command state”.

    2. You read “s”, “s” is the command. You switch to the “ready to read first comma” state.

    3. You read the first comma, you switch to the “ready to figure out the sign of the first parameter” state.

    4. You read a digit. Since this wasn’t a “-“, the first parameter is positive. You set the first number to the value of the digit, 1. You are now in “reading first number” state.

    5. You read a digit, 0. You set the first number to 1*10+0 -> 10. You are still in “reading first number” state.

    6. You read a comma. You are now in the “ready to figure out sign of the second parameter” state.

    7. You read 1. The second number is positive (since this wasn’t a “-“). You set the second number to 1. You are in the “reading second number” state.

    8. You read 0. The second number is now set to 1×10+0 -> 10. You are still in “reading second number” state.

    9. You read 0. The second number is now set to 10×10+0 -> 100. You are still in “reading second number” state.

    10. You read an end of line. You execute your results: The command is “s”, the first number is positive, the first number is 10, the second number is positive, the second number is 100.

    11. You switch back to “ready to read command” state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I want to construct a data frame in an Rcpp function, but when I
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.