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 download page in your default browser.
9 * http://firmata.org/wiki/Download
12 /* This firmware supports as many analog ports as possible, all analog inputs,
13 * four PWM outputs, and two with servo support.
15 * This example code is in the public domain.
20 /*==============================================================================
22 *============================================================================*/
25 Servo servo9, servo10; // one instance per pin
27 int analogInputsToReport = 0; // bitwise array to store pin reporting
28 int analogPin = 0; // counter for reading analog pins
30 unsigned long currentMillis; // store the current value from millis()
31 unsigned long previousMillis; // for comparison with currentMillis
34 /*==============================================================================
36 *============================================================================*/
38 void analogWriteCallback(byte pin, int value)
41 case 9: servo9.write(value); break;
42 case 10: servo10.write(value); break;
47 analogWrite(pin, value);
51 // -----------------------------------------------------------------------------
52 // sets bits in a bit array (int) to toggle the reporting of the analogIns
53 void reportAnalogCallback(byte pin, int value)
56 analogInputsToReport = analogInputsToReport & ~ (1 << pin);
58 else { // everything but 0 enables reporting of that pin
59 analogInputsToReport = analogInputsToReport | (1 << pin);
61 // TODO: save status to EEPROM here, if changed
64 /*==============================================================================
66 *============================================================================*/
69 Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
70 Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
71 Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
78 /*==============================================================================
80 *============================================================================*/
83 while (Firmata.available())
84 Firmata.processInput();
85 currentMillis = millis();
86 if (currentMillis - previousMillis > 20) {
87 previousMillis += 20; // run this every 20ms
88 for (analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) {
89 if ( analogInputsToReport & (1 << analogPin) )
90 Firmata.sendAnalog(analogPin, analogRead(analogPin));