How to Organize Code

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 เม.ย. 2022
  • 🤩 Check out this Free Arduino Course 👇
    bit.ly/get_Arduino_skills
    **Want to learn more? Check out our courses!**
    bit.ly/3DQBw4J
    **Get your Free Trial of Altium PCB design Software**
    www.altium.com/yt/programming...
    Arduino Sketch Build Process Website:
    arduino.github.io/arduino-cli...
    **Get the code, transcript, challenges, etc for this lesson on our website**
    bit.ly/38kpPrC
    **We designed this circuit board for beginners!**
    Kit-On-A-Shield: amzn.to/3lfWClU
    SHOP OUR FAVORITE STUFF! (affiliate links)
    ---------------------------------------------------
    We use Rev Captions for our subtitles
    bit.ly/39trLeB
    Arduino UNO R3:
    Amazon: amzn.to/37eP4ra
    Budget Arduino Kits:
    Amazon:amzn.to/3C0VqsH
    Multimeter Options:
    Amazon: amzn.to/3rRo3E0
    Helping Hands:
    Amazon: amzn.to/3C8IYXZ
    Soldering Stations:
    Amazon: amzn.to/2VawmP4
    AFFILIATES & REFERRALS
    ---------------------------------------------------
    ►Audible Plus Free trial: amzn.to/3j5IGrV
    ►Get LastPass: lastpass.wo8g.net/b3N1vk
    ►Join Honey- Save Money bit.ly/3xmj7rH
    ►Download Glasswire for Free:bit.ly/3iv1fql
    FOLLOW US ELSEWHERE
    ---------------------------------------------------
    Facebook: / programmingelectronics...
    Twitter: / progelecacademy
    Website: www.programmingelectronics.com/
    ________________________________
    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...
    bit.ly/38kpPrC
    **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.**

ความคิดเห็น • 56

  • @HollyHertig
    @HollyHertig 4 หลายเดือนก่อน +1

    Thank you! You answered so many questions!

  • @RobWhittlestone
    @RobWhittlestone 2 ปีที่แล้ว +2

    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

  • @avejst
    @avejst 2 ปีที่แล้ว +3

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

  • @jumadhaheri
    @jumadhaheri 2 ปีที่แล้ว +5

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

  • @mahmoodmohammad7241
    @mahmoodmohammad7241 2 ปีที่แล้ว +1

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

  • @h-h1859
    @h-h1859 2 ปีที่แล้ว +4

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

  • @planker
    @planker ปีที่แล้ว +2

    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!

  • @jon_raymond
    @jon_raymond 2 ปีที่แล้ว +9

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

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว +5

      Thanks so much, Jon! You must be reading minds because we just recorded a video on using tabs to help organize code, which will be released later this month. Thanks a ton for the suggestion; if you have any other video ideas, I would love to hear them!

    • @jumadhaheri
      @jumadhaheri 2 ปีที่แล้ว +1

      @@programmingelectronics yes please my currant WIFI clock is 3000 lines of code and any adjustment also a nightmare in Arduino IDE

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว

      Thanks ​@@jumadhaheri !

  • @KW-ei3pi
    @KW-ei3pi 9 หลายเดือนก่อน

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

  • @richardfurniss8091
    @richardfurniss8091 2 ปีที่แล้ว +3

    That explains a lot, Thanks

  • @Bianchi77
    @Bianchi77 2 ปีที่แล้ว +2

    Creative video, thanks for sharing it :)

  • @cursoderobotica
    @cursoderobotica 2 ปีที่แล้ว +2

    I love it!!! Cheers!!

  • @ZookeeperJohnG
    @ZookeeperJohnG 2 ปีที่แล้ว +1

    Super helpful!

  • @jamescullins2709
    @jamescullins2709 ปีที่แล้ว

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

  • @joshuapitong899
    @joshuapitong899 ปีที่แล้ว +1

    Thank you.😊🙌

  • @mr.goldenrod291
    @mr.goldenrod291 2 ปีที่แล้ว +4

    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.

  • @ianbertenshaw4350
    @ianbertenshaw4350 2 ปีที่แล้ว +4

    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 ?

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว

      Thanks so much! Same for me, I really had no idea what was going on behind the scenes until I ran into an issue with not having a prototype written.
      Thanks for the MicroPython recommendation! No current plans but it’s something that’s has been interesting me for a while.

    • @pmanolak
      @pmanolak 2 ปีที่แล้ว +1

      IDE well allow users to put their functions at the end. Actually the sequence does not matter. we do the same in other languages too. p.e. PHP, JavaScript.

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว +1

      You're right, in most cases the sequence doesn't matter as the IDE should be auto-generating those functions prototypes for you.

  • @kabandajamir9844
    @kabandajamir9844 2 ปีที่แล้ว +2

    So nice

  • @tonymai124
    @tonymai124 ปีที่แล้ว +1

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

    • @programmingelectronics
      @programmingelectronics  ปีที่แล้ว

      I think it is just the Arduino IDE default which i believe is called "Monoco"

  • @DIYRobotGirl
    @DIYRobotGirl ปีที่แล้ว

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

  • @warrenscorner
    @warrenscorner 2 ปีที่แล้ว +2

    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?

    • @jovialcupid9687
      @jovialcupid9687 2 ปีที่แล้ว +1

      U can make new tabs, no need for library

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว +1

      We have a video coming out soon about adding tabs in the Arduino IDE to help organize code better - this is a different approach than creating a library, but I think still pretty helpful depending on the use case. If the functions are all related and share/act on similar data, then making a C++ class may be the way to go.

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว

      Tabs are great! Thanks for adding this.

  • @takatamiyagawa5688
    @takatamiyagawa5688 2 ปีที่แล้ว +2

    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++.

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว

      On a slightly similar vein (but definitely different), we do have a video coming out about using tabs in the Arduino IDE to help organize code. When you add a tab, it creates an additional .ino file in the sketchbook folder and can help with organization.

  • @0x8badbeef
    @0x8badbeef 10 หลายเดือนก่อน

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

    • @programmingelectronics
      @programmingelectronics  10 หลายเดือนก่อน

      The Arduino IDE has a build process that automatically creates prototypes for you (but sometimes it fails ;), which is why I usually always create prototypes for my functions. Thanks for the comment and for watching!
      arduino.github.io/arduino-cli/0.20/sketch-build-process/

  • @FioBrio
    @FioBrio 2 ปีที่แล้ว +7

    You should use Arduino IDE 2.0

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว +1

      The Arduino IDE 2.0 is great! I love code completion, the movable serial monitor window, and moveable error console (among many other things). But, I am still having an issue with a lag in the serial monitor. When that gets worked out, I will probably make the switch.
      What I still like about Arduino IDE 1.0 is that it is really simple, which can be a huge advantage for beginners.

  • @sandwon
    @sandwon 2 ปีที่แล้ว +1

    How do I install into my library? it is not in there.

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว

      Great question! If you go to tools > library manager and then search for the FastLED library you can one click install it there.

    • @sandwon
      @sandwon 2 ปีที่แล้ว +1

      @@programmingelectronics Found it .Thanks

    • @programmingelectronics
      @programmingelectronics  2 ปีที่แล้ว

      @@sandwon Great! Glad that helped!