#110 Arduino Basics – Substring Command Arduino – Programming Tutorial

We make videos on things that we struggled with hoping to help you so you dont, consider, subscribing and hitting that bell in this video im going to go over the arduino sub string, its a fairly simple project. But if any of you follow me regularly last week i didnt put out any videos, because i had a week of failures, both technologically and just things, just didnt work out. So i thought id do a couple easy ones this week and i have had some questions on the substrings ive set up a string im going to call it x and the string is cheapcontrols.com rocks because it does and then im going to use a serial port. Just so, i can use a serial, monitor and show you what im doing with it and just to start out were just going to print that string just to make sure everything works well, upload it ive got an uno set up for this one. I have the serial monitor set up over here and its printing. It and ive got a delay of three seconds so every three seconds its going to print out and now well start adding to the command itself and ive set up a spreadsheet that has cheapcontrols.comrocks in it and then ive broken it up by each character. The first thing were going to print is cheap and thats, going to be the substring 0 5 and the first digit in here. The first selector is 0 and it starts with 0 0 base 0 through 24, but the second character is not zero based.

It starts with one so when were looking for, our sub string were going to want zero to the p, which is five, so zero comma five im going to refer back to this, as i work through the video so now were going to serial print the x, But the substring of x, 0 5.. So we should see cheap im going to upload it clear the output and there we go weve got cheap, just like we would expect for the next one were going to print out cheap and then were going to put a space and were going to put controls And since the first one was zero and five characters for the second one, since its zero based were going to start at the sixth character and were going to print out seven characters well upload this and see what happens, we got cheap and then an o. So thats kind of strange, why would we get that lets go back to our sheet to find out, so the second character was six and seven. If we were to go over to the sixth character, six, which would be the o, which that makes sense since its zero based, i should have put a five and then since the second number is. The second number is seven: it wasnt seven characters which would be this whole thing. It was to the seventh character, so we started at six. We ended at seven and we got the o.

So now cheap o makes sense, it should have been cheap, 5 and then a comma 13.. So lets go back and replace that well make this 5 and well make this 13., and you can see that i added this space right here and you can add strings together in the arduino by just using the plus sign plus m plus. So now lets upload. This and see if we get a better result – and you can see this is more of what we want to cheap controls now for this third one, i want to put cheap space control, space rocks and 1925.. If you can see, 19 is the r. It goes to 25, which should be the exclamation point. It should work just fine and we get cheap controls rocks and the reason i wanted to do this and do this 19 to 25, was to show you something interesting about the substring. If youre going from a character to the end, you dont have to put the second character in so what i mean is in this case i can get rid of the 25, because what its going to do is its going to go from 19 all the way To the end, when i upload this, you shouldnt see any difference and, as you can see, you still get cheap controls rocks, just like you did before, and just to show that a little more clearly were going to comment out everything and were going to print substring Of zero, so substring of zero is just the same thing as printing the string itself, and you can see we get cheap controls rocks.

It did not format anyway, its just the string itself for the final test were going to add some text with the substring. So, in this case, were going to print dot. Www.Cheapcontrols.Com because were gon na go to 0 13 and 16 9. were going to go from 0 to 13, which starts at cheap and goes to the s, controls and then well have to insert the dot the period right here and then were going to go 16 To 19. so were going to start up here at 16 and go to 19, which is c o m, and you can see we get www.cheapcontrols.com. The main reason i wanted to show you this or make a video on. It is its kind of strange that the first character or the first argument or attribute here, is zero based and the second one isnt. When i first started doing this, i got a little bit confused and then i thought well. Maybe this was the the number of digits thats. Why i did that one x. I did that one example where i did six comma seven um, but you have to remember thats, just the starting point and the ending point and its zero based for the first one and its not zero base for the second one. Otherwise substring is a very useful command.

Share.
Exit mobile version