{"page":"\u003clink rel=\"stylesheet\" href=\"https://lessonplanet.com/assets/packs/css/resources-572d6a42.css\" /\u003e\n\u003clink rel=\"stylesheet\" href=\"https://lessonplanet.com/assets/packs/css/lp_boclips_stylesheets-f4d0de30.css\" media=\"all\" /\u003e\n\u003cdiv data-title='How to Organize Code' data-url='/boclips/videos/62a0e4808b9cc0130f1cb6e6' data-video-url='/boclips/videos/62a0e4808b9cc0130f1cb6e6' id='bo_player_modal'\u003e\n\u003cdiv class='boclips-resource-page modal-dialog panel-container'\u003e\n\u003cdiv class='react-notifications-root'\u003e\u003c/div\u003e\n\u003cdiv class='rp-header'\u003e\n\u003cdiv class='rp-type'\u003e\n\u003ci aria-hidden='true' class='fai fa-regular fa-circle-play'\u003e\u003c/i\u003e\nVideo\n\u003c/div\u003e\n\u003ch1 class='rp-title' id='video-title'\u003e\nHow to Organize Code\n\u003c/h1\u003e\n\u003cdiv class='rp-actions'\u003e\n\u003cdiv class='mr-1'\u003e\n\u003ca class=\"btn btn-success\" data-posthog-event=\"Signup: LP Signup Activity\" data-posthog-location=\"body_link_boclips\" data-remote=\"true\" href=\"/subscription/new\"\u003e\u003cspan\u003e\u003cspan\u003eGet Free Access\u003c/span\u003e\u003cspan class=\"\"\u003e for 10 Days\u003c/span\u003e\u003cspan\u003e!\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv class='rp-body'\u003e\n\u003cdiv class='rp-info'\u003e\n\u003cdiv aria-label='Hide resource details' class='rp-hide-info' role='button' tabindex='0'\u003e\u0026times;\u003c/div\u003e\n\u003ci aria-label='Expand resource details' class='rp-expand-info fai fa-solid fa-up-right-and-down-left-from-center' role='button' tabindex='0'\u003e\u003c/i\u003e\n\u003ci aria-label='Compress resource details' class='rp-compress-info fai fa-solid fa-down-left-and-up-right-to-center' role='button' tabindex='0'\u003e\u003c/i\u003e\n\u003cdiv class='rp-rating'\u003e\n\u003cspan class='resource-pool'\u003e\n\u003cspan class='pool-label'\u003ePublisher:\u003c/span\u003e\n\u003cspan class='pool-name'\u003e\n\u003cspan class='text'\u003e\u003ca data-publisher-id=\"30358947\" href=\"/search?publisher_ids%5B%5D=30358947\"\u003eProgramming Electronics Academy\u003c/a\u003e\u003c/span\u003e\n\u003c/span\u003e\n\u003c/span\u003e\n\u003c/div\u003e\n\u003cdiv class='rp-description'\u003e\n\u003cspan class='short-description'\u003eIf 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...\u003c/span\u003e\n\u003cspan class='full-description hide'\u003eIf 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 have a global and then another constant like that doesn't really matter. It's basically that these constants and defines in globals they're gonna be above your user-defined functions. They're gonna be above your setup and above your loop. And the reason they need to be above is because all of this stuff is gonna be depending on stuff that you say here. So you can't call a global variable before it's actually been defined and declared. \u003c/span\u003e\n\u003c/div\u003e\n\u003cdiv class='action-container flex justify-between'\u003e\n\u003cbutton aria-expanded='false' aria-label='Read more description' class='rp-full-description' type='button'\u003e\n\u003ci class='fai fa-solid fa-align-left'\u003e\u003c/i\u003e\n\u003cspan id='read_more'\u003eRead More\u003c/span\u003e\n\u003c/button\u003e\n\u003cdiv class='rp-report'\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv aria-labelledby='resource-details-heading' class='rp-info-section'\u003e\n\u003ch2 class='title' id='resource-details-heading'\u003eResource Details\u003c/h2\u003e\n\u003cdiv class='rp-resource-details clearfix'\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eCurator Rating\u003c/dt\u003e\n\u003cdd\u003e\u003cspan class=\"star-rating\" aria-label=\"4.0 out of 5 stars\" role=\"img\"\u003e\u003ci class=\"fa-solid fa-star text-action\" aria-hidden=\"true\"\u003e\u003c/i\u003e\u003ci class=\"fa-solid fa-star text-action\" aria-hidden=\"true\"\u003e\u003c/i\u003e\u003ci class=\"fa-solid fa-star text-action\" aria-hidden=\"true\"\u003e\u003c/i\u003e\u003ci class=\"fa-solid fa-star text-action\" aria-hidden=\"true\"\u003e\u003c/i\u003e\u003ci class=\"fa-regular fa-star text-action\" aria-hidden=\"true\"\u003e\u003c/i\u003e\u003c/span\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt class=\"educator-rating-title\"\u003eEducator Rating\u003c/dt\u003e\u003cdd\u003e\u003cdiv class=\"educator-rating-details\" data-path=\"/educator_ratings/rrp_data?resourceable_id=191183\u0026amp;resourceable_type=Boclips%3A%3AVideoMetadata\"\u003e\u003cspan class=\"not-yet-rated\"\u003eNot yet Rated\u003c/span\u003e\u003c/div\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eMedia Length\u003c/dt\u003e\n\u003cdd\u003e13:39\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eGrade\u003c/dt\u003e\u003cdd title=\"Grade\"\u003eHigher Ed\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eSubjects\u003c/dt\u003e\u003cdd\u003e\u003cspan\u003e\u003ca href=\"/search?grade_ids%5B%5D=259\u0026amp;search_tab_id=1\u0026amp;subject_ids%5B%5D=378077\"\u003eSTEM\u003c/a\u003e\u003c/span\u003e\u003c/dd\u003e\u003cdd class=\"text-muted\"\u003e\u003ci class=\"fa-solid fa-lock mr5\"\u003e\u003c/i\u003e3 more...\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eMedia Type\u003c/dt\u003e\u003cdd\u003e\u003cspan\u003e\u003ca href=\"/search?grade_ids%5B%5D=259\u0026amp;search_tab_id=2\u0026amp;type_ids%5B%5D=4543647\"\u003eInstructional Videos\u003c/a\u003e\u003c/span\u003e\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eSource:\u003c/dt\u003e\n\u003cdiv class='preview-source' data-animation='true' data-boundary='.rp-info' data-container='.rp-resource-details' data-html='false' data-title='Got questions about Arduino? We have the best Arduino tutorials, lessons, and courses that you\u0026#39;ll find, presented in a way you’ll love.  At Programming Electronics Academy we are all about teaching programming and electronics with Arduino.' data-trigger='hover focus'\u003e\n\u003cspan\u003eProgramming Electronics Academy\u003c/span\u003e\n\u003ci aria-hidden='true' class='fa-solid fa-circle-info channel-tooltip-icon' id='channel-tooltip'\u003e\u003c/i\u003e\n\u003c/div\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eDate\u003c/dt\u003e\n\u003cdd\u003e2022\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003ci aria-hidden='true' class='fai fa-solid fa-language'\u003e\u003c/i\u003e\n\u003cdt\u003eLanguage\u003c/dt\u003e\u003cdd\u003eEnglish\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eAudiences\u003c/dt\u003e\u003cdd\u003e\u003cspan\u003e\u003ca href=\"/search?audience_ids%5B%5D=371079\u0026amp;grade_ids%5B%5D=259\u0026amp;search_tab_id=1\"\u003eFor Teacher Use\u003c/a\u003e\u003c/span\u003e\u003c/dd\u003e\u003cdd class=\"text-muted\"\u003e\u003ci class=\"fa-solid fa-lock mr5\"\u003e\u003c/i\u003e2 more...\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003cdiv class='detail'\u003e\n\u003cdl\u003e\n\u003cdt\u003eUsage Permissions\u003c/dt\u003e\u003cdd\u003eFine Print: Educational Use\u003c/dd\u003e\n\u003c/dl\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv aria-labelledby='additional-materials-heading' class='rp-info-section'\u003e\n\u003ch2 class='title' id='additional-materials-heading'\u003eAdditional Materials\u003c/h2\u003e\n\u003cdiv class='additional-material'\u003e\n\u003ci aria-hidden='true' class='fai fa-solid fa-lock'\u003e\u003c/i\u003e\n\u003ca class=\"text-muted\" title=\"Video Transcript\" data-html=\"true\" data-placement=\"bottom\" data-trigger=\"click\" data-content=\"\u003cdiv class=\u0026quot;text-center py-2\u0026quot;\u003e\u003ca class=\u0026quot;bold\u0026quot; href=\u0026quot;/auth/users/sign_in\u0026quot;\u003eSign in\u003c/a\u003e or \u003ca class=\u0026quot;bold text-danger\u0026quot; data-posthog-event=\u0026quot;Signup: LP Signup Activity\u0026quot; data-posthog-location=\u0026quot;body_link_boclips\u0026quot; data-remote=\u0026quot;true\u0026quot; href=\u0026quot;/subscription/new\u0026quot;\u003eJoin Now\u003c/a\u003e\u003c/div\u003e\" data-title=\"Get Full Access\" data-container=\"body\" rel=\"popover\" tabindex=\"0\" href=\"/subscription/new\"\u003eVideo Transcript\u003c/a\u003e\n\u003c/div\u003e\n\u003cdiv class='additional-material'\u003e\n\u003ci aria-hidden='true' class='fai fa-solid fa-lock'\u003e\u003c/i\u003e\n\u003ca class=\"text-muted\" title=\"Video Preview\" data-html=\"true\" data-placement=\"bottom\" data-trigger=\"click\" data-content=\"\u003cdiv class=\u0026quot;text-center py-2\u0026quot;\u003e\u003ca class=\u0026quot;bold\u0026quot; href=\u0026quot;/auth/users/sign_in\u0026quot;\u003eSign in\u003c/a\u003e or \u003ca class=\u0026quot;bold text-danger\u0026quot; data-posthog-event=\u0026quot;Signup: LP Signup Activity\u0026quot; data-posthog-location=\u0026quot;body_link_boclips\u0026quot; data-remote=\u0026quot;true\u0026quot; href=\u0026quot;/subscription/new\u0026quot;\u003eJoin Now\u003c/a\u003e\u003c/div\u003e\" data-title=\"Get Full Access\" data-container=\"body\" rel=\"popover\" tabindex=\"0\" href=\"/subscription/new\"\u003eVideo Preview\u003c/a\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv aria-labelledby='concepts-heading' class='rp-info-section'\u003e\n\u003ch2 class='title' id='concepts-heading'\u003eConcepts\u003c/h2\u003e\n\u003cdiv class='clearfix'\u003e\n\u003cdiv class='details-list concepts' data-identifier='Boclips::VideoDecorator62a0e4808b9cc0130f1cb6e6' data-type='concepts'\u003eelectronics\u003c/div\u003e\n\u003cdiv class='concepts-toggle-buttons' data-identifier='Boclips::VideoDecorator62a0e4808b9cc0130f1cb6e6'\u003e\n\u003cbutton aria-expanded='false' class='more btn-link' type='button'\u003e\n\u003cspan\u003eShow More\u003c/span\u003e\n\u003ci aria-hidden='true' class='fa-solid fa-caret-down ml5'\u003e\u003c/i\u003e\n\u003c/button\u003e\n\u003cbutton aria-expanded='true' class='less btn-link' style='display: none;' type='button'\u003e\n\u003cspan\u003eShow Less\u003c/span\u003e\n\u003ci aria-hidden='true' class='fa-solid fa-caret-up ml5'\u003e\u003c/i\u003e\n\u003c/button\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv aria-labelledby='additional-tags-heading' class='rp-info-section'\u003e\n\u003ch2 class='title' id='additional-tags-heading'\u003eAdditional Tags\u003c/h2\u003e\n\u003cdiv class='clearfix'\u003e\n\u003cdiv class='details-list keyterms' data-identifier='Boclips::VideoDecorator62a0e4808b9cc0130f1cb6e6' data-type='keyterms'\u003earduino, arduino ide, arduino lesson, arduino tutorial, c++, microcontrollers, programming electronics academy, altium designer, arduino basics, arduino code, arduino coding, arduino functions, arduino ide programming tutorial, arduino lcd, arduino mp3 player, arduino program, arduino programming, code academy, how to, how to connect arduino to computer, how to organize, how to organize code, learn to code, organize code, vscode arduino, global variable, fast led, num leds, free trial, data type, refer back, function prototype, error messages, include library, global, type, function, order, back, written, fade, error, library, call, drive, inside, include, array, program, stuff, compiler\u003c/div\u003e\n\u003cdiv class='keyterms-toggle-buttons' data-identifier='Boclips::VideoDecorator62a0e4808b9cc0130f1cb6e6'\u003e\n\u003cbutton aria-expanded='false' class='more btn-link' type='button'\u003e\n\u003cspan\u003eShow More\u003c/span\u003e\n\u003ci aria-hidden='true' class='fa-solid fa-caret-down ml5'\u003e\u003c/i\u003e\n\u003c/button\u003e\n\u003cbutton aria-expanded='true' class='less btn-link' style='display: none;' type='button'\u003e\n\u003cspan\u003eShow Less\u003c/span\u003e\n\u003ci aria-hidden='true' class='fa-solid fa-caret-up ml5'\u003e\u003c/i\u003e\n\u003c/button\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv aria-labelledby='classroom-considerations-heading' class='rp-info-section'\u003e\n\u003ch2 class='title' id='classroom-considerations-heading'\u003eClassroom Considerations\u003c/h2\u003e\n\u003cdiv class='classroom-considerations'\u003e\u003cdiv class='fai fa-solid fa-bell'\u003e\u003c/div\u003eBest For: Explaining a topic\u003c/div\u003e\u003cdiv class='classroom-considerations'\u003e\u003cdiv class='fai fa-solid fa-bell'\u003e\u003c/div\u003eVideo is ad-free\u003c/div\u003e \n\u003c/div\u003e\n\u003cdiv aria-labelledby='educator-ratings-heading' class='rp-info-section'\u003e\n\u003ch2 class='title sr-only' id='educator-ratings-heading'\u003eEducator Ratings\u003c/h2\u003e\n\u003cdiv id=\"educator-ratings-root\"\u003e\u003c/div\u003e\u003cdiv id=\"all-educator-ratings-root\"\u003e\u003c/div\u003e\u003cdiv id=\"educator-rating-form-root\"\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv class='rp-resource'\u003e\n\u003cdiv aria-label='Show resource details' class='rp-show-info' role='button' tabindex='0'\u003e\n\u003ci class='fai fa-solid fa-align-left'\u003e\u003c/i\u003e\nShow resource details\n\u003c/div\u003e\n\u003cdiv aria-label='Video player' class='player' id='player-wrapper' role='region'\u003e\n\u003cdiv class='relative container mx-auto' id='lp-boclips-visitor-thumbnail'\u003e\n\u003ca class=\"block\" data-html=\"true\" data-placement=\"bottom\" data-trigger=\"click\" data-content=\"\u003cdiv class=\u0026quot;text-center py-2\u0026quot;\u003e\u003ca class=\u0026quot;bold\u0026quot; href=\u0026quot;/auth/users/sign_in\u0026quot;\u003eSign in\u003c/a\u003e or \u003ca class=\u0026quot;bold text-danger\u0026quot; data-posthog-event=\u0026quot;Signup: LP Signup Activity\u0026quot; data-posthog-location=\u0026quot;body_link_boclips\u0026quot; data-remote=\u0026quot;true\u0026quot; href=\u0026quot;/subscription/new\u0026quot;\u003eJoin Now\u003c/a\u003e\u003c/div\u003e\" data-title=\"Get Full Access\" data-container=\"body\" rel=\"popover\" tabindex=\"0\" aria-label=\"Play video: How to Organize Code\" href=\"/subscription/new\"\u003e\u003cimg class=\"resource-img img-thumbnail img-responsive z-10 lp-boclips-thumbnail w-full h-full lozad\" alt=\"How to Organize Code\" title=\"How to Organize Code\" onError=\"handleImageNotLoadedError(this)\" data-default-image=\"https://static.lp.lexp.cloud/images/attachment_defaults/resource/large/missing.png\" data-src=\"https://cdnapisec.kaltura.com/p/1776261/thumbnail/entry_id/1_fqw1dj5a/width/250/vid_slices/3/vid_slice/1\" width=\"315\" height=\"220\" src=\"data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs\" /\u003e\n\u003cspan aria-hidden='true' class='flex justify-center items-center bg-white rounded-full w-16 h-16 absolute top-1/2 left-1/2 -mt-8 -ml-8 cursor-pointer z-0 border-2 border-primary drop-shadow-md lp-boclips-thumbnail-playBtn'\u003e\n\u003ci class='fa-solid fa-play text-primary text-3xl ml-1 drop-shadow-xl'\u003e\u003c/i\u003e\n\u003c/span\u003e\n\u003c/a\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n"}