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
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr