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

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

Название: Arduino Sketches

Автор: Langbridge James A.

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

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

Серия:

isbn: 9781118919699

isbn:

СКАЧАТЬ Figure 2.1 The Arduino download page

      Windows users have a choice between an installer and an archive. For the installer, simply download the installer, double-click it, and follow the instructions. For more information on installing, please see the Arduino website on installing: http://arduino.cc/en/Guide/HomePage.

      Mac OS X and Linux users have to download an archive. Simply unpack the archive using your normal tool, and double-click the Arduino icon inside the newly created folder. Everything required is inside this folder.

      If you have an operating system that is not listed, or if you are curious about the source code, a source code bundle is also available. You could compile the source code yourself.

      Some Linux distributions might bundle the Arduino IDE directly; others might require external repositories. Refer to your distribution's forums or look at Arduino's Playground website, a community edited wiki, at: http://playground.arduino.cc.

      Running the Software

Once you have downloaded and installed the software, open the application. If everything went well, you should have a window that looks like the one in Figure 2.2.

image

Figure 2.2 Empty sketch

      This is the Arduino IDE, where you will design your sketches. The main window is the sketch editor, which is where you write your code. At the bottom is the status window; you receive information on compilation, uploads, or code errors. In the bottom right of the screen is the device information panel, which shows the device that you are using, as well as the serial port it is connected to.

      The sketch editor isn't just a simple text editor; the editor colors and formats text depending on what you write. Comments are greyed out, data types are written in color, and so on. This provides a nice, easy way to read and write source code.

      Using Your Own IDE

      The Arduino IDE is a capable environment, but some people may want to use their own IDE, either for preference or simply because they are used to another environment. The Arduino community has worked hard on porting the tools to other programs, and you can find a complete list on the Arduino Playground. Eclipse, CodeBlocks, Kdevelop, and the command line are just a few of the environments proposed. Although this book concentrates on the Arduino IDE, check out other IDEs. For more information see http://playground.arduino.cc/Main/DevelopmentTools.

      Your First Program

It's time to dive in! By default, Arduinos come with a default sketch called Blink. This sketch will blink the on-board LED connected to pin 13, available on most Arduinos. Just plug a USB cable into your computer and your Arduino, and after a few seconds you will see the LED blink, telling you that everything went well. Arduinos are all about getting things done, and what better way to show you just how easy they are than to run your first program. Your first sketch will look like Listing 2.1:

Listing 2.1: Your first sketch

      If this source code doesn't make much sense to you, don't worry; everything will be explained a little later. Seasoned C developers might have a few questions, which will also be answered later.

      The previous sketch is an entire program. You can either type it in or use the Arduino IDE directly; this code listing is actually an example from the Arduino IDE. To open it, go to File ➪ Examples ➪ 01.Basics ➪ Blink, and a new window will open with the code. This sketch has comments, text zones where the user can write about what he is intending to do, indicated by // at the beginning of the line. Have a quick read through, and try to see what the program is doing.

When you are ready, it is time to upload your first program! Uploading means installing the binary code onto the Arduino board. Make sure your Arduino board is connected to your development computer via USB. For this example, use an Arduino Uno or Arduino Mega. This code can run on all the Arduinos, so feel free to use whichever you have. To upload the program, a few simple steps must first be completed. The IDE needs to know what type of board is connected. First, go into the menu; Tools ➪ Board, and select your board. As you can see, there are a lot of different boards to choose from. Select the entry that corresponds to your board; in this example, I have an Arduino Mega 2560, as illustrated in Figure 2.3.

image

Figure 2.3 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 СКАЧАТЬ