Now we are going to have a closer look at one of the template examples that you have recently downloaded on your board. For instance, Blink. Firstly, I would like to pay you attention at the fact that any programming language has its own set of rules which one needs to follow so that the compiling program would understand what
you mean. There is so-called syntax. Just as you write essays in your native language, and use punctuation, paragraphing and other methods, allowing better readability of the text, the development environment and the programming language have a set of requirements to what is being written. Otherwise it just would not understand what it’s all about, and in the end it could create a faulty computer code. For one thing, at the beginning of the sketch, we can see some grey text, which looks as if it was written in an ordinary
human language. In truth, this is very much so. These are comments. Comments are at the very least a bon ton when you register your code and as mаximum, a useful habit to take up. Because even if you are familiar with the programming language, when some time passes after you had written the code, you begin to forget what exactly you meant when you were writing it. This is why it is useful to comment your coding in a familiar language, be that English or your native language. In order for the compiling program not to take in the code as one written in a
programming language, you need to somehow separate it from the code. We can see at the beginning one of the ways of writing comments, in particular
delimited comments, which start with symbols « /*» and end with « */ ». Anything placed in between these markers will be ignored by the compiling program and will not be turned into a computer code. There is also a way to compile a single-line comment. It starts with « // », here we can see such line. Let’s move on. We can see two sections in the program — setup and loop. You might have already noticed them when you were opening an empty sketch. This template was there. These sections are an integral part of any Arduino program. If anyone of them is absent, the compiling program will simply respond that it does not agree with the present
state of affairs and that it needs a setup or a loop. In reality, there are two functions determined here. We will talk about the function determination in detail in the upcoming
lectures, and now you are just getting acquainted with it. Consequently, both these fragments are described according to the rules of the function. So what’s important for us at this point? The content of each function is limited by curly brackets. In fact, curly brackets serve as a way to point to the compiling program at a certain finished fragment of a program. In our case, this is the determination of a function. It starts with an opening curving bracket and it ends with a closing curving bracket. Later today, we are going to look into a different kind of a program fragment where curving brackets are also used. Do not forget about them and make sure they always come in a pair. This means that every opening curving bracket should have a closing curving bracket. And don’t forget about inclusion. If inside one of the fragments incased in curly brackets there is another such fragment, you need to first close the internal curly brackets and then the external
ones. The development environment is a real aid for us here. If we place the cursor next to one of the brackets, like I’ve just done at the end of the setup, the corresponding correlative bracket will
be highlighted. As we can see, the opening curly bracket has been highlighted. Let’s see what we have in the Setup section description. Here we only have one line, which helps us to invocate the pinMode function. What is exactly function invocation? A function is a certain fragment of the code, which is separately described and has a name
assigned to it, as well as its own composed function. The pinMode function is described in the Arduino standard library, which we don’t even usually happen to see. The development environment knows about it, and we just call the needed standard
functions. pinMode is responsible for the tuning of the mode, in which a certain port is going to function in this program. The thing is that with Arduino, the majority of the ports can both serve
as inputs and outputs. As a result, they either receive an external electric signal or send it externally. However, in each case, we need to determine in which direction the port is going to operate. When we call a certain function, we can convey a set of parameters to it, or specifications about the way it should be operating at the moment. These are the parameters, which are enumerated in the round brackets and are separated from each other with a comma. This is one of the basic syntax rules and you shouldn’t forget about it. What do we convey to the pinMode function? The first parameter is conveyed through the pin number we would like to set up. In our case, this is pin 13, because it has an in-built LED. This means that if we connect an external device to port 13, it will also receive and display voltage, the LED or the motor could be on, but simultaneously with this, the built-in LED will also be on. The second parameter, which comes after the comma, is the area of the port’s activity. In our case, this is OUTPUT. Please, note that this key word – OUTPUT – is written in capital letters. If you try to write it in lower-case letters, it will not be a good idea, because the compiling program will not understand this. You also need to pay attention to a very important punctuation mark – a
semi-colon. You should start with checking whether or not there is a semi-colon in the given examples at the end of the line. If it is there, you must also include it in your code. If it isn’t there, you don’t have to include it. You will subsequently understand when it is necessary to put it or not. Let’s see the description we have in our loop. Here we can see some other function calls. They are also standard functions defined in the Arduino library, with the first one being digitalWrite. It takes two things as parameters. The first one is the number of the contact with which we are currently
working, as well as the condition it needs to be brought to. After this function has been performed, the 13th contact will be featuring a high level of voltage. By high we mean 5 V. The next standard function is delay. It is needed so that the controller doesn’t perform any actions during a certain period of time. This exact period of time is communicated to it as a parameter. 1000 milliseconds is 1 second. Thus, we have turned on the voltage on the 13th contact, and since this contact has an LED, it is now on. Then during 1000 milliseconds, or 1 second, nothing is happening. Next, we are again calling digitalWrite function telling it to work with the 13th contact. This time it has to convey a low voltage level. You need to press 0 V – on and wait again for 1 second. After that, the loop will be performed from the beginning, and we shall see an endless cycle of LED blinking. For one second it is on together with the 13th contact, and for one second
it is off. I would like to draw your attention to the parameters which we have turned into the delay function. 1000 milliseconds. In this case, we have used a numeric literal, in other words, we have just written a number. Instead, we could have written any expression here. An expression in programming is something… that can be calculated. Well, we don’t need to calculate 1000, because we have already written “1000”. This could be an arithmetic expression, for instance: 1000 divided by 3. This can be calculated, and we will get the result of 333,(3). This may mean that some value stored in the memory has been used. However, this we shall discuss later. Let’s sum up what we have learnt thanks to this simple example. Firstly, we know that any Arduino program has setup and loop sections, which are in reality defining functions. In addition, setup is a fragment which is performed only once as the startup of the controller. In other words, there are always some settings there. Like now, for instance, we have made an output out of the 13th pin. However, loop, in its turn, will be running again and again. For that matter, we will turn on the 13th contact where we have the LED, wait for one second, turn the 13th contact off, and then wait for another
second. And we will get LED blinking in an endless cycle. At this point, we can finally proceed with traffic light programming.