Arduino Sketches. Langbridge James A.. Читать онлайн. Newlib. NEWLIB.NET

Автор: Langbridge James A.
Издательство: Автор
Серия:
Жанр произведения: Зарубежная образовательная литература
Год издания: 0
isbn: 9781118919699
Скачать книгу
by a human, and translating it into a binary file that can be understood by a processor. For this, some tools are required. The data written by humans is called source code, and because most source code is in text format, sometimes a simple text editor is enough. Most people go with an Integrated Development Environment (IDE), an augmented text editor with add-ons designed for developers. These add-ons can range from text auto-completion to debugging and often include tools to handle different types of source files, which contain source code. Some projects might use only one file, but large projects can sometimes have hundreds of files, if not thousands. After the source code is written, a compiler must be used, which reads in the source code and creates one or more binary files. These binary files are later uploaded onto the Arduino and run by the microcontroller.

      Arduino developed all the tools required to get straight to work. With a different embedded system, you may have to make a choice of an IDE, install a compiler, and sometimes even a flasher, and spend precious hours setting up the system. With Arduino, this isn't the case; everything is delivered in a simple package and contains everything needed, from writing your programs to flashing the final binary file.

      An Arduino program is known as a sketch. There are several definitions of the word sketch such as a brief literary composition or a brief musical composition. Whatever your preference, an Arduino sketch is like a work of art; you, the artist, gather and assemble elements to create your masterpiece. Google X engineer Jeremy Blum, author of the book Exploring Arduino (Wiley, 2013), said,

      I believe that creative engineering is indistinguishable from fine artwork.

      The Arduino will be your canvas; you are on your way to making something amazing using sketches and electronics. Your only limitation will be your imagination.

      Installing Your Environment

      The first thing that you need to do is to install the Arduino IDE. The Arduino IDE is a fully integrated piece of software written in Java. Java can run on multiple platforms, and the IDE is available for Windows, Mac OS X, and Linux. You can get the Arduino IDE free of charge at the Arduino website:

      http://arduino.cc/en/main/software

      On this page, you will most likely have several options. The latest stable version will always be listed first. Next, any beta versions available will be listed. Beta versions are test versions that might not be up to the quality of a finished version but that add functionality; it will be up to you to decide if you want to use it. Beta versions sometimes support more hardware, and if you use the latest Arduino boards, you might not have a choice.

      Also listed on the site are nightly builds and builds for specific hardware. Nightly builds are installers that are generated every night that contain the latest updates but may in some rare cases also have bugs. Specific builds are builds created for a single board in mind. At the time of writing, there is an IDE available for the Intel Galileo, an Arduino compatible board designed and manufactured by Intel that does not use the same compiler.

      Downloading the Software

Time to get to work! You have to download the software, so find the latest version and download it. Figure 2.1 shows what the Arduino site looks like on my development computer.

image

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