arduino strtok


Answering a lot of questions. I got asking for an example for Arduino serial communications, with a little more in depth into how to do some parsing, because in the video I did, this was are doing a tutorial number 4. The first thing I did wasn’t exactly optimal and was rather limited in what it could do so today this tutorial is going to go a little more, like I said in death and do a little more complex parsing. So with that let’s go ahead and get started for this you’re going to need to know a little bit of how to do some string parsing some string searching because that’s pretty much. What this tutorial is going to use is strings to sort commands. Basically, so let’s just go ahead and set up the serial port because that’s what we’re going to be using I’m using the visual micro plug in to do this tutorial, because it’ll make things a little easier later on I’m starting the serial at 1, 1. 5. 200. Baud, just because that’s what the debugger uses natively so get everything talking at the right: speed, ok and I’m gon na create a string variable and we’ll just call that command. This is the variable that’s going to contain the string data that we’re going to pull in from the serial buffer so go into the loop if serial dot available we’re going to create a character, variable we’ll, just call that C equals serial dot read if C equals A newline: this is how we judge what the end of a command comes in if we receive a newline character, so every command is separated by a newline and we’ll set that up in the serial monitor when we go to test this.

So if the character is a newline we’ll get back to that in a second else: command plus equals C, so we’re just going to append the character C to the end of command actually, and here just going to clear the command variable area. Okay, now we’re going to create a separate function, void parse command, and this is going to take a string argument. We’Ll just call it calm short for command and for this I’m going to create I’m looking to do a command with two parts. So what I should have mentioned this at the beginning, what we’re doing is we’re going to create a parsing system to accept two part commands to turn a pin on and off so we’re going to be able to turn any pin on the Arduino on and off. So it’ll accept the pin on or off command and then the number argument, along with it so that’s. What we’re trying to do so I’ll go up here and set pin mode 3 as now put in mode 4 as an output and pin mode 5 as an output output, optic output. So those are the three pins we’ll be able to control and you’ll see that in a second. So so the two parts are going to be string, part 1 and string, part 2, creative naming system and then we’ll just jump up back here. So if we get the new line, we’re just going to say, parse command and then we’re gon na feed it the command variable we’ve been filling data into so now, let’s work out the parsing command.

This is where the parsing is done, because it’s the parsing command function. So the first thing we need to do is divide the command. We’Ve sent it into two parts, so I’m envisioning, something like this we’re going to say, pin on and then the number pin so we’ll say, pin on 3 or pin off 3, so it’s going to be separated by this space. So part 1 is going to be com, dot, substring, we’re gon na go from the beginning of the string to calm dot index of and then just a space. So this is going to read from the beginning of the string. If you don’t know what substring does is it takes apart, a sub part of a big string, given two arguments, the beginning of where you want to read and the end of what you want to read. So what this will do is it’ll read from the beginning right up until the space, but not including the space and then we’re gon na say part, two is column, dot, substring and then anything after com dot index of space plus one. So what part two is anything after the space plus one character, because we don’t want the space this. This will, if we ignore the plus one we’ll get space and then the number we just want the number, so we move one character over. So this is going to be a word argument. There a word command and a number argument.

So, if part one dot equals and we’re going to ignore case, there are a couple ways you can get around this. If you, if you don’t, want to use the ignore case, you can make the command all uppercase just by doing calm, dot a sub string, dots to uppercase or to lowercase all depends on how you want to format your commands. I want it to be such that you can put in any combination of upper and lower case letters that you want. I just wanted to be able to say type in pin on and whatever and it’ll do that. So you don’t have to worry about case sensitivity. So equals ignore case and we’re going to say equals pen on and I don’t have to specify any any upper or lowercase for pen on because it’s going to again ignore the case now in spin is going to equal part two. This is the pin number dot. Two in so we’ll just convert that to an integer and then digital right pin we’re going to turn it on else. If parts one dot equals ignore case, pin off scroll down a bit, we go we’ll, do the same thing in pin equals part 2.2 int digital right, pin we’re going to write it not high. This time, we’re gon na write it low and there we go so we’ve got the two commands we want, pin on and pin off and then we’ve got the second part of the command, which is the pin itself, obviously it’s only going to convert the second part To an integer if these commands are either pin on or pin off, so if you wanted to add extra commands afterward that didn’t have a number second argument.

You can do that. I would like to write code in a way that I can add things on later. So everything’s expandable, if need be – and I guess we’ll just add one more case else – we’ll – add like a default case. So if none of these are commands well there, if it doesn’t recognize any of the commands that we typed in, we can just put in something like serial dot, println command, not recognized and they’re real. So that should do it now: I’m, not going to be showing the Arduino and a couple LEDs attached to the pins I’m, going to be using the visual micro debugger so I’m going to go into the parsing example. Class I’m going to make sure I’ve got the debugger set to full, which I do. Obviously, if you want to run this yourself, you’re gon na need some visual means to inspect the pins. I have the debugger here and I’m going to turn on. Where is it where there’s I’m gon na report the digital pins? So if I start the debugging, I see I have an error somewhere, but I didn’t actually even read the air error in function void loop expected before. If oh, I did not put a semicolon, there. Try that one more time f5 for the real in void loop, oh and this shouldn’t – be a string that should be there. You it’s alright move a little fast when I start typing: okay, so they’re real, oh, so what we have here is drive that over, but there yeah okay, so here we’ve got visual representation of pins.

Three four and five red means off green means on. So if I type in on three, you can see B, green at the three turns green and if I type pin off three it goes off and again I can create any any change in capitalization, so I can say: pin off their pin on rather three and It still recognizes it and again I can do, pin off three still recognizes it, and you can again. This works with any of the pins we’ve got set up so pin four on and five on and three on and then pin off four and they’re real, so that does it that we’ve successfully created a parsing system. To that accepts, two commands, pin on and pin off for any pin we’ve got set up as an output. Obviously, if you move over here, if you had more pins set up as an output, you can control more pins or, if you had all the pins set up as an output from the start again, you could control all these pins right away. So and now, if we try say, pin off there, we go so it doesn’t know what that command is so it’s going to say, command, not recognized, and so our default case works as well. So that is it for this. I hope this answered a lot of questions. People had about creating a little more complex and a little more well functioning parsing system. I yeah – and I said I know the last one, the one I did in my last review – wasn’t that well done probably could have been done a lot, what a lot better.

So hopefully this shows that, yes, I do know how to write a parser, and I hope this has explained to you how to write your own system parser and again this can be expanded. This has been done so that it will run for any two part command. Again. You can make this three parts, four parts you can even make it so that it’s dynamic you can have parts you can have commands with three parts commands with five parts commands that just run off one word so again, very dynamic system.


arduino strtok Video







arduino strtok news








    arduino strtok Social






    👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.
    safe image.php?d=AQDyi1ELcW4VGzhx&w=720&h=720& dChOhNMmeF - arduino strtok

    Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself:

    🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today:

    70537898 550174072395145 8798850142103928832 n.jpg? nc cat=102& nc oc=AQmCfMPwMf1oPpYqFhzbxfnGdbrmsjZXrEEBy1ox5Z8EJtiYMADoFNZ5mnSNRgCCY74& nc ht=scontent - arduino strtok









    (Visited 74 times, 1 visits today)

    About The Author

    You might be interested in

    Comment (20)

    1. Please help!

      void setup()
        pinMode(3, OUTPUT);
        digitalWrite(3, LOW);

      void loop()
        String command;
        if (Serial.available() > 0)
          char com =;
          if (com == ‘\n’)
            command = “”;
            command += com;

      void serialcommand(String com)
        if (com.equalsIgnoreCase(“pin on”))
          digitalWrite(3, HIGH);
        else if (com.equalsIgnoreCase(“pin off”))
          digitalWrite(3, LOW);
          Serial.println(“Invalid command”);

    2. Thanks this really helped. ..This is way faster than the slow Serial.parseInt(). To just parseInt() 2 integer values it takes a little over 6000micros but doing it this way and converting to integers only takes 320micros.

    3. His code works really great. I built it in Arduino IDE and worked well. I just had a hard time not getting the pins on or off and this was because I forgot to select (Both NL and CR) in the serial monitor. hope this helps other people.

    4. how should be if I pass more then only 2 parameters? like pin 4 3 6 … to turn off 3 pins in the same time

    5. I need to find a way to capture the oscillation point of a moving object.
      To visualize this, Picture a rail with a distance sensor at each end. The target would oscillate between the 2 end points with the center being 180. To the left the max measurement would be 270 and max 90 to the right. When the target moves to the right then switches direction I want to capture the farthest measurement lowest number serial data to trigger a device when the target is at a certain point on the left. Then I want to capture the oscillation point highest number on the left to in turn trigger the device at a certain point on the right. Due to changing loads on the device being triggered the oscillation points will change randomly.

      Can this be done?

      Below is a rough draft of comments without any code.

      //Arduino uno

      // vl53l0x sensors
      // distance sensor1 value 181 to 270 degrees
      //distance sensor2 value 179 to 90 degrees

      //1 stepper Nemo 17 or 23
      //Stepper full rotation 0-359 steps (clock wise count 0 north)
      //Home 180, max rotation from 270 to 90 degrees.
      // orientations. 270 right, 90 left.
      //sensors placed at the ends of a rail with oscillating target.

      //if sensor1 value oscillates set sensor1 to highvalue of oscillation
      Oscillation at 172-173-172 would set value at 173

      // if sensor2 lowalue is = 178
      // run stepper to 183 when sensor1 reaches 182 //1degrees
      // if sensor2 lowalue is =177
      // run stepper to 184 when sensor1 reaches 183
      // if sensor2 lowalue is =176
      // run stepper to 185 when sensor1 reaches 184
      // if sensor2 lowalue is = 175
      // run stepper to 186 when sensor1 reaches 185
      // if sensor2 lowalue is =174
      // run stepper to 187 when sensor1 reaches 186
      // if sensor2 lowalue is =173
      // run stepper to 188 when sensor1 reaches 187
      // if sensor2 lowalue is = 172
      // run stepper to 189 when sensor1 reaches 188
      // if sensor2 lowalue is =171
      // run stepper to 189 when sensor1 reaches 188
      //Etc……. to lowvalue of 90
      //use < at end max run to 270 //If sensor 2 value oscillates set sensor2 to lowvalue of oscillation Ocilation at 186 - 185 - 186 would set value at 185 // if sensor1 highvalue is 182 // run stepper to 177 when sensor2 reaches 178 // if sensor1 highvalue is 183 // run stepper to 176 when sensor2 reaches 177 // if sensor1 highvalue is 184 // run stepper to 175 when sensor2 reaches 176 // if sensor1 highvalue is 185 // run stepper to 174 when sensor2 reaches 175 // highvalue of 270....... //use > at end max run to 90.

    6. Hello, how can I read a returned message after using GET method? Basically, I am able to push data with GET and recieving back “success” message on Serial Monitor. I want “success” message to be read and trigger a PIN. Is it possible? Thanks

    7. Hey man, I know this video is 5 years old or whatever, but it’s a great tutorial on separating out data from serial input. I have spent hours reading Arduino forums about methods of doing this, but they are SO convoluted (making temporary indexes and arrays, etc). Thanks so much man, this is so much cleaner and easier to understand.


    Your email address will not be published. Required fields are marked *