How to Organize Code

preview_player
Показать описание
🤩 FREE Arduino Crash Course 👇👇

***Want to learn more? Check out our courses!***

***Get your Free Trial of Altium PCB design Software***

Arduino Sketch Build Process Website:

***Get the code, transcript, challenges, etc for this lesson on our website***

***We designed this circuit board for beginners!***

FOLLOW US ELSEWHERE
---------------------------------------------------
________________________________

If you are new to writing code with Arduino it can be confusing on how to organize the program. But once you know where stuff goes in an Arduino program it's actually pretty simple. In this lesson, you are gonna learn how to organize the key components of an Arduino sketch like includes, define, constants, global variables, functions, setup and loop. Let's dive in. All right, so here I am in the Arduino IDE and it doesn't matter which IDE you're using. You could be in the online, editor or maybe you're in the Arduino 2.0 IDE. It really doesn't matter or maybe you're in VS Code. It doesn't matter where you're programming this stuff. It's all gonna be the same as far as this organization goes. Okay, so we open up a new sketch and what Arduino does is it pre-populates two functions in here. It gives a setup and loop. We're gonna visit these later but right now I'm just gonna go ahead and delete that 'cause what I wanna do is start from just a blank slate. All right, so what I'm gonna do just in comments is kind of list out all the stuff in the order that we're gonna want it generally. All right, so here's the basic order, now of course every rule has exceptions and this is definitely a rule that has exceptions, but here's the rough order we wanna lay stuff out in an Arduino sketch. First you're gonna start off with including libraries. So if you have any libraries that you're using with your sketch, putting them at the very top is generally where you're gonna see them. So an easy way to include a library is just to go up to sketch, include library and then select the library you want to include. I'll use FastLED for this example and you'll see it just inserts this line include and then the name of the library right at the top. So I'll just go ahead and move this down to where I've got the comment. Of course, we could always just write this out on our own. The nice thing about using the shortcut up in sketch with the included library is that you know you're not gonna make any spelling errors. And as simple as that might be sometimes a spelling error can throw you for a loop for a little bit. All right, so you wanna include your libraries at the very top of the sketch. So the next thing you'll want after you include the libraries is to add your constants and any defines. And also you'll wanna have your global variables. Now you can have these intermingles, so you might have a constant and then you could hav

CONTINUED...

***About Us:***
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.

***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Рекомендации по теме
Комментарии
Автор

NIce refresher for an old software engineer. Nice explanations. I haven't written code in over 20 years but have no problem staying with you. I cut my teeth on FORTRAN, ALGOL, FORTRAN 77, S-FORTRAN, Assembler from various machines, also embedded, Pascal, Turbo Pascal, ANSI 'C', COBOL, Visual Basic, JavaScript - and that's when I became a full time project manager. But it's important to know what the 'softies' are going through and what can go wrong. All the best, Rob in Switzerland

RobWhittlestone
Автор

Well prototype functions is something new to me, fantastic educational video, thank you

jumadhaheri
Автор

Excellent. Lots to digest here. I have some printed sketches, I'll take a pencil to them and do some breakdown. Reverse engineering is how I've been doing this, nobody has thought me. You are doing Ok!

planker
Автор

You just established my workflow forever... Thank you!

sanamandingra
Автор

Excellent, I like your way of teaching from scratch. Thanks

mahmoodmohammad
Автор

Top of program, first comment is program purpose. Second comment(s) are the revision changes.

I put my user-defined functions after loop() (and have not used prototyping.) Seems to make editing easier as I am paging up and down less. Otherwise, based on 30 years of programming, I organize my programs as you do.

mr.goldenrod
Автор

Great walkthrough of the process 👍
Thanks for sharing your experience with all of us 👍😀

avejst
Автор

Thank you! You answered so many questions!

HollyHertig
Автор

I always pick up something new, that may not be the main focus of the video. Like the side information on Function Prototypes. Thanks!

KW-eipi
Автор

Thanks for the video. Super helpful as always🙏❤️

h-h
Автор

This was really helpful. Perhaps you might consider an episode about using different tabs to help organize code?

jon_raymond
Автор

Creative video, thanks for sharing it :)

RixtronixLAB
Автор

Excellent video!
This is one thing i think the Arduino IDE does wrong - by allowing a programmer to stick their user defined functions below the loop and secretly compensating for it is a bit misleading to newcomers. It wasn't until i used platform io which is more strict that i realised what was going on .
Any plans to make some videos on MicroPython ?

ianbertenshaw
Автор

I was kind of expecting a guide on how to break up code into multiple .cpp and .h files, but I guess that just amounts to a rehash of the tutorial on the arduino site on writing your own library, and it's effectively C++.

takatamiyagawa
Автор

Awesome video! What if my sketch gets so big it’s hard to find my functions? I created a sketch that the loop got so busy it was hard to figure it out so I created functions to break it up. Then I had so many functions that it was hard to find the function I wanted. How do I create my own libraries? Then how do I insert the code to jump to the library?

warrenscorner
Автор

Do you cover using other processors other than Arduino, like EPS32, STM ?

jamescullins
Автор

I like your code font, would you tell the name of the font ?

tonymai
Автор

Arduino appears to be an exception to C. It surprised me I can put a function below where it is used, even without prototyping.

xbadbeef
Автор

Isn't those neopixel legs need a 12v battery?

DIYRobotGirl
Автор

How do I install <Fast_LED.h> into my library? it is not in there.

sandwon