Hardware Reference
In-Depth Information
expanderShield.writeBit(1, 6);
delay(1000);
expanderShield.writeBit(1, 2);
expanderShield.writeBit(1, 5);
delay(1000);
expanderShield.writeBit(1, 3);
expanderShield.writeBit(1, 4);
delay(1000);
// turn off all the lights
expanderShield.writeByte(0, 0);
delay(1000);
// Create a light display by shifting a bit from one side to the
other, increasing speed
for(int i = 0; i < 20; i++)
{
output = 1;
for(int j = 0; j < 8; j++)
{
// Write a byte to device 0 (the first I2C extender)
expanderShield.writeByte(output, 0);
delay(600 - (i * 30));
output = output << 1;
}
}
}
This example shows the user how to use the PCF8574AP I/O expander shield,
and the very i rst thing it does is to include that library. To be able to use that
library, the user must provide two pieces of information: the address for each
component. To make this clear, the addresses are included as dei ne statements
on lines 5 and 6.
On line 9, the PCF8574AP object is created, called expanderShield . By using
the dei ned addresses, the code becomes more readable, and the user understands
what is required to get started. On line 13, the setup() function is declared, as
with any sketch. Inside, the serial connection is coni gured and expanderShield
is initialized with a begin() function.
The loop() function is declared on line 20, and this is where the example code
will be placed. To show how the library can write words (or 16-bit numbers),
the example uses the writeWord() function. This sets all the outputs to HIGH,
turning the LEDs off.
Next, the user is presented with an example on how to use writeByte() .
A series of four commands are run, each time setting more and more outputs to 0.
The effect of this is to turn on the LEDs from the edge towards the center.
The next series of instructions demonstrates how to write individual bits
using the writeBit() function. Once again, a visual effect is created, this time
turning the LEDs off from the edge towards the center.
Search WWH ::




Custom Search