Speed up Firmata

Hi, I've been trying to optimize the Firmata code on my Arduino to execute as quickly as possible. I only need the analog sensor data, so I've deleted everything else from the code. Right now, the analog sensor reading routine takes 7 ms. If I set it any lower, the damn thing stops working... I've already set the baud rate to 115200 but I think it's far from being a bottleneck.

Anyone know of any possible optimizations that can be made? Ideally, I'd like to shorten the routine to 1 ms.

[EDIT: FWIW, I'm communicating between Arduino and Max/MSP over USB. If there's any faster protocol to use, I'd be glad to try it out!]

Here's my code. It's not pretty, I know, but I'm a beginner :slight_smile:

 * Firmata is a generic protocol for communicating with microcontrollers
 * from software on a host computer. It is intended to work with
 * any host computer software package.
 * To download a host software package, please click on the following link
 * to open the list of Firmata client libraries in your default browser.
 * https://github.com/firmata/arduino#firmata-client-libraries

/* This firmware supports as many analog ports as possible, all analog inputs,
 * four PWM outputs, and two with servo support.
 * This example code is in the public domain.
// #include <Servo.h>
#include <Firmata.h>


/* servos */
// Servo servo9, servo10; // one instance per pin
/* analog inputs */
int analogInputsToReport = 0; // bitwise array to store pin reporting
int analogPin = 0; // counter for reading analog pins
/* timer variables */
unsigned long currentMillis;     // store the current value from millis()
unsigned long previousMillis;    // for comparison with currentMillis


//void analogWriteCallback(byte pin, int value)
//  switch (pin) {
//    case 9: servo9.write(value); break;
//    case 10: servo10.write(value); break;
//    case 3:
//    case 5:
//    case 6:
//    case 11: // PWM pins
//      analogWrite(pin, value);
//      break;
//  }
// -----------------------------------------------------------------------------
// sets bits in a bit array (int) to toggle the reporting of the analogIns
void reportAnalogCallback(byte pin, int value)
  if (value == 0) {
    analogInputsToReport = analogInputsToReport & ~ (1 << pin);
  else { // everything but 0 enables reporting of that pin
    analogInputsToReport = analogInputsToReport | (1 << pin);
  // TODO: save status to EEPROM here, if changed

 * SETUP()
void setup()
//  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.attach(REPORT_ANALOG, reportAnalogCallback);

//  servo9.attach(9);
//  servo10.attach(10);

 * LOOP()
void loop()
  while (Firmata.available())
  currentMillis = millis();
  if (currentMillis - previousMillis > 7) {
    previousMillis += 7;                   // run this every 7ms
    for (analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) {
      if ( analogInputsToReport & (1 << analogPin) )
        Firmata.sendAnalog(analogPin, analogRead(analogPin));

I have never used Firmata. You don't say which Arduino you use. Do you have connected sensors on all analog inputs? And send all their values? Maybe the speed of 115200 will be limiting. Why just not increase speed? If you use Arudino with ATmega328 you can try to increase the ADC speed through the ADCSRA register.

Does this post differ from your other one? What’s the fastest way…?

Not really, but since this one didn’t get any replies, I tried rephrasing it. :slight_smile: Sorry if you find I’m littering the forum; that wasn’t my intention. I will delete this thread shortly, if possible.

You probably can't delete your topic. I will report this one and request it to be merged.

You’re right, I can’t. Thanks for the heads up!