firmata + quadrature encoder input + MAX

I use firmata on the arduino to communicate with Max/MSP. The use of the maxuino patch in max is quite useful since I need to send a couple pwm and digital out for my project. So everything is fine there when I use it for this only.

But I also use a quadrature encoder (USDigital part #LFLS7184) with the code below (by Gordon McComb) that work flawlessly. Again this by itself is working great.

I need to be able to integrate this code in a firmata/maxuino compatible pde.

Simply said just need to send to MAX "encoderACount" (the count of the encoder that is the result of the input collected from digital port 2 and 4) and keep all the flexibility of the Maxuino patch open.

Thanks for your help on this, firmata is a bit complex for me.

emile


/*
Quadrature encoder example
Interface to LSI LS7184 quadrature clock converter
(USDigital part #LFLS7184)

Demonstrates reading quadrature encoder using Arduino interrupts.
Clock output is connected to pin 2 (Int0); direction output is
connected to pin 4.
Current position is displayed in Serial Monitor window.
*/

const int clkPinA = 2;
const int dirPinA = 4;
volatile int encoderACount = 0;
volatile boolean changeFlag = false;

void setup() {
Serial.begin(115200);
pinMode(clkPinA, INPUT);
pinMode(dirPinA, INPUT);
attachInterrupt(0, encoderIntA, RISING);
}

void loop() {
if (changeFlag) {
changeFlag = false;
Serial.print("position: ");
Serial.println(encoderACount);
}
}

void encoderIntA() {
if (digitalRead(dirPinA) == HIGH)
encoderACount++;
else
encoderACount--;
changeFlag = true;
}

I need to be able to integrate this code in a firmata/maxuino compatible pde.

You've got your work cut out for you, then. The firmata protocol does not have a mechanism for getting a variable's value from the Arduino.

The firmata protocol does not have a mechanism for dealing with interrupts, either.