Читать книгу Arduino Sketches - Langbridge James A. - Страница 21

Part I
Introduction to Arduino
Chapter 2
Programming for the Arduino
Understanding Your First Sketch

Оглавление

Now that your sketch works and you have seen the results, it is time to have a closer look at the source code. This is presented step by step. The first part gives some interesting information:


Everything placed between the text /* and */ is considered to be a comment, a portion of source code that is ignored by the compiler. Everything within these markers will be ignored, so it is the best place to write natural language text about what the program does, or is doing. It is common to start a source code file with a comment, explaining what the application does. Just by looking at these few lines, you already have an idea about what the program will do.


This, again, explains what will happen using comments. Just like the /* and */ markers, when the compiler encounters the marker //, it will ignore everything else after that marker but only for that line. On the first line, the compiler encounters a comment marker and ignores the text. It then attempts to read in the second line but again encounters a comment and ignores that, too. On the third line, there is no comment; this is a real line of code.

It starts with the keyword int, short for integer. This is a variable declaration; it tells the compiler to reserve space for a variable, a named container that can change its contents. Because the variable was declared as an integer, it can hold only whole numbers between -32,768 and 32,767. This variable is named led. The compiler will assign the value 13 to the variable. Finally, the line is finished with a semicolon. In C, a semicolon marks the end of an instruction.

Now for the next part:


The first line is a comment. It explains what the next portion of the code will do.

The next line is interesting. The keyword void means an empty data type. The second word, setup, declares the name of a function. Because of the parentheses and curly brackets, you know that this is not a variable but a function. Functions are portions of code that can be called inside a program; instead of writing the same code dozens of times, it is possible to write it only once and have the program call this function as required. It is also a way of separating code for special needs.

Inside the parentheses, you would list any parameters for the function: these are variables that can be passed to the function. Because there is nothing inside the parentheses of setup(), there are no parameters. The function therefore does not need any data to run. Because the function was declared as void, it will not return any data either. When this function is called, it will do its job and then return without any data. But what exactly does it do?


Конец ознакомительного фрагмента. Купить книгу
Arduino Sketches

Подняться наверх