ebook img

Pro Arduino PDF

305 Pages·2013·4.561 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro Arduino

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Authors �������������������������������������������������������������������������������������������������������������xvii About the Technical Reviewer �������������������������������������������������������������������������������������������xix Acknowledgments �������������������������������������������������������������������������������������������������������������xxi Introduction ���������������������������������������������������������������������������������������������������������������������xxiii ■ Chapter 1: Arduino 1�0�4 Core Changes �����������������������������������������������������������������������������1 ■ Chapter 2: Arduino Development and Social Coding �����������������������������������������������������������15 ■ Chapter 3: openFrameworks and Arduino �����������������������������������������������������������������������47 ■ Chapter 4: Android ADK ���������������������������������������������������������������������������������������������������63 ■ Chapter 5: XBees �������������������������������������������������������������������������������������������������������������91 ■ Chapter 6: Simulating Sensors ��������������������������������������������������������������������������������������111 ■ Chapter 7: PID Controllers ���������������������������������������������������������������������������������������������129 ■ Chapter 8: Android Sensor Networks ����������������������������������������������������������������������������143 ■ Chapter 9: Using Arduino with PIC32 and ATtiny Atmel Chips ��������������������������������������169 ■ Chapter 10: Multiprocessing: Linking the Arduino for More Power ������������������������������189 ■ Chapter 11: Game Development with Arduino ���������������������������������������������������������������209 ■ Chapter 12: Writing Your Own Arduino Libraries �����������������������������������������������������������237 ■ Chapter 13: Arduino Test Suite ��������������������������������������������������������������������������������������259 Index ���������������������������������������������������������������������������������������������������������������������������������283 v Introduction Since its release, Arduino has become more than just a development platform; it has become a culture built around the idea of open source and open hardware, and one that is reimagining computer science and education. Arduino has opened hardware development by making the starting skills easy to obtain, but retaining the complexities of real-world application. This combination makes Arduino a perfect environment for school students, seasoned developers, and designers. This is the first Arduino book to hold the title of “Pro,” and demonstrates skills and concepts that are used by developers in a more advanced setting. Going beyond projects, this book provides examples that demonstrate concepts that can be easily integrated into many different projects and provide inspiration for future ones. The focus of this book is as a transition from the intermediate to the professional. xxiii Chapter 1 Arduino 1.0.4 Core Changes If you are writing sketches, creating your own libraries, or making your own Arduino-compatible boards, the Arduino 1.0.4 changes will affect you. Many of the changes optimize the Arduino IDE for improved workflow and customization. Changes to the IDE include the removal of unused buttons, and the placement of the Compile and Upload buttons next to each other. The Arduino IDE is now multilingual; you can pick a custom language for the editor. These changes are only the visible portions—with these updates, the Arduino team took the opportunity to make significant and code-breaking changes in order to deliver a more consistent and complete Arduino API. The core libraries of the Arduino Core API have been overhauled as well. Additional improvements include better support for making your own Arduino variations and the ability to integrate programmable USB devices with Arduino. This chapter will go through these changes, what they mean, and how they will affect your code. The changes break down into the following categories: • Arduino IDE • Sketches • API Core • Core libraries • Variant support for Arduino-derived boards Changes to the Arduino IDE The original file extension for Arduino was .pde. This is the Processing application file extension. If you had both programs installed, Arduino files would be opened in the Processing program. Now, after the updates, Arduino sketches have their own extension: .ino. Therefore, mysketch.pde is now named mysketch.ino. Double-click the file name, and Arduino launches. You can change the preferences to support the older PDE extension, but by default, PDE files simply open. Files will not be renamed to .ino unless you change the setting in the preferences. The Arduino IDE editor now has line numbers in the lower-left corner, as shown in Figure 1-1. Compile is the first button, and the second button is Upload. The lower-right corner shows the selected board and what port it is connected to. These changes make it possible to quickly debug simple errors by identifying the line of code, verifying the correct serial port, and establishing whether the board is connected. 1 Chapter 1 ■ arduino 1.0.4 Core Changes Figure 1-1. Updated main window for the Arduino 1.0.x environment Look now at the Preferences panel (File ➤ Preferences), shown in Figure 1-2. I always use verbose output when I’m looking for errors in the compile process. The verbose-output feature has been moved to the Preferences panel, whereas before it could be triggered by pressing Shift plus the Compile button. The Preferences panel now enables you to resize the compile output for easier reading. Figure 1-2. Updated Preferences panel for the Arduino 1.0.x environment 2 Chapter 1 ■ arduino 1.0.4 Core Changes The location of the preferences.txt file is listed in the Preferences dialog box. It is good to know this because you may need to edit this file. Changes to Sketches Whenever you write an Arduino sketch, you are using the core functions and collection of objects that are always accessible, without needing to include external libraries in your sketch. For instance, Serial can be used without having to declare it. The Arduino IDE preprocesses the Arduino sketch before compiling. This process includes the Arduino.h file from core. Some of the files from core have to be included manually, as the Ethernet core does. The Ethernet core features are needed for the Arduino Ethernet board, but because not all Arduino boards have Ethernet, the files are available but not automatically included. Arduino achieves its simplicity by preprocessing the sketch and automatically generating a basic functional set. So, you never have to worry about including Arduino.h and creating header files for sketches, unless you create your own Arduino libraries. Arduino libraries have to be written in standard C/C++; I will cover their creation later, in Chapter 14. Here, you will examine how the default core functionality has changed. Then the chapter will cover how these changes have affected the default libraries that come with Arduino. These default libraries have been replaced by new variants with new features. Also, WProgram.h has been change to Arduino.h. API Updates This section will discuss the changes to the API. pinMode pinMode has been updated to support INPUT_PULLUP. This adds clean support for creating buttons and switches that are active high by default, and when activated pulled low. Listing 1-1 shows an example. Listing 1-1. pinMode INPUT_PULLUP Resistor Feature setup() { Serial.begin(9600); pinMode(10, INPUT); digitalWrite(10, HIGH); int val = digitalRead(10); Serial.print(val); } In Arduino 1.0.x you can do it this way: setup() { Serial.begin(9600); pinMode(10, INPUT_PULLUP); int val = digitalRead(10); Serial.print(val); } 3 Chapter 1 ■ arduino 1.0.4 Core Changes This approach has the benefit of making the pinMode set the default value as needed. Also, using the internal pull-up resistors removes the need to use external pull-up resistors, allowing you to remove parts from your project. Return Types Return types have been updated to return the size of data using size_t, which is an unsigned integer that is platform dependent. size_t is included from stdio.h in the Print.h header. This returns a size type for the data printed. You can use this to check the quantity of data returned for iterating. When writing your own libraries that print custom data, you would use size_t as the return value. uint_8 Several functions now take and return uint_8, which is a universal 8-bit integer that allows for cross-platform compatibility. Arduino API Core 1.0.4 Now let’s look at the changes in the Arduino API Core. Arduino.h If you are using standard AVR GCC system libraries or writing your own library, it’s important to know the Arduino library. Arduino.h now includes all the values from wiring.h. If you are already programming with C/C++, it’s good to know which functions are already available, so that you don’t include the libraries twice. Arduino.h includes the libraries shown in Listing 1-2, so you don’t need to include them in your own sketches. Listing 1-2. New Headers Automatically Included in Arduino.h #include <stdlib.h> #include <string.h> #include <math.h> #include <avr/pgmspace.h> #include <avr/io.h> #include <avr/interrupt.h> #include "binary.h" #include "WCharacter.h" #include "WString.h" #include "HardwareSerial.h" #include "pins_arduino.h" You never have to duplicate the libraries in your own sketches. They are automatically included for your use. The preprocessing compiles Arduino.h, and then combines the sketch with a file called main.cpp. This file contains the implementation for void setup() and void loop(). In fact, it’s short enough to show in Listing 1-3. 4 Chapter 1 ■ arduino 1.0.4 Core Changes Listing 1-3. The New Version of main.cpp #include <Arduino.h> int main(void) { init(); #if defined(USBCON) USBDevice.attach(); #endif setup(); for (;;) { loop(); if (serialEventRun) serialEventRun(); } return 0; } Looking at the source, there are two interesting items to note. First, main.cpp now looks to see if a USB connection is defined and attached. Second, the void loop() code runs, and then a serial event is checked for. If the event is found, then the code runs it. Updated Serial Object Sending data from serial is now asynchronous. The serial object depends on a parent object called stream, so it is included automatically with HardwareSerial.h in your main sketch. Updated Stream Class The Stream class has been updated. This is part of the serial object and provides the search, find, and parse value functions that the HardwareSerial object uses. Constructor The constructor simply sets the timeout for the serial port to a default of 1000 ms. Stream() {_timeout=1000;} Member Functions The member functions are shown in Table 1-1. 5 Chapter 1 ■ arduino 1.0.4 Core Changes Table 1-1. Stream Member Functions Function Description void setTimeout(unsigned long timeout); Sets the timeout value for stream functions. If the process takes too long, it returns. The default is configured for 1000 ms, which is 1 second. The constructor sets this value. bool find(char *target); Searches the stream for the target string. Returns true if found, otherwise false. Also, will return as false if a timeout occurs. bool find(char *target, size_t length); Reads the stream until a target string of a specific length is found. bool findUntil(char *target, char Works according to the same logic as find(), but returns true when *terminator); a terminator string is found. bool findUntil(char *target, size_t Within a particular buffer and length, returns true if a termination targetLen, char *terminate, size_t string is found or the length reached. termLen); long parseInt(); Searches for the first valid (long) integer from the current position. Initial characters that are not digits (0 through 9) or the minus sign are skipped; once a non-digit is found, the value is returned. float parseFloat(); Searches for the first valid float from the current position, ignoring characters that are not digits or the minus sign. Once a non-digit is found that is not a period (.), the value is returned. size_t readBytes(char *buffer, size_t Reads characters from the stream into the buffer. If a length or length); timeout is reached, the function returns either 0 (for no data found) or the number of characters in the buffer. size_t readBytesUntil(char terminator, Reads characters from the stream into the buffer. If a terminator char *buffer, size_t length); character, length, or timeout is reached, the function returns 0 (for no data found) or the number of characters in the buffer. long parseInt(char skipChar); Allows for the parsing of integers and for a character (e.g., a comma) to be skipped. float parseFloat(char skipChar); Works similarly to parseFloat(), but ignores the skip character. Print The Print class has been updated. This affects the Client and Stream classes directly. The classes that include them are affected as well. The HardwareSerial and UDP classes use Stream. Therefore, you do not specifically have to include Print in your main Arduino sketch. Table 1-2 shows some of the more important updates to the public methods. 6

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.