Arduino Sketches. Langbridge James A.
Чтение книги онлайн.

Читать онлайн книгу Arduino Sketches - Langbridge James A. страница 10

Название: Arduino Sketches

Автор: Langbridge James A.

Издательство: Автор

Жанр: Зарубежная образовательная литература

Серия:

isbn: 9781118919699

isbn:

СКАЧАТЬ a small program that is run every time the device starts. This is only one of the strong points of the Arduino system; the bootloader is always available to allow the programmer to reflash a program. Even if you accidentally flash a program that continuously crashes, you will always be able to reflash your Arduino, provided the bootloader is present.

      WARNING

      If you need more program space, you can delete the bootloader and place your own application at the start of the processor's instruction sequence. Doing this has the advantage of freeing the space used by the bootloader and using it for your own application. The bootloader is a small program, about 2 kilobytes in size. If you delete the bootloader, you can still reflash your Arduino, but more specialized equipment will be required.

      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?

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

      Текст предоставлен ООО «ЛитРес».

      Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.

      Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.

/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQCAwMDAgQDAwMEBAQEBQkGBQUFBQsICAYJDQsNDQ0LDAwOEBQRDg8TDwwMEhgSExUWFxcXDhEZGxkWGhQWFxb/2wBDAQQEBAUFBQoGBgoWDwwPFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhb/wAARCAAfAJIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKT
СКАЧАТЬ