Appendix H. Migrating to Arduino 1.0

Although it should not be difficult to get sketches written for previous Arduino versions working with Arduino 1.0, that release has important changes you need to be aware of. The first thing you will notice when launching the software is the look of the IDE. Some icons are different from previous versions of the software and there are changes and additions in the menus. The error messages when dealing with selecting boards have been improved and the new ADK, Ethernet, and Leonardo boards have been added.

More significant are changes in the underlying core software and libraries. The stated purpose of 1.0 is to introduce disruptive changes that will smooth the way for future enhancements but break some code written for older software. New header files mean that older contributed libraries will need updating. Methods in Ethernet and Wire have been changed and there are subtle differences in the print functionality.

New functionality has been added to Streams (the underlying class for anything that uses .print() statements), Ethernet, Wire (I2C), and low-level input/output.

Improvements have been made to the way libraries handle dependencies and to simplify the support for new boards. Because of these changes, third-party libraries will need updating, although many popular ones may already have been updated.

The file extension used for sketches has been changed from .pde to .ino to differentiate Processing files from Arduino and to remove the inconvenience of accidental opening of a file in the wrong IDE.

Sketches opened in the 1.0 IDE will be renamed from .pde to .ino when the file is saved. Once renamed, you will not be able to open them in older versions without changing the extension back. There is an option in the FilePreferences dialog to disable this behavior if you don’t want the files renamed

The following is a summary of the changes you need to make for 1.0 to compile sketches written for earlier releases. You will find examples of these in the chapters covering Serial, Wire, Ethernet, and Libraries.

Migrating Print Statements

There are a few changes in how print() (or println) is handled:

Working with byte datatypes

print(byte) now prints the integer value of the byte as ASCII characters; previous releases sent the actual character. This affects Serial, Ethernet, Wire or any other library that has a class derived from the Print class.

Change:

Serial.print(byteVal)

to:

Serial.write(val); // send as char
The BYTE keyword

The BYTE keyword is no longer supported.

Change:

Serial.print(val, BYTE)

to:

Serial.write(val); // sends as char
Return values from write() methods

Classes derived from Print must implement a write method to write data to the device that the class supports. The signature of the write method has changed from void to size_t to return the number of characters written. If you have a class derived from Print you need to modify the write method as follows and return the number of characters written (typically 1). See the discussion on the i2cDebug library in Recipe 16.5 for an example of a 1.0 write method.

Change:

void write

to:

size_t write

Migrating Wire (I2C) Statements

You’ll need to make some changes when working with the Wire library.

Method name changes

Wire method names have been changed to make them consistent with other services based on Streams.

Change:

Wire.send()

to:

Wire.write()

Change:

Wire.receive()

to:

Wire.read()
The write method requires types for constant arguments

You now need to specify the type for literal constant arguments to write.

Change:

write(0x10)

to:

write((byte)0x10)

Migrating Ethernet Statements

Arduino 1.0 changes a number of things in the Ethernet library.

Client class

The client Ethernet classes and methods have been renamed.

Change:

client client(server, 80)

to:

EthernetClient client;

Change:

if(client.connect())

to:

if(client.connect(serverName, 80)>0)

Note

client.connect should test for values >0 to ensure that errors returned as negative values are detected.

Server class

Change:

Server server(80)

to:

EthernetServer server(80)

Change:

UDP

to:

EthernetUDP

Migrating Libraries

If your sketch includes any libraries that have not been designed for 1.0 then you will need to change the library if it uses any of the old header files that have been replaced with the new Arduino.h file.

If you include any of these libraries, change:

#include "wiring.h"
#include "WProgram.h"
#include "WConstants.h"    
#include "pins_arduino.h"

To:

#include "Arduino.h"

Note

You can use a conditional include (see Recipe 17.6) to enable libraries to also compile in earlier versions. For example, you could replace #include "WProgram.h" with the following:

#if ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

New Stream Parsing Functions

Arduino 1.0 introduced a simple parsing capability to enable finding and extracting strings and numbers from any of the objects derived from Stream, such as: Serial, Wire, and Ethernet.

These functions include:

    find(char *target);
    findUntil(char *target,char *term)
    readBytesUntil(term,buffer,length);
    parseInt(); 
    parseFloat();

See the discussion of Recipe 4.5 for an example of Stream parsing with Serial. Many recipes in Chapter 15 demonstrate the use of Stream parsing; see Recipe 15.4 and Recipe 15.7 for examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.186