Hi JohnRD,
You are correct. To set Pin 13 to output you send the 3 bytes - Hex values F4 0D 01. To actually set the values or states (on/off) of the digital pins you address an entire port at once. On a Duemilanove board the ports (as Firmata sees them) are set up as follows:
Port 0 - pins 2,3,4,5,6,7
Port 1 - pins 8,9,10,11,12,13
You turn these digital pins on/off by sending a 3 byte message in the following format:
Port | 1st Byte | 2nd Byte
Where Port is 0x90 for Port 0, 0x91 for Port 1 ... (and that's all you need for the Duemilanove it only has that many digital pins)
The 1st and 2nd Bytes are the 'bitmasks' for the port in question. Think of the bits in the 1st and 2nd Byte as individual switches for the pins in that port.
So to send a message to turn on Pin 13 you send:
Message |
Port |
1st Byte |
2nd Byte |
Hex version of message |
0x91 |
0x20 |
0x00 |
Hex and byte bits version |
0x91 |
00100000 |
00000000 |
The second row in the table shows the Hex values. In the third row I've shown the bits in each byte. Pin 13 is actually the 6th pin in Port 1 so to turn it on you set the 6th bit to a '1'.
Each time you send a digital I/O message you are actually setting the state of ALL the pins in that port. So in the example above, when you turned ON pin 13 the message did turn on pin 13 but it also turned OFF all the other pins (all those other bits set to '0').
To effectively use Digital I/O Messages you have to keep a track of which pins are on or off in your software. If pin 10 is on and you decide to then turn on pin 13 and use the message above you'd be turning pin 13 on but 'accidentally' turning off pin 10.
Below are the bits that correspond to each pin on the board:
Port 0 (pins 2,3,4,5,6,7)
1st Byte 2nd Byte
X X 13 12 11 10 9 8 X X X X X X X X
Port 1 (pins 8,9,10,11,12,13)
1st Byte 2nd Byte
X X 6 5 4 3 2 X X X X X X X X 7
In Port 1 pins 0 and 1 are RX and TX so aren't used. Wherever you see an X above you should always simple send a 0 (zero).
So... let's say you want to turn on pins 13, 11 and 8 you'd send:
0x91 followed by the following 2 bytes of 'bitmasks' - 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0
In Hex that'd be 0x91 0x29 0x00.
If you then want to turn off only pin 11 you'd have to send 0x91 followed by 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0
Which sets the bit for pin 11 to 0 but leaves the bits for pin 13 and 8 set to 1
I hope this was of some help and that it made sense.
Happy bitmasking,
Andrew