Using tabs to organize code with the Arduino IDE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- [Instructor] Did you know in the Arduino IDE that you can organize your code using different tabs? In this lesson, you're gonna learn how to add these tabs, how they get organized in the Arduino sketch folder and how to use them in order to better organize your code. Stay tuned. (piano music) Before we get started, if you wouldn't mind subscribing to the channel, it really helps us bring you the best content. It doesn't cost you anything but a click and it allows us to keep doing this. All right, so here we are in the Arduino IDE. I've got a program open. It's called Tabbatha and I have got a sketch written out. So, let me just familiarize you with the sketch so you kind of get an idea what's going on. So I've got some neopixels. Those are individually addressable LEDs. It's like an individually addressable LED strip. You've probably seen 'em around. Really cool looking. And I've using this library called fastLED to control them. And I've got some variables here that set up the number of pins and which data pin I'm using and and a array to store the different LEDs that I'm in. Okay, and then I have some functions that do some different things. So one is setting them all the LEDs red. Then one sets 'em all blue, one sets 'em all green but it uses this little fadeall function in here that kind of makes it interesting. And I also have that function right here, fadeall. So I've got four functions and then below that I have my setup in my loop. So in setup, you know just start serial communication and then I'm just initializing some stuff for that fastLED library. Nothing crazy going on in setup. And then loop, all I'm doing is I'm calling these three functions: setRed, setBlue and setGreen. Okay, so what I wanna do is I wanna you know there's a lot kind of, I don't know, going on in here. Maybe I wanna like reduce the clutter in here and let's say I wanna put each one of these functions in its own file, in its own tab. All right, so all of the files that you create in Arduino are gonna live in the Arduino Sketchbook folder. And you can figure out where your Sketchbook folder is by going to if you're on a Mac, you'd go to Arduino Preferences or if you're on a PC, I think it's File Preferences. And the first thing they show is your Sketchbook location. So the location of my Sketchbook folder is under my name, my documents, and then in a folder Arduino. And then I have saved Tabbatha somewhere inside this folder. So I'm gonna go ahead and open that up. All right, so here is the folder Tabbatha and you'll notice I've got a .eno file inside here. Okay, so Tabbatha is the name of the folder and Tabbatha.eno is this file right here. So the way Arduino saves your sketches is is it creates a folder with the same name as the .eno file in here. That's like how the folder structure works. That's how the program stores and organizes the code. Okay, so what I'm gonna do is I am gonna add a tab up here and I'm gonna put this function setRed inside of this tab. So all I do is I come up over here. There's this little dropdown and I'm gonna say new tab and then come down here is where I can name it. So I'm gonna call it, I'm just gonna call it setRed. Doesn't matter what you name it. I could call it, you know, yo mama if I wanted to but I'm gonna call it setRed because to me that's instructive of what it's gonna do. I hit Okay and you'll notice a new tab pops up here. Okay, so now I'm gonna take this function setRed. I'm gonna cut it out of there and I'm gonna paste it in here. All right. So I've cut it out, I've pasted it. I'm gonna save that and then I'm gonna verify it and compiling works. And now I'm just gonna upload it. And you know what? The neopixel strip is doing the exact same thing as it was doing before. Okay. So cool. So I have added this function right here in its own tab. So now, hey, this is a little less congested a little less in here. So now let's go ahead and take a look at that file structure. So now notice what happens here. So here's that enclosing folder, Tabbatha. We still have Tabbatha.eno, that's this tab right here but now we have a new .eno file, setRed.eno. So now what I'm gonna do is I'm gonna go ahead and I'm gonna take all of these functions right here and I'm gonna put them in their own tabs. Tell me what you thinks gonna happen over here, okay? Make a guess while I do this. (lively music) - [Broadcaster] This video is sponsored by NextPCB. For $0, you can get a one to four layer PCB. That's right. $0 for a PCB prototype for the first order. For a limited time, you can get $1,050 in coupons during their spring sale. That's $1,050 worth of coupons for their spring sale. You can upload a Gerber File and when you do that, it will fill in all this information for you and that also allows you to use the Gerber Viewer. That's $0 for a PCB prototype. You can use the link below to register and get that $1,050 in coupons during their spring sale. - [Instructor] All right, check this out. So now I've got Tabbatha. I've got fadeall, setBlue, setGreen, setRed. I've uploaded it. Everything is working just as it was before. Now let's take a look. See what happens is this what you thought was gonna happen? I bet it is. So now all we do, all we have here is multiple .eno files in one enclosing folder named Tabbatha. So take a look at the tabs. Can you figure out how these are organized? Because I think the first one I made was setRed but now it's at the very end. But it looks like these are alphabetically organized up top. With the first sketch like the first one that gets opened, always stays over at the left. So this one always is gonna be at the far left, but the rest of 'em are gonna be based on their alphabetical order in naming in the name of that file. So, F is before S and then B is before G and G is before R but let let's test it. Let's go ahead. Let's make new tab. We're gonna call it Alpha. Where does it go? See, it pops right over in front of all of those. And then if let's make a new tab, let's call it Zulu and it ends up at the... All right, so I think you know, that sounds about right. Okay, I'll delete those. See, that's easy to delete these too. You just delete it and hey, if I wanna rename these, all I gotta do is click over here. I can rename it. So new name for file. I can call it fadeall Ds. Okay, so that's pretty straightforward. Now, how does this actually work though? What's, like what actually happens in the background that sets this up? Well let's check out this awesome page on the internet on GitHub where Arduino talks about their Sketch build process. So this is how the sketch actually gets put together. And if we come down here in pre-processing, it tells us some interesting things. First, it says all eno and .PDE files in the sketch folder. So what is the sketch folder? Well that is, if we come back here, that's this right here. That is that enclosing folder. That's our sketch folder. Okay, so all eno and .PDE files in the sketch folder shown in the Arduino IDE is tabs with no extension are concatenated together. So they take the files like they were cats and they put one after another starting with the file that matches the folder name followed by the others in alphabetical order. So this sketch is gonna have some processing done to it that's gonna allow it to get loaded onto the Arduino hardware. And part of that processing they're telling us is that they're gonna concatenate these files. So here's Tabbatha. They say we're gonna start with the eno file that matches the including folder name. Then in alphabetical order, they're gonna take these right here. So it's literally gonna go in. It's gonna take this file, these lines of code and it's gonna paste 'em right there. Plop and it's gonna have this one file now and it's gonna do those with all of, all of these files. So it's gonna condense 'em into one file. All right, that's pretty interesting. What else? Does it do anything else that we might be interested in? Okay, it does. What it says is prototypes are generated for all function definitions in .eno/.pde files that don't already have prototypes. Okay, so what is a function prototype? Well a function prototype is just this first part of the function, like the function signature, right? And what it's gonna do is stick it right in here. It's gonna look like this. And this tells the compiler, hey, this is this fadeall function. These are the parameters it takes. This is what it's gonna return and it might be referenced somewhere inside this sketch. So this is this function prototype. I know a little bit hand waving there, but I mean that's essentially what it does. But what this build processes is telling us is that those prototypes are generated for all functions in the eno and PDE files that don't already have prototypes. So it's gonna take this code, it's gonna concatenate it. Then it's gonna look for functions, you know, like fadeall and then it's gonna make a prototype for these and it's gonna stick it right up here. Okay, so why, why are we talking about that? Well here's why, this next sentence. In some rare cases, prototype generation may fail for some functions. To work around this, you can provide your own prototype for these functions. Now like in some rare cases, we don't know what those cases are necessarily. I'm sure there's probably a way to figure out what these cases are. I personally don't know what the rare cases are but I do know that I have seen it enough that it can be frustrating. So some cheap insurance for us is simply to write the prototypes ourself and then we can be sure that there aren't gonna be any errors when the compilation happens. So I'm just gonna make prototypes for all of these functions. (light music) And that's pretty much it. That's how you can use these different tabs. Now, you know, I'm not saying that you make a tab for every function. I mean that's what I did here. You know, that can, might be a little bit of overkill. Maybe I would have a maybe I'd have one file for like I'd call it setColors. And then I would have each of these functions inside the setColors tab. Maybe I'd do something like that. You know, I'm not saying this is the best way to organize it. I just wanted to use something to demonstrate different ways of doing it. But yeah, this is pretty much how it works. Hopefully this was helpful. And again, adding the tabs is really straightforward. Just new tab. You can rename tabs. In fact, what's kind of nice is you can rename the eno file. So right now, like see how it's called Tabbatha. Let's just go ahead and rename this. We're gonna rename it to Tabadaba. All right, so I renamed it to Tabadaba. And now when I come in here, notice that the enclosing folder got changed. The name of the enclosing folder is now Tabadaba which is what I just named that first tab to. So, well the best way to learn this stuff is to actually go do it. So hey, go open up your Arduino IDE. This is gonna take you all of two minutes. Use the blink sketch or like that and open up a tab. Try it out, you know, put it into practice. It'll help stick in your head and become something useful that maybe you can do in the future. Thanks again to NextPCB for sponsoring this video. make sure to click the link in the description and grab one of those limited time $1,050 off coupons. Well hey, if you enjoyed this, please like the video. Leave a comment If you have any questions. I'd love comments in the videos. and if you could subscribe to the channel, I would really appreciate it. Helps us bring you great content. Thanks again and have a great day. Bye. (light music)
Info
Channel: Programming Electronics Academy
Views: 50,916
Rating: undefined out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy, arduino ide tabs, arduino program, internet of things, organize code, arduino programming full course, arduino code for beginners, arduino projects for beginners, arduino project, arduino programming tutorial, arduino programming, arduino code, arduino code tutorial, arduino code upload
Id: HtYlQXt14zU
Channel Id: undefined
Length: 12min 18sec (738 seconds)
Published: Sun Apr 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.