The full Arduino Workshop in step-by-step format can be found here: https://core-electronics.com.au/tutorials/arduino-workshop-for-beginners.html In this section, we'll be learning about how to properly structure code using indentations, nested levels, and semicolons.

Transcript

The most important thing to understand when writing your program is how the code should be structured. You can't just put things anywhere; each section of the code has its own place. Writing code is all about logic, and as logic would suggest, your program will run from top to bottom in a linear fashion. Of course, the program can jump to different points in the code while running if it's directed to do so. However, unless it is told to do so, it will run from top to bottom. When you open the Arduino IDE, at the very top of your program is where you can create variables, include libraries, and create macro definitions for your code. We'll get to all of that later on, but for now, this area is blank by default. When you create a new Arduino sketch, you'll be greeted with some initial code which makes it easier to get started and shows how you should structure your program. This code, as you can see, doesn't actually do anything, but it provides the two functions that every Arduino sketch needs: setup and loop.

We'll take a more in-depth look at functions in a future video. Simply put, your setup function is the first thing your program does when it starts to run. Everything inside the setup function highlighted here will run once and only once. When it reaches the end of the setup function, unless it's told otherwise, it will move straight on to the void loop function. The loop function will repeat over and over again until it is told otherwise, or the device is turned off or power is removed. The goal of any programmer is to create code that is elegant. A well-written piece of code will be easy for others to understand, even if they don't fully get every small detail. They can look at it and understand the intended outcome and process. Open up the Arduino IDE because one of the biggest things that helps with this is the styling and formatting of your code.

One of the biggest things you can do to increase the readability of your code is to use proper indentation. So what do we mean by indentation? A good text editor will help you out a lot here, but the general rule is that each level or nest of your code should be indented once further to the right than the previous level. For example, everything in our code starts at a zero indentation, so the function void loop or void setup sits all the way to the left. However, when we write code inside the loop function, it gets a new level, so it gets indented once further to the right. You can see we press the tab here, and it keeps everything in line. Another statement or function inside the loop would be indented twice to the right, or once again, and so on and so forth. As you'll discover in the next chapter, nested functions and statements can easily become quite confusing if you don't use the best practices from the start.

The other thing you should always remember when writing your code is to use semicolons. A semicolon is known as a terminating character, and it allows the compiler to differentiate each one of your code lines because the compiler isn't super smart. It does exactly what you tell it to, and if there's an error, it'll mirror that. As you'll see further on in the course, almost every line requires a semicolon to terminate it. However, some lines that you write, such as logic statements and definitions, don't. This is because that line isn't actually telling your microcontroller to perform an instruction or an action; it merely controls the flow of your code. The most common compilation error when you're starting out with Arduino, or really any programming language that uses semicolons for that matter, is forgetting syntax elements such as semicolons, brackets, and spaces. You will see examples of this all the way through the course, so remember to keep that goal in mind: Have I formatted my code as efficiently as possible?

If you don't quite understand what's going on here, don't worry. This is designed to present a general and broad overview of some syntax and styling functions to make it much easier for you later on. Once you've gone through and watched some of the courses on variables and digital writes and digital reads, go back and take a look at this and double-check that you've got those styling and formatting methods correct. This will make your code so much easier to read and understand once you get to more complex projects and applications.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.