I was trying to make an Arduino Robot that is Bluetooth controlled but I've some problems!

I can't figure out how to parse strings in Arduino like c++/java/python! I tried this, If the first letter is L, set the next value to left motor, same for R

but for example "L255" is received as L - 2 - 5 - 5 and it sets L to 2 and ignores 5 - 5.

Any ideas on what I should do?

EDIT: Thought of something else, Sending L or R will toggle the motors! but that's just terrible, it'll be my last option

Update: This is how it works now: L(0-5) means Left motor speed 5 * 51, 6-9 are backwards! its go backwards at (L(6-9) -5 * 51)

  • Either collect characters until you have a full string (probably terminated with a newline) or else create a state machine which interprets characters one at a time as they are received, for example first deciding on a mode based on a letter, then doing a multiply-and-add on digits to create a value, and executing on the newline. – Chris Stratton Oct 10 '15 at 17:15
  • :O Good idea! I'll try that tomorrow :) thanks! But I just realized, I also don't need my speed to be so accurate, It can be from 0-9! but anyway that's also good for future stuff and way better than inaccurate speed :) – user3033693 Oct 10 '15 at 18:11
  • I'm starting to think my lpc1678 is better for this :) but its too hard to work with :S my board is so huge! – user3033693 Oct 10 '15 at 18:41
  • Further to Chris Stratton's comment, you can read about using state machines for processing serial data. – Nick Gammon Oct 10 '15 at 20:24

A state machine is the right way to go if you are sending multibyte commands.

However do you need to send 4 bytes for every turn? Do you need accuracy to one degree? Are you going to make regular turns of greater than 60 degrees? If not I would consider encoding the dAta into a single byte. You will reduce the radio bandwidth by 3/4, it will improve the responsiveness of. Your software and it will remove the need to have a handler for incomplete messages.

Of course there are probably other requirements that I don't know that will stop you doing this, but in case you haven't thought of it.

  • For my RC Car its enough! (0-9 speed) but I would love to know about it just for other projects :) But chris and Nick told me how to do so! Thanks :) – user3033693 Oct 11 '15 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.