Arduino Sketches. Langbridge James A.

Читать онлайн.
Название Arduino Sketches
Автор произведения Langbridge James A.
Жанр Зарубежная образовательная литература
Серия
Издательство Зарубежная образовательная литература
Год выпуска 0
isbn 9781118919699



Скачать книгу

Arduino IDE with the Arduino Mega 2560 selected

Next, the IDE needs to know how the board is connected to your computer. Using the Tools ➪ Serial Port menu, you can select the proper connection. On a Windows machine, the board will appear as a COM port. On a Mac, the Arduino connection will start with “/dev/tty.usbmodem.” My development machine is a Linux system, and in this case the Arduino is connected to /dev/ttyACM0. On some systems, there might be several serial ports listed. Figure 2.4 illustrates me selecting my port.

image

Figure 2.4 Arduino IDE with the Arduino Mega 2560 serial port selected

      That's it – as far as configuration goes. You have to do this only once; the Arduino IDE remembers your preferences and keeps them for the next time. You will need to change your settings if you change boards or plug the board into a different USB port.

      Next, you may optionally verify the source code. The verification stage actually compiles the source code; the compiler will warn you if anything goes wrong. If there is a problem, the IDE shows a message at the bottom of the screen, indicating a line number and the cause of the problem. For this example, the compiler shouldn't complain, and it will compile your application. To compile, you must click the Verify button (the check mark) in the top left of the IDE or go into the menu Sketch ➪ Verify/Compile. There is also a keyboard shortcut: Ctrl+R.

There is now one final step: you must upload the program onto your Arduino. Simply click the Upload button next to the Verify button, or go to the menu item File ➪ Upload. Again, a keyboard shortcut is available: Ctrl+U, as shown in Figure 2.5. The upload process also re-verifies the source code before uploading.

image

Figure 2.5 Successful upload

      The Arduino IDE now attempts to contact the Arduino board and transfer the program into the microcontroller's flash memory. A message at the bottom should soon display the text “Done Uploading”. Now look at your Arduino board. Next to the USB connector, a small LED should be blinking; the same one used to verify that your Arduino was working in the beginning of the chapter. This time, it should be blinking two to three times per second. Congratulations! You have now successfully uploaded your first Arduino program!

      The program has now been written into flash memory, but what does that mean? Like a program on a computer, it has been “installed” into the nonvolatile memory and will be executed every time you turn on the Arduino, so try that right now. Unplug your Arduino from the USB port, wait a few seconds, and then plug it back in. The Arduino will be powered again from the USB port, and after a few seconds, the LED will start to flash. Your program is running.

      Although it may appear that the Arduino has simply run your program, it hasn't done only that. Arduinos contain something called a bootloader, 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