Hardware Libraries - Beginning Arduino Programming (Technology in Action) (2011)

Beginning Arduino Programming (Technology in Action) (2011)

Chapter 9. Hardware Libraries

In this chapter we are going to take a bit of a breather on the lengthy code and look at some specific hardware and the appropriate libraries used to make them work. A library is a collection of functions designed to perform specific tasks that make life easier for us. Most of what we have discussed in this book so far, like the functions digitalWrite() or delay(), are a part of the standard Arduino library. These things are so integrated into the way that we write code for the Arduino that we don't even notice it.

The hardware libraries that we will discuss in this chapter are not a part of the standard, built- in functions because they would take up too much memory. Instead, each library must be added to our sketch when we want to use the extra functionality that it provides. Each library is usually written for a particular type of hardware, so in order to use each of these libraries, we need to have a lot of different hardware available. We have tried to keep our hardware selections to simple and cost-effective choices in order to make trying each of these libraries a possibility. Rather than one or two monstrous projects for the chapter, we will instead look at four smaller examples that put to use the functions described in each section. Beginning with an overview of that library, we will also have an example schematic and illustration, some sample source code, and a breakdown of the main functions in that library. Our code will be kept fairly light so that each mini-project could be used as a building block for something bigger.

This chapter is by no means an exhaustive look at every library available, as we just don't have enough paper here to do that. Instead, we will work with a few of the common libraries distributed with the Arduino software suitable for our audience. There are many more contributed libraries available to try out that you are encouraged to look into on your own. It is also possible to write custom libraries for specific hardware, but that is a bit more of an advanced topic involving principles of C++—beyond the scope of this book. Let's begin with an overview of how to use libraries.

What's needed for this chapter:

• Arduino Uno

• 16 x 2 character liquid crystal display HD44780 compatible

• Hobby servo (Hitec HS-322 or similar)

• Unipolar or bipolar stepper motor (approximately +5v supply)

• ULN2003 or SN754410 or similar (as appropriate for the type of stepper motor)

• Arduino Ethernet Shield (SparkFun or Adafruit SD shield or breakout also okay)

• LED and appropriate 220-ohm resistor

• TEMT6000 light sensor or other analog sensor

• Hookup wires

• Solderless breadboard

Using Libraries

To use a library in an Arduino sketch, we need to tell the compiler which library we want to use. There are two ways to do this. The first is to use a #include pre-processor directive at the beginning of our sketch to specify the library's file name, as follows:

#include <LibraryName.h>

LibraryName.h is the file name of the library that we want to use. In our last project example, we used the line #include <LiquidCrystal.h> to enable those functions that make it easier to use an LCD with our project. Pre-processor directives are instructions to the compiler that tell it to include the functions that we want to use at the time of compiling. They don't follow the normal syntax of statements and don't end with semicolons. We mention this here because every library listed later will need this line at the beginning of the sketch for it to work, but we will not break it out to its own section.

Another way to access libraries is through the Sketch menu and the option Import Library, and then select the library name from the menu. The programming environment will write the #include line to match the specific library for you. If we want to make use of a contributed library that is not on the list because it is not a part of the standard Arduino distribution, we need to properly install the library before we can access it. While this will not be necessary in any of the examples in this chapter, should you need to install a contributed library, you would need to create a folder named Libraries in the Arduino folder that contains our sketches, if there's not one already, and then copy the new library's folder into it. This library should contain at least two or three files, one ending in .h, as well as other files that could include example sketches or other documentation. Before this new library can be used, the Arduino programming environment will need to be restarted and then include or import the file as normal.

Creating an Instance

After including the library, one of the first things we will do is to either create a new instance of the library or initialize the library, depending on what the library does and how it is written. While this will be specific to each library, it is worth mentioning the basic mechanic for this now. Using our previous LCD example from the last chapter, we needed to create an instance of the LiquidCrystal library to begin working with it. This is done in the following line:

LiquidCrystal lcd(5, 6, 7, 8, 9, 10);

Theoretically it is possible to have multiple LCDs, servos, steppers, or other hardware in use by some libraries and so we could have multiple lines, like that just shown, with different instance names. Here we have created the instance named lcd, which is a variable name that links to the LiquidCrystal library and assigns the pin numbers to be used with it, according to the specifications of the library. Creating an instance of a library is generally only necessary when multiple instances can exist, as is the case here. If we had multiple instances of a library, for example we had multiple LCDs connected to the Arduino, then they would each need a unique name like lcd1, lcd2, and so on.

Initializing the Library

Once an instance of the library has been created, and sometimes even when we don't need to create an instance for the library, we will then usually need to initialize the library somehow. Usually this function is placed inside the setup() function, like we did with the LCD example, by using the following line:

lcd.begin(l6, 2);

In this line we use a function called begin() that is a part of the LiquidCrystal library linked to the instance that we created called lcd. The instance name and function name are separated by a period. In this case, two parameters are passed to this function, which tell the library that we have a display that is 16 characters wide and 2 characters tall.

Every library is a little different and beyond this point what can be done with them begins to significantly diverge. So let's start with our first hardware library that includes a more thorough discussion of the LiquidCrystal library.

LiquidCrystal library

Earlier in this book we looked at one way to see the information coming out of the Arduino using both the Serial Monitor and code that uses the Serial library, which we will revisit in the next chapter. We can also use any of a number of different displays to show visual information and even create a simple user interface to provide visual feedback. These displays could include the simple monochrome character LCD that we used in the last chapter, a color graphical LCD, seven-segment and alphanumeric LEDs, and even text overlays for composite video or e-ink displays. Since we can't cover all of these here, we are going to go back to our character LCD that we used in the last project and look at some of the other things that the library will allow us to do with code examples, which will include displaying text, working with symbols and custom characters, and even creating very basic animations.

Obviously, to use the LiquidCrystal library we need an LCD. The schematic and illustration in Figures 9-1 and 9-2 show a simpler circuit than the one from last chapter that will be used for the next few example sketches. LCDs like these use the HD44780 driver and have a parallel interface with up to eight data bus pins, but we are only going to use four marked DB4-7. In addition, we need to connect the RS or register select pin to tell the display whether to display data or perform a command and the EN or enable pin to let the display know that data is waiting for it. The RW or read/write pin is simply tied to ground to keep the display in a read state and the V0 pin controls the contrast of the display and can be connected to the middle pin of a trimpot or to a resistor. To keep things simple, we have connected the positive or anode side of the backlight LED marked LED+ to a 220 ohm resistor that is then connected to +5v.

GND

Figure 9-1. LiquidCrystal schematic

PINS 5, 6, 7, 8, 9, 10

□□□□□□□□□□□a

R1 2.2K R2 220 16x2 LCD □ □

ARDUINO

□□□□□ □□□□□ □□□□□ □□□□□ □□□□□

+5VDC GND

Figure 9-2. LiquidCrystal illustration

Example Code: Arduino Haiku

With the circuit wired up, we can get into our first source code example found in Listing 9-1 to demonstrate the basic functions of the LiquidCrystal library. For this sketch, we will display some text in the form of a haiku written by John Maeda. In short, this sketch will display four screens of text separated by a 5-second delay. After the two lines of text have been printed to the display and it has paused long enough for us to read it, the LCD is cleared removing any text from the display and then it starts over at the top left of the screen.

Listing 9-1. Arduino Haiku Source Code

#include <LiquidCrystal.h>

LiquidCrystal lcd(5, 6, 7, 8, 9, 10);

void setup() { lcd.begin(16, 2);

}

void loop() {

lcd.print("All I want"); lcd.setCursor(0,1); lcd.print("to be,"); delay(5000);

lcd.clear();

lcd.print("is someone that"); lcd.setCursor(0,1); lcd.print("makes new things"); delay(5000);

lcd.clear();

lcd.print("And thinks"); lcd.setCursor(0,1); lcd.print("about them."); delay(5000);

lcd.clear(); lcd.setCursor(0,1); lcd.print("- John Maeda"); delay(5000);

lcd.clear();

delay(5000);

}

To understand how these functions work, we will need to examine them more closely after we get the code uploaded, beginning with the LiquidCrystal() function.

LiquidCrystal()

The LiquidCrystal() junction creates a new instance for the library (so we can use the library in our sketches), gives it a variable name, and assigns the pin numbers for the LCD. This function must be used the first time we want to use the library. There are several different formats for the functions syntax, but we will stick with the following simple one:

LiquidCrystal name(rs, en, db4, db5, db6, db7)

In this function, name can be any unique name that we want following the normal rules for variable names, but to make things easier we will stick with the convention of using lcd in our code. To make wiring easier and to leave certain I/O pins open on the Arduino board, we will skirt tradition here and assign the digital pins 5, 6, 7, 8, 9, 10 as the pin numbers for the rs, en, db4, db5, db6, db7 pins respectively. This comes together in our source code as the following line:

LiquidCrystal lcd(5, 6, 7, 8, 9, 10);

Now that we have created an instance of the LiquidCrystal library, we will use the variable name lcd in front of each of the functions that are a part of that library, as you will see in the next function.

begin()

With the library instance created, we need to start the library and define what size LCD we intend to use. To do that, we use the function begin() inside the setup() function.

begin(cols, rows)

For a 16-character wide by 2-character tall LCD like the one we have been using, we would use 16, 2 for the function parameters. For larger or smaller sized displays we would adjust these numbers to suit, as in 20, 4 or 8, 1. The function as it appeared in our example follows:

lcd.begin(16, 2);

This statement links the begin() function to our instance lcd and specifies a 16 x 2 display. Now that we've got the library set up correctly, let's look at some of the functions that we can use with this library.

■ Note While LCDs of other sizes will work for these examples, the code may act a little odd or need modification.

print()

To actually send information to our LCD, we can use the print() function for basic character strings in one of the following two syntax formats:

print(data) print(data, BASE)

Data can include any text strings, bracketed by double quotes (“ and ”), and values or variables that include char, byte, int, long, or string data types. Any numerical value sent to the print() function will be converted to a text string. For integer values, a base can optionally be specified as either BIN for binary or base 2, DEC for decimal or base 10, HEX for hexadecimal or base 16, or OCT for octal or base 8. For a simple character string like in our example sketch, we used the following statement:

lcd.print("All I want");

This statement prints the text All I want beginning with the current cursor position and proceeding one character at a time to the right (by default) of that position. To send an integer value, we might write a line like the following:

lcd.print(analogRead(A0), DEC);

By combining the analogRead() function, this statement will read the value of the analog in pin A0 and print that value to the LCD in decimal format. Because the print() function will keep on spitting out characters to the right of the current position each time the function is called, we need to use some additional functions to control the position of the cursor.

clear()

The clear() function will clear all of the contents of the display and position the cursor in the top left of the display. There's not much to the functions syntax because there are no values to pass to it, so let's look at the following example of the function as used in our code:

lcd.clear();

Like I said, not much to it. Because of the way LCDs display data, this function can be useful for clearing the screen in order to write new data to it without overwriting data or sending data off the edge of the LCD.

setCursor()

When we want to move the position of the cursor on the display without clearing the display, we can use the setCursor() function to position the cursor anywhere on the display. Its syntax looks like the following:

setCursor(col, row)

All we need to do with this function is provide a number between 0 and 15 for the horizontal column position and either 0 or 1 for our two-row display. A position of 0,0 for example would be the top left corner of the display and 15,1 would be the last character on the bottom-right corner. The following is a line from our example code:

lcd.setCursor(0,1);

This line is used to set the cursor to the beginning or left-hand side of the second row of the display before writing the second line of text. In our haiku sketch, we used the clear() function to not only clear the display, but to also return the cursor to the top left of the display so setCursor(0,0) was not needed.

Example Code: Symbols and Characters!)

So far, we have only displayed simple characters on the LCD by using what is easily entered from our keyboard. To make more interesting symbols on the display, the HD44780 driver has a number of special symbols, like Ф, °, O, or n, which can be accessed using a code that tells the display what symbol to show. These character displays also have the capability of displaying up to eight custom characters of our own design. The source code in Listing 9-2 provides an example of using both the built-in symbols as well as some new ones that we will create ourselves.

Listing 9-2. Symbols and Characters Source Code

#include <LiquidCrystal.h>

LiquidCrystal lcd(5, 6, 7, 8, 9, 10);

byte degree = B11011111; byte cents = B11101100; byte sqRoot = B11101000; byte divide = B11111101; byte pi = B11110111; byte omega = B11110100; byte rgtArrow = B01111110; byte lftArrow = B01111111;

byte symbols[] = {degree, cents, sqRoot, divide, pi, omega, rgtArrow, lftArrow};

byte smiley[] = {B00000,B00000,B01010,B00000,B10001,B01110,B00000,B00000}; byte skull[] = {B00000,B01110,B10101,B11011,B01110,B01110,B00000,B00000}; byte bell[] = {B00000,B00100,B01110,B01110,B01110,B11111,B00100,B00000}; byte note[] = {B00000,B00100,B00110,B00101,B00101,B01100,B01100,B00000}; byte heart[] = {B00000,B00000,B01010,B11111,B11111,B01110,B00100,B00000}; byte fish[] = {B10000,B00000,B01000,B10000,B01101,B11110,B01101,B00000}; byte lock[] = {B00000,B01110,B10001,B10001,B11111,B11011,B11011,B11111}; byte unlock[] = {B00000,B01110,B10001,B10000,B11111,B11011,B11011,B11111};

byte* characters[] = {smiley, skull, bell, note, heart, fish, lock, unlock};

void setup() { lcd.begin(16, 2);

for (int i=0; i<8; i++) { lcd.setCursor((i*2),0); lcd.write(symbols[i]);

lcd.createChar(i, ((byte*)characters[i]));

lcd.setCursor((i*2),1);

lcd.write(i);

}

}

void loop() {}

This example is maybe not the most dynamic sketch we've ever written, but it gives us a sense of the types of characters that can be displayed on our display. Once we have the sketch uploaded, we can then look at the functions that we use to both display these symbols and characters as well as those that create the new characters, in order to better explain the example source code.

writeO

Where the print() function prints strings of text to the LCD, the write() function is used to send a character to the display. The syntax for both functions is similar:

write(data)

The character to be sent to the display is specified in a numerical data format or through a variable name. From our example code, it is as follows:

lcd.write(symbols[i]);

This line displays whatever symbol is named at the current index in the symbols[] array determined by the loop counter. The symbols[] array is just a way to run down a list of each of the eight symbols defined in the earlier variable declarations, using a for loop to print them to the display. Our example code continues to print each of the eight pre-defined symbols using the setCursor() function to space each one out on the first line.

Declaring the address for the symbol at the beginning of our code and calling that variable name later is only one way to use the write() function. We can actually specify the symbol to be displayed in many different ways. For example, the symbol for cents, Ф, can be written multiple ways, as shown in the following sample fragment:

byte cents = B11101100;

lcd.write(cents);

lcd.write((B11101100));

lcd.write(236);

lcd.write(0xEC);

This block of code will display four Ф symbols in a row on the display because each of these different values refers to the same thing. Just like our example code earlier, the first line declares a byte data type variable named cents and assigns it the 8-bit binary value B11101100. The variable cents is then called in the second line to print the first symbol. The second symbol is printed by specifying the binary number directly in the write() function. Because the value B11101100 is not a real number so to speak, the B is used to tell the compiler that the following number is binary not decimal, we need to place this value in a second pair of parentheses to force the compiler to evaluate this value as a binary number. The third line uses the decimal value 236 and the fourth the hexadecimal value 0xEC for the third and fourth symbols.

To find the numerical value that corresponds to the symbol that we want to use, we need to have a look at the LCD's data sheet, a document that gives us the technical specification of the device. Figure 93 is a small part of the symbol set that is available to a particular LCD found in a data sheet for the driver chip, like the one hosted by SparkFun at www.sparkfun.com/datasheets/LCD/HD44780.pdf. These may vary depending on the chip used by the LCD, so refer to the specific data sheet for your display.

Low^^Bits

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

mi

xxxxOOOO

CG

RAM

(1)

ill

H

"l

ill

Г"і

Г"

l“-

1"

П

У

■■■

г'і

пв

1

xxxxOOOl

(2)

1

J

i

H

■■■■

l֊:.J

3

ч

П

r

«■

Lj

.=!

1՜1

ххххООЮ

(3)

■և.

P".

Ւ-:

Լ-՛

Г".

Ւր" 1 s

l"i 1—"

l"B

1

PB

.-1

1

si

■■1

il>

1—"

1

xxxxOOl 1

(4)

11

■T

u\

w

1 Г'і 1

j“

;=

1

•Л

-L. 1 1

T

bb

+:

B

Л1

XXXX0100

(5)

■X՛

■t

.■1

1

1 Կ

L."

1

І

i

■Ւ

V

-L

L i"

+1

i'

1

--I

■■■в

J к

xxxxOI 01

(6)

■v

■■ H

■_l

L-

L_

l,J

1“

1 1 ■--1

в

"T

1

-H

У

"1

_L

|T"

1 1 l_J

xxxxOI10

(7)

■■

է.

P“

ի-

и

ի

I...I

“i

.1.1

֊]

B"l

և/

1

րա

շ_

xxxxOI11

(8)

4

l“l

1

l-i

u

III

w

■Ij

III

\\B

■■

1

"7 ■■ ■

iB

Г'І

■■■I

ТГ .■ ւ

Figure 9-3. Partial symbol codes from data sheet

To use a symbol like the Greek omega, Q, for example, we need to cross reference the first four bits, or 1s and 0s, at the top of the chart with the second four on the left. That will give us the binary number 11110100 that we can then use with the write() function to display the symbol on the LCD. Likewise, pi, n, would be the symbol at 11110111. We can then convert this value to another base if we particularly wanted to and use that as the parameter for the write() function as we mentioned earlier.

createCharO

While the LCD has many different symbols built into its driver chip, we might want to create a symbol like a smiley face, music note, or even a fish. Our character display can store eight custom characters numbered 0–7, each occupying a 5 x 8 grid of pixels. Creating a custom character to display on the LCD is a two-step process. First, we need to define a bitmap array that corresponds to each dot for the character and, second, we need to use the createChar() function to send our bitmap to an address in the displays memory. The syntax for this function is as follows:

createChar(number, data)

The number parameter is the number of the custom character with a range of 0–7. The data is the array name that stores the bitmap image. Before fully explaining our example code, let's look at the following hypothetical statement to make a smiley face:

lcd.createChar(0, smiley);

This line will create a new character at the number 0 according to the pattern in the array smiley[]. One way of creating this array is as follows:

byte smiley[] = {B00000,B00000,B01010,B00000,B10001,B01110,B00000,B00000};

Because each character is a collection of dots in a 5 x 8 grid, we can create an array of bytes, one byte for each row and one bit for each column. A 1 represents a pixel that is on and 0 a pixel that is off. Our example is written to save space, but it is common to write these arrays in the following format:

byte smiley[] = {

B00000,

B00000,

B01010,

B00000,

B10001,

B01110,

B00000,

B00000

};

While this way takes up more space on the screen, if you squint just right you can make out the smiley face in the code. With the array defined and the custChar() function called to assign our character to a position, we then use the write() function to display the character on the screen, like so:

lcd.write(0);

One useful utility for creating custom characters is the Character Creator shown in Figure 9-4 and found online at http://icontexto.com/charactercreator/.

□ □□□□ □ □□□□

byte neuChar[8] - {

B10000,

BOOOOO,

B01000,

B10000,

801101,

B11110,

B01101,

BOOOOO

led.createChar(0, newChar led.begin(16, 2); lcd.write(O);

Arduino.cc I LiquidCrystal Libray

Created by Bruno Maia, IconTexto.cc

^ ^ ^ / ©Arduino - LiquidCrystal - C ;:\y.

<- G 1 © icontexto.com/charactercreator/

Arduino LiquidCrystal

Character Creator

#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Figure 9-4. Arduino LiquidCrystal Character Creator

Simply toggle each block to draw your character and then copy and paste the generated code in the right panel into your Arduino sketch. And that's all there is to creating custom characters. The library fortunately takes on all the hard work of writing this information to the LCD's memory so that we can display this character, or even make a new character, whenever we want.

So now going back to our earlier example code that displays eight custom characters on the second row of our display, we first defined eight character arrays that included characters called smiley, skull,

bell, note, heart, fish, lock, and unlock. We then made an array called characters[] to create an index of each of these individual character names. Because this is an array of arrays, similar to our discussion of character arrays in the last chapter, we need to use the “*” symbol after the data type to indicate that this is an array of pointers—basically references to places in memory where the arrays are stored.

In our for loop, we created each character in turn using the following line:

lcd.createChar(i, ((byte*)characters[i]));

Here, our character number is determined by the for loop counter and the name of the array is determined by the index of the characters[] array. To get this array to point to the correct character array, we need to remind the compiler that characters[i] is a pointer by adding (byte*) in front of the array name bracketed by parenthesis. This turns that array into a pointer that the compiler can reference, creating the appropriate character. I know I said we wouldn't talk much about pointers, but it's not that hard and plenty of reference material abounds if you want find out more.

Example Code: Fish Tank Animation

We are almost done with the LiquidCrystal library, with only one more example and a couple new functions to go. By using these new functions with some custom characters, we can create a rather low-tech 8-bit animation—maybe something like a fish swimming in a fish tank. The example in Listing 9-3 takes two strategies for creating an animation. First, it moves the character around the screen by using functions that scroll the data to the left or right. Second, it displays multiple characters at the same location with a delay in between each one to create a simple flipbook style animation.

To make this code work, we designed multiple fish characters that include different directions the fish might swim, as well as a few characters so that the fish can blow bubbles. Four functions are created to control the fish's movement: scrollLeft(), scrollRight(), stopCenter(), and blowBubbles(). By calling each function, and passing the number of steps in the case of the scrolling functions, we can get the fish to move around the LCD while keeping track of it the entire time using the variables x and y.

Listing 9-3. Fish Tank Animation Source Code

#include <LiquidCrystal.h>

LiquidCrystal lcd(5, 6, 7, 8, 9, 10);

byte fishLeft[8] = {B00000,B00000,B00000,B00000,B01101,B11110,B01101,B00000}; byte fishRight[8] = {B00000,B00000,B00000,B00000,B10110,B01111,B10110,B00000}; byte fishCenter[8] = {B00000,B00000,B00000,B00000,B00100,B01110,B00100,B00000}; byte fishBubbles1[8] = {B00010,B00000,B00100,B00010,B00100,B01110,B00100,B00000}; byte fishBubbles2[8] = {B00000,B00100,B00010,B00000,B00100,B01110,B00100,B00000}; byte fishBubbles3[8] = {B00100,B00000,B00000,B00000,B00100,B01110,B00100,B00000};

byte x = 0; byte y = 0;

int time = 600;

void setup() { lcd.begin(16,2);

lcd.createChar(0, fishBubbles1); lcd.createChar(1, fishBubbles2); lcd.createChar(2, fishBubbles3);

lcd.createChar(3, fishLeft); lcd.createChar(4, fishRight); lcd.createChar(5, fishCenter);

}

void loop() { scrollRight(9); stopCenter(); blowBubbles();

y = 1; x += 1;

scrollLeft(5);

stopCenter();

blowBubbles();

y=0;

scrollRight(lo); delay(time*l0); x = 0;

У = 0;

}

void scrollRight(int steps) { lcd.setCursor(x, y); lcd.write(4); delay(time);

for (int i=0; i<steps; i++) { lcd.scrollDisplayRight(); delay(time); x++;

}

lcd.clear();

}

void scrollLeft(int steps) { lcd.setCursor(x, y); lcd.write(3);

for (int i=0; i<steps; i++) { lcd.scrollDisplayLeft(); delay(time);

x--;

}

lcd.clear();

}

void stopCenter() { lcd.setCursor(x, y); lcd.write(5); delay(time); lcd.clear();

}

void blowBubbles() { for (int i=0; i<3; i++) { lcd.setCursor(x, y); lcd.write(i); delay(time);

}

lcd.clear();

}

This sketch uses many of the LiquidCrystal functions that we have already discussed, but it also adds a couple new ones. Let's look at these two functions before moving on to our next library.

scrollDisplayLeftO and scrollDisplayRightO

Each time these functions are called, the display will be shifted one character to the left or right, depending on which function we call. We placed these functions in for loops with a delay and a counter to track the horizontal position of the cursor. Take for example the following:

for (int i=0; i<steps; i++) { lcd.scrollDisplayRight(); delay(time); x++;

}

This loop will call the function scrollDisplayRight() once each time through the loop for the total number of steps passed to the function. The loop will also delay for the specified amount of time and increment the horizontal position by one.

That might seem like a pretty exhaustive look at the LiquidCrystal library, but believe it or not there are even more functions to do all sorts of things that we haven't even mentioned here. However, rather than dwelling too long on this one library, let's keep moving to some of the other interesting libraries that we need to talk about.

Servo Library

Moving things around on a screen is pretty cool, but let's now look at how we can physically move material things around. The best way to move something with the Arduino is to use a motor. We already looked at a simple motor with the DC fan in Project 4, which is functionally equivalent to controlling any simple DC motor. Two other common types of motors include the hobby servo and the stepper motor. Both of these motors can be difficult to work with without the awesome libraries that are a part of the Arduino platform. The first motor that we'll discuss, the hobby servo, typically moves in 180° arcs. While initially used in radio-controlled hobby applications, the servo has quite the following in robotics and animatronics because of its integrated electronics, gearing, and positional feedback, which allow for fairly precise control.

Typically, to control the servo, a signal of varying pulse widths is sent to the servo over a regular time period and the servo moves to the position that corresponds to that pulse. As long as the signal is refreshed about every 20 milliseconds, the servo will maintain its position. On our Hitec servo, a digital pulse with a duration of 900 microseconds will send the servo arm to a position of 0°, while on the other end, a pulse of 2100 microseconds will send the servo 180° in the other direction. The drawing in Figure 9-5 illustrates the typical movement arc of our Hitec servo. Every brand of servo will be different, with typical ranges of movement exceeding 180°, and some servos even spin in the opposite direction, so

always double-check the specifications for your motor. It is also important to remember that while we are in essence using a form of PWM, we do not want to try to PWM the servo signal line using analogWrite() or we could damage our servo motor.

90°1500us

0° 900us

Figure 9-5. Hobby servo rotation

\

\

I

= 180°2100us

The Servo library can control up to a total of 12 servos on the Arduino Uno on any of the digital pins, although this functionality disables PWM on digital pins 9 and 10. Servos have three wires: signal, power, and ground. The yellow, white, or sometimes orange wire is for the logic signal and should be connected to one of the Arduino digital pins. The red wire is power and is connected to the +5v pin on the Arduino board. The black or brown wire is for ground and should be connected to the ground pin on the interface board. While we can control quite a few servos, they can consume current in excess of 100s of milliamps, so Arduino's power supply can't handle more than a few servos at a time. The supply voltage for servos should be in the +4.8v to +6v range, while the logic signal should remain within a +3–5v range. If using an external power supply, it is important that the ground wires share a common connection with the Arduino board. Figures 9-6 and 9-7 show how easy it is to connect a single servo to the Arduino board and we will then continue the discussion with some example code.

SERVO

PIN 10 Г

SIGNAL

+5 VDC —

+5 VDC

GND

GND

Figure 9-6. Servo schematic

Figure 9-7. Servo illustration

Example Code: Reminder Bell

For our example codein Listing 9-4, we will use the servo to ring a bell. So, why a reminder bell? Honestly, it seems to me like ringing a physical bell is something that a servo would be good at. You could attach a bell with wire to the servo or maybe instead attach an arm so that when the servo moves, it will tap a bell. We might use this as the basis of a physical hit-counter or to notify us of an online-friend status or put it to use for some other nefarious means. We'll leave that up to your imagination.

Listing 9-4. Reminder Bell Source Code

#include <Servo.h>

Servo servo;

unsigned long startTime = 0; unsigned long interval = 600000;

void setup() { servo.attach(10); servo.write(90);

}

void loop() {

if (startTime + interval < millis()) { servo.write(96); delay(90); servo.write(90); startTime = millis();

}

}

In this example code, we start with a position of 90° and jump briefly to 96° and back again each time the interval has been exceeded. Couple this movement with an event, say a timer that reminds us to get up and stretch every 10 minutes, and we have the beginnings of a really simple project. This could be adjusted to suit many applications, so rather than dwell too long on the hardware, let's look at the Servo functions.

Servo

Creating a new instance of the Servo library is quite a bit easier than with the previous library. All we need to do is use the Servo library name at the beginning of our sketch, followed by a name for our instance. In our example code this was done in the following line:

Servo servo;

And that's it for this line. Pick a name that might mean something to you whether its servol, rightSideServo, or george—so long as the name is unique and each instance of the Servo library has been declared.

attachO

To use our servo, we need to bring out the attach() function to tell the library which pin we are using for this instance of the library. The syntax has the following two options:

name.attach(pin) name.attach(pin, min, max)

In our example code this line took the form of

servo.attach(10);

Here we have set up servo to work with pin 10. Pins 9 and 10 seem like obvious choices because the servo function disables PWM on those pins anyway, although you are not limited to these pins.

Not all servos are created equal, however, and many will have minimum and maximum rotation angles that might be at odds with the Arduino defaults. Check the data sheet for your servo to see what these might be. If it's necessary, we can use the attach() function to define the minimum and maximum rotation angle specified in microseconds. The default minimum value is set to 544 for 0° and the maximum is set to 2400 for 180°. This will provide us with the greatest potential range of movement, but at the risk of possibly damaging our servo. If we were concerned about matching the specifications of our Hitec servo, we could specify the minimum and maximum as follows:

servo.attach(10, 900, 2100);

writeO

Now with our servo set up, we need to do something with it. To make it move, we can use the write() function with the following syntax:

name.write(angle)

Simply specify an angle in degrees and the servo will move to that angle at the only speed it knows. In our sketch we wanted to start off at 90° and when the timer was triggered to move to 96°.

servo.write(90);

Because the servo only moves at one speed, when we need to slow things down we need to break large jumps in position down into smaller movements with delays between each movement. This might look something like the following:

for (int i=45; i<136; i++) { servo.write(i); delay(io);

}

This is a fairly standard sweep using a for loop that starts the servo at 45° and increments by 1°

every 10 milliseconds until it has reached 135°. In order to return to the starting position at the same

speed, we would need to reverse the loop. Take the code in Listing 9-5 for example.

Listing 9-5. Double Servo Sweep Source Code

#include <Servo.h>

Servo leftServo;

Servo rightServo;

void setup() {

leftServo.attach(10);

rightServo.attach(9);

}

void loop() {

for (int angle=0; angle<180; angle+=2) { leftServo.write(angle); rightServo.write(180-angle); delay(20);

}

for (int angle=180; angle>0; angle-=2) { leftServo.write(angle); rightServo.write(180-angle); delay(20);

}

}

In this sketch, two servos are used to sweep in opposite directions, maybe for a pan and tilt mechanism for a camera or sensor, or a pair of wheels for a robot. To do this, two instances of the Servo library are created named leftServo and rightServo attached to pins 9 and 10. Beginning at 0° and continuing through to 180° at 2° increments, the first loop writes this angle to the first servo while the opposite angle, by subtracting the current angle from 180, is written to the second servo. The second for loop reverses the direction at the same speed. A delay of 20 milliseconds is added in each loop to slow the servos down. To slow them down even more, we could reduce the increment or decrement to 1 and increase the delay time. Alternatively, we could speed things up by increasing the increment and decrement counter and decrease the delay.

While there are additional functions that can be used to read the location of the servo, write a position with microseconds instead of angles, and even detach the servo, but these are all somewhat situational. Let's move on to another kind of motor that is also known for its positional accuracy, but can spin 360 degrees.

Stepper Library

The servo motor is an easy motor to hook up and does what it does with ease, but what if we want that same kind of positional accuracy in a motor that can spin the full 360? For that, we need to use a stepper motor, which is a type of motor that rather than spinning continuously, rotates in a specific number of degrees or steps. The amount of rotation per step is dependent on the individual stepper; ours has a rotation of 1.8° per step while others can range from as little as 0.9° per step to as much as 30° per step or more.

To control a stepper motor, the internal coils of the motor need to be individually energized in a particular sequence, dependent on the type of stepper motor, either unipolar or bipolar. Unipolar steppers often have five or six wires with a common ground connection and the current flows through each coil in only one direction. Bipolar steppers, on the other hand, only have four wires with no common connection and the current flows through each coil in both directions.

While there are a multitude of drivers available, we are going to use one of two simple chips that only cost a couple dollars each and require no external components to get going. To control a unipolar stepper motor, we will use the ULN2003 Darlington transistor array. A Darlington array is a series of transistor pairs, where a small transistor drives a second transistor to increase its capacity, all on a single integrated circuit or IC. Four individual transistors would have the same effect, although not as convenient. The ULN2003 actually has seven transistor pairs on the chip, although we will only use four of them. By turning each transistor on in order, we can power each coil of the motor causing the motor to spin.

For bipolar stepper motors, we will use the SN754410 dual H-bridge. An H-bridge is a special arrangement of transistors on a chip that is capable of reversing the direction of the current between two pins. When used on standard DC motors, this would change the direction that the motor spins. In the case of a bipolar stepper, alternating the direction of current flow on each coil will create a step causing the motor to turn in a direction determined by the sequence.

Because every stepper is a little different, we will provide two schematics and illustrations for the two types of motor: Figures 9-8 and 9-9 for unipolar and 9-10 and 9-11 for bipolar. Pick the circuit that matches your motor type, being careful to wire the stepper motor coils as shown in the schematic. We have also used a motor that can safely operate from the Arduino board's +5v output. If you are using a motor with a different voltage or higher current requirement, then you should connect an external power supply to the +V Motor pins instead of +5v. For more information on determining the electrical characteristics of stepper motors, you might want to check out the following links:

www.makingthings.com/documentation/how-to/stepper-motor

www.cs.uiowa.edu/~jones/step/types.html

www.jasonbabcock.com/computing/breadboard/unipolar/index.html

www.jasonbabcock.com/computing/breadboard/bipolar/index.html

PIN 8

Z^֊

PIN 9 1

z^-

PIN 10

z^֊

PIN 11 1

z^֊

1 IN

OUT 1

2 IN

OUT 2

3 IN

OUT 3

4 IN

OUT 4

NC

NC

NC

NC

NC

NC

GND

+V MOTOR

ULN2003

STEPPER

+5 VDC

+5 VDC

GND

Figure 9-8. Unipolar Stepper schematic

GROUND

PINS 8, 9, 10, 11

□ □ □ □ □ □ □ □ □ □

□ □ □ □ □ □ □ □ □ □

□ □ □ □ □ О □ □ □ □

+5VDC (OR AS NEEDED FOR MOTOR)

Figure 9-9. Unipolar Stepper illustration

+5 VDC PIN 8

PIN 9 +5 VDC

1,2 EN

+5 VDC

1 IN

4 IN

1 OUT

4 OUT

GND

GND

GND

GND

2 OUT

3 OUT

2 IN

3 IN

+V MOTOR 3,4 EN

+5 VDC PIN 11

PIN 10 +5 VDC

SN754410

STEPPER

GND

GND

Figure 9-10. Bipolar Stepper schematic

PINS 8, 9, 10, 11 STEPPER

Figure 9-11. Bipolar Stepper illustration

Example Code: 60-Second Sweep

Have you ever wanted a clock that moves backwards? For our example source code found in Listing 9-6, we are going to create a pseudo time-keeping device that is eternally trapped in a 60-second loop. The stepper will move in a full 360° arc in about a minute. At the end of its arc, it will switch direction and spin the other way. Because our stepper moves in 1.8° steps, it's not entirely possible to have the most exacting of timepieces here, but it's close enough. Because of this, each second the stepper should move 3.33 steps, which it can't do. So we fudge the delay a little bit with a delay between steps of 909 milliseconds instead of 1 second. Because of how the two different circuits shown in the figures are designed, the single sketch provided will control either type of motor.

Listing 9-6. 60-Second Sweep Source Code

#include <Stepper.h>

const int steps = 200;

Stepper stepper(steps, 8, 9, 10, 11);

int stepDirection = 3; int counter = 0;

void setup() {

stepper.setSpeed(30);

}

void loop() {

stepper.step(stepDirection);

delay(909);

counter+=3;

if (counter > steps) { counter = 0;

if (stepDirection == 3) stepDirection = -3; else stepDirection = 3;

}

}

With our simple code in place rotating our stepper motor back and forth, let's explore the Stepper functions in greater depth.

Stepper

To create a new instance of the Stepper library, we use one of the following two syntaxes:

Stepper name(steps, pin 1, pin 2)

Stepper name(steps, pin1, pin 2, pin 3, pin4)

The syntax we use depends on whether we are using two pins or four pins to control the stepper drivers. To make our wiring a little easier and the code work on both drivers, we chose to use four pins— easier to wire at the cost of a pair of digital pins. In our example code, we created the following Stepper instance:

Stepper stepper(steps, 8, 9, 10, 11);

Our instance is named stepper although we could use almost anything for the name. The first parameter is the number of steps that our stepper motor is capable of in one full rotation. For our stepper, it has 1.8° steps, meaning that it would take 200 steps to complete a single rotation. A 7.2° step stepper motor would likewise have 360° / 7.2° or 50 total steps per revolution. In our earlier code, we declared the number of steps for our motor as the constant variable steps that we will use later in our code. The next four parameters, pins1-4, are the pin numbers connected to the drivers. Refer to the schematic for which pins go where.

setSpeed()

With our instance created, we need to set the rotational speed for our motor. The syntax for this function only has one parameter, as follows:

name.setSpeed(rpm)

The speed for our motor is set as rotations per minute. This value determines how fast the stepper moves from step to step and will naturally vary for each stepper motor. Setting this value too high for your motor may cause skipped steps or intermittent operation. Choose a speed appropriate for your application and/or motor.

Remember that the setSpeed() function does not actually move the motor—it only establishes the speed of the motor when our next function is called.

step()

The step() function is what actually makes the motor move. The syntax is simple, but there are some things we need to keep in mind.

name.step(steps)

The one parameter to pass to this function is the number of steps that we want to move the stepper motor. For example, with a 1.8° step motor, one step equals a 1.8° movement. Setting the number of steps to 50 will turn the motor 90° clockwise on our 200-step motor, or a quarter of a revolution, where -50 will turn the motor 90° counterclockwise. As you see, a positive integer will create a movement in one direction while a negative value sends it in the reverse direction.

Unlike something similar to the analogWrite() function, which you set and forget, step() will prevent the Arduino from doing anything else until it has completed its full move. This is not that big of a deal if we step the motor in small increments each time we call the step() function, but it quickly adds up. As a practical example, if we set the RPM speed to 10 RPM, our 200-step motor would take 6 seconds to complete 200 steps. In other words, if we used the statement stepper.step(200) then that would be 6 seconds that we will not be able to read inputs, communicate with other devices, or anything else. Instead it might be better to complete large rotations with a loop statement like the following code fragment:

for (int i=0; i<200; i++) { stepper.step(1);

if (digitalRead(2) == HIGH) break;

}

In this example, we are incrementing the stepper motor 200 times in single-step increments to complete one revolution. Instead of calling a single 200-step movement by using a for loop, each individual step will only take 30 milliseconds and in between each step, we can, for example, check the status of a digital input pin and exit the loop using the break statement if a condition has been met.

With all of the Stepper library functions out of the way, let's look at one more sketch in Listing 9-7 before we move on to our next and final library for this chapter. This sketch demonstrates one way to map the values from an analog sensor to the movement of the stepper motor. A potentiometer with a giant knob on it is a good choice, or maybe better yet an accelerometer that controls the movement of the stepper through the tilt of its axes, although any analog input would work as well. Using the map() function, we can take an analog reading and map it to the total number of steps to indicate the level of the analog input—a full reading of 1024 will turn the stepper one full revolution.

Listing 9-7. Analog Sensor to Stepper Source Code

#include <Stepper.h>

const int steps = 200;

Stepper stepper(steps, 8, 9, 10, 11); int previous, val;

void setup() {

stepper.setSpeed(30);

}

void loop() {

val = map(analogRead(0), 0, 1024, 0, steps); stepper.step(val - previous); previous = val; delay(20);

}

Now that we have used libraries to display text on an LCD as well as making things move with two different kinds of motors, let's take a look at how we can use libraries to store and retrieve information.

SD Library

In the last chapter, we very briefly introduced the idea of using the program memory of the Arduino microcontroller to keep large chunks of data from taking up too much room in RAM. Instead, we could use additional external hardware to read and write to an SD memory card. SD or Secure Digital flash memory cards are the very same wafer-thin memory cards used by cameras, mp3 players, and other devices. The SD library is a relative newcomer to the standard Arduino libraries and the list of functions and associated libraries would make our conversations of the LiquidCrystal library and the PROGMEM functions seem like a leisurely stroll in the park in comparison. Rather than a comprehensive analysis of the library in the few remaining pages of this chapter, we are going to take a quick look at reading and writing values to the card using as few functions as we can get away with.

To get access to an SD card, we need some hardware in the form of a breakout board or shield that will connect the card to the appropriate pins on the Arduino board. Adafruit Industries makes a nice compact microSD card breakout that can be wired up to the Arduino with only a handful of wires, while SparkFun Electronics has an inexpensive shield available that plugs directly into the Arduino board. Instead, we are going to use and recommend the Arduino Ethernet Shield, which in addition to having the hardware necessary for accessing a microSD card, also has Ethernet hardware built-in for future projects that open up the possibility for exploring Arduino Internet connectivity. The Arduino Ethernet Shield is convenient to use in that it plugs right into the pin headers on the Arduino board while still giving us access to all of the I/O pins not used by the devices on the shield.

In addition to the Ethernet Shield, we need to have a properly formatted microSD card as well. To read up not only on the procedure for formatting the SD card but also some of the differences in the hardware, check out the SD Card Notes page on the Arduino web site at

http://arduino.cc/en/Reference/SDCardNotes. Once we have the hardware in place and a properly formatted microSD card, it's time to do something with it.

Because just storing data is not that interesting in and of itself, we will use the circuit shown in Figures 9-12 and 9-13 to create a very simple data logger to record an analog sensor input to a text file on the SD card called data.txt. We should start by mentioning a couple of things about the SD card on the Ethernet Shield. The Ethernet device and the SD card are both devices that use the Serial Peripheral Interface or SPI. We won't be getting into much detail with this protocol in this book, but fortunately for us, the SD library hides much of this interface from us. The Ethernet Shield connects digital pins 11, 12, and 13 to each SPI device with an additional pin needing to be tied to a hardware select pin to enable each device. The Ethernet device is connected to pin 10, the default for one SPI device, so the SD card has been connected to pin 4. In our code, we need to configure pin 10 as an output even though we are not using the Ethernet part of the shield for this example.

For the rest of our hardware, the sensor we will be using is the TEMT6000 breakout board from SparkFun to measure ambient light levels, although any other analog sensor would work as well. We will also use the Serial Monitor so that we can see the status of opening and writing to the file on the SD to tell us if something has gone wrong, what data is being written to the card, and when the file has been closed. Every time the Arduino is reset, it will open the file on the card and continue to update the values on the card for another round.

Once we know we can write files to the card, we will try out another sketch that reads values from the same text file on the card and outputs those values to a simple LED connected to a PWM pin— basically, a larger version of our Flicker Sketch from the last chapter. Because the SD card is connected to pins 4, 11, 12, and 13, and we can't use pin 10, we will connect our single LED to pin 9. Like the rest of the example code in this chapter, however, these sketches are not meant to be full-fledged projects and this is especially the case with the SD library. Think of these examples as proof-of-concept ripe for future exploration and expansion. So, let's get started.

TEMT6000

PIN A0 m

ZD-

OUT

GND

+5VDC

+5VDC

GND

R1 220 Հ* LED

pin 101 >-VVV-И—

GND

+ -

Figure 9-12. SD schematic

Figure 9-13. SD illustration

Example Code: SD Logger

Our first sketch for the SD library in Listing 9-8 will run entirely in the setup() function since it is not necessary to run it in a continuous loop. We start the sketch with a few settings that determine how long we want to record data, specified as runningTime in minutes, and the interval in between sequential readings, specified as interval in milliseconds. We then step through the initialization process providing feedback through the Serial Monitor at every step. Once the SD library is set up, we need to open a file to write our data to, set our clock for the running time, and then we enter a while loop that, for as long as the duration lasts, will print the analog sensor data to both the Serial Monitor and to the file on the SD card at an interval determined at the beginning of the sketch. After the time has expired, the file is closed and the sketch effectively ends. Each time the sketch is restarted, through reset or cycling power, it will continue to amend data to the existing values already in the file. To start over, the file will need to be deleted first.

Listing 9-8. SD Logger Source Code #include <SD.h>

File file;

const long runningTime = 5; // time in minutes

const long msMin = 60000; // milliseconds in 1 minute

const int interval = 2000; // time in milliseconds

const int sensorMin = 30; const int sensorMax = 1000; const int sensor = A0; const int SDcs = 4;

void setup() {

Serial.begin(9600);

Serial.print("Initializing SD card..."); pinMode(10, OUTPUT);

if (!SD.begin(SDcs)) {

Serial.println("initialization failed!"); return;

}

Serial.println("initialization done."); file = SD.open("data.txt", FILE_WRITE); if (file) {

while (millis() < (runningTime * msMin)) {

byte value = map(analogRead(sensor), sensorMin, sensorMax, 0, 255);

Serial.print("Writing ");

Serial.print(value, DEC);

Serial.print(" to data.txt... "); file.write(value);

Serial.println("done.");

delay(interval);

}

file.close();

Serial.println("File closed.");

} else {

Serial.println("Error opening data.txt.");

}

}

void loop() {}

This sketch builds on concepts elsewhere in this book, including the delay without using the delay() function and the Analog Serial Monitor, however, the SD library's functions are a little different from the other libraries that we have looked at.

File

The first line we will look at creates an instance of the file that we want to use. It has a very simple syntax, as follows:

File name

Where name is anything you want it to be. This does not have to be the actual file name on the SD card. In our example code, we used the line File file; because it seemed easiest although myFile, dataFile, or any other name that you like could be used here.

SD.beginO

With our file ready to go, we need to initialize the SD library using the SD. begin() function. It too has a basic syntax:

SD.begin(csPin)

This function will default to a CS, or chip select pin of pin 10, so in order to use this function with the Ethernet Shield or any other configuration where the CS pin on the SD card is on any other pin we need to specify that pin number. From our code it is as follows:

if (!SD.begin(sdCsPin)) {

This line contains the SD.begin() function and performs a test to see if the function was successful or not. If the SD card is successfully started, this function will return true while if there was a problem, for example the SD card is not present, it will return false. The if statement in our code will give us an error message and exit the sketch if there is any problem initializing the SD card.

■ Note Even if we are using a different CS pin from the default pin 10, we need to configure pin 10 as an output or else the SD library will not properly function.

SD.openO

To be able to write data to a file, we must open it first with the SD.open() function. The syntax for the function follows:

SD.open(filename, mode)

The filename parameter is a character string for the name of the file that we want to access. This name can also include the directory structure on the card using forward slashes, /, to separate directories. If the file does not exist, this function will create it, although the directory must already exist beforehand. This function only works with short file names from the days of yore in an 8.3 format. This means an 8-character name with a 3-character file extension separated by a period, as in datafile.txt.

There are two possible modes for this function: FILE_READ and FILE_WRITE. The default mode for the function when no mode is specified is FILE_READ. This mode will open the specified file at the beginning of the file to be read by the read() function explained later. The FILE-WRITE mode is used to write information to the file beginning at the end of the file. In other words, we read from the beginning of the file and we write to the end of a file. In our example code it is as follows:

file = SD.open("data.txt", FILE_WRITE);

We opened the file data.txt and set it to a FILE_WRITE mode so that we could write data to the file and then attributed this to the instance file that we created earlier. In our next few functions, the instance name file will be used to tell the functions what file to use.

■ Note Keep in mind that only one file can be opened at a time.

closeO

When we are done with the file that we have open, or if we need to open a new file, we must first close the current file using the close() function to save the data that has been written to the SD card. The syntax for this function follows:

name.close();

The specified name is the name of the File instance that we have been working with, as in file.close(); from our example code earlier, and has no further parameters and does not return any data. This function has been called after the data has been written to the file to save the file before moving on.

writeO

Used in the sketch to write a data value to the SD card, the write() function has the following syntax: name.write(data)

The name is the instance of the File class created at the beginning of the code and data can be a numerical value of the byte, char, or string data type. In our example we used the following code:

byte value = map(analogRead(sensor), sensorMin, sensorMax, 0, 255); file.write(value);

These two lines read a value from an analog input, map those values to a range of 0–255, and assign that value to the byte named value. This value is then written to our file.

print()

Instead of writing numerical data to the SD card, we can use the print() function to print a string of characters to our file instead. This has a similar syntax to write(), as follows:

name.print(data)

Like the print() function for the LiquidCrystal library, data can include any text strings, bracketed by double quotes (" and ”), and values or variables that include char, byte, int, long, or string data types. Any numerical value sent to the print() function will be converted to a text string, which is why it was necessary to use the write() function in our example so that we can use those values for our next example code.

Example Code: SD Flicker

Now that we have some data collected from our simple data logger stored permanently on our memory card, we can use that data to generate some form of output. The example code in Listing 9-9 will read each byte individually and write that value to the LED connected to PWM pin 9. To smooth things out some, we are using a for loop to fade from one value to the next.

Listing 9-9. SD Flicker Source Code

#include <SD.h>

File file;

const int interval = 10; // time in milliseconds

const int LED = 9; const int SDcs = 4;

byte oldValue = 0;

void setup() {

Serial.begin(9600);

Serial.print("Initializing SD card..."); pinMode(10, OUTPUT);

if (!SD.begin(SDcs)) {

Serial.println("initialization failed!"); return;

}

Serial.println("initialization done.");

}

void loop() {

Serial.println("Reading from data.txt. "); file = SD.open("data.txt"); if (file) {

while (file.available()) { byte value = file.read();

if (oldValue < value) {

for (byte i=oldValue; i<value; i++) { analogWrite(LED, i); delay(interval);

}

} else {

for (byte i=oldValue; i>value; i--) { analogWrite(LED, i); delay(interval);

}

}

oldValue = value;

}

file.close();

Serial.println("File closed.");

} else {

Serial.println("Error opening data.txt.");

}

}

This example is a more complex version of the Flicker sketch presented in Chapter 8. Instead of using an array, we are reading sensor values from our memory card. This has the advantage that we do not need to worry so much about available memory space. Let's see how we read these values with the functions in the SD library.

availableO

Because we may not know how many bytes are available in a particular file, we can use the available() function with the following syntax to find out:

name.available()

Okay, there's not much to it beyond giving it the name for our File instance, but it does return the amount of bytes that remain available in the file. When we open a file to read from it, we start at the beginning of the file, so we can use the available() function to determine when we have reached the end of the file. We used this function in the following manner:

while (file.available()) {

By placing the function in a while loop, we will continuously execute the following code so long as data is still available in the file that we are reading from.

readO

Now to get the information out of our file, we need to use the read() function. Like many of the other functions in this library, the syntax is minimal, as follows:

name.read()

When called, this function will read one byte from the file and return that value. This function will return data in whatever format it was written in. Because we used the write() function in our SD Logger example, each byte read would be a numerical value that we can use to send to the analogWrite() function later in this sketch. Considering the following line:

byte value = file.read();

Here we assign the byte obtained from the read() function to the byte variable named value. We then use this data to fade our LED according to the value stored in memory.

Summary

That pretty much wraps up our cursory glance at some of the functions available to us from the SD library for reading and writing files to SD cards and with it our discussion of a few of the standard hardware libraries available on the Arduino platform. The hardware libraries are unique in that they are generally written for very specific types of hardware, devices, or chips. Instead of presenting a comprehensive description of every function for every piece of hardware that has a library, an exhausting and expensive venture to be sure, this chapter showed some of the wide-ranging capabilities available by using libraries and some of the more universal mechanics on how libraries work. Hopefully, you've picked up a least some of the hardware presented in this chapter and have had success with the circuits and code in our examples. You might even want to try out some of the other standard Arduino libraries or some of the many community-contributed libraries, now that you have a basic understanding of how they work.

We are now going to build on our discussion of libraries by looking at some of the serial libraries used to communicate with all sorts of different types of devices. We have already used some of the Serial functions in our code throughout this book, so it's time to have a closer look and see how they work. There are also other forms of serial communication that we should look at, as well with their assorted advantages and disadvantages. These libraries are more generalized and work with a range of devices that support their protocol, so we shouldn't need as much hardware for the next chapter.