2 * Firmata is a generic protocol for communicating with microcontrollers
3 * from software on a host computer. It is intended to work with
4 * any host computer software package.
6 * To download a host software package, please click on the following link
7 * to open the list of Firmata client libraries in your default browser.
9 * https://github.com/firmata/arduino#firmata-client-libraries
12 /* Supports as many digital inputs and outputs as possible.
14 * This example code is in the public domain.
18 byte previousPIN[TOTAL_PORTS]; // PIN means PORT for input
19 byte previousPORT[TOTAL_PORTS];
21 void outputPort(byte portNumber, byte portValue)
23 // only send the data when it changes, otherwise you get too many messages!
24 if (previousPIN[portNumber] != portValue) {
25 Firmata.sendDigitalPort(portNumber, portValue);
26 previousPIN[portNumber] = portValue;
30 void setPinModeCallback(byte pin, int mode) {
31 if (IS_PIN_DIGITAL(pin)) {
32 pinMode(PIN_TO_DIGITAL(pin), mode);
36 void digitalWriteCallback(byte port, int value)
39 byte currentPinValue, previousPinValue;
41 if (port < TOTAL_PORTS && value != previousPORT[port]) {
42 for (i = 0; i < 8; i++) {
43 currentPinValue = (byte) value & (1 << i);
44 previousPinValue = previousPORT[port] & (1 << i);
45 if (currentPinValue != previousPinValue) {
46 digitalWrite(i + (port * 8), currentPinValue);
49 previousPORT[port] = value;
55 Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
56 Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
57 Firmata.attach(SET_PIN_MODE, setPinModeCallback);
65 for (i = 0; i < TOTAL_PORTS; i++) {
66 outputPort(i, readPort(i, 0xff));
69 while (Firmata.available()) {
70 Firmata.processInput();