Question about ofArduino "encoder"

Hi everyone, I’ve got a quick question regarding the ofArduino class,

I’d like to get readings from a rotary encoder attached to my arduino into openFrameworks, I saw that ofArduino has functions like ‘attachEncoder’ and ’ getAllEncoderPositions’, so I was wondering if that could be related to what I’m trying to do and this refers to rotary encoders or similar inputs, however none of these functions have any sort of documentation so I’m a bit lost…

I tried to use attachEncoder with my two encoder pins, but I get a console message “ofArduino: Encoder Control is not supported for pin 5”, and I tried this for (digital) pins with and without pwm so I don’t understand what the issue could be.

Am I misunderstanding this and encoder means something different in this context, or is there a way to get this to work? Otherwise I would probably use an encoder library on the arduino and send the values over via serial which would be a lot more work.

Any help would be appreciated, thanks in advance !!

Hi,
I’ve worked with Arduino and encoders in the past, not with ofArduino and firmata.
However, here’s a wild guess, assuming you’re using an Arduino Uno:
I think encoders need an interrupt-pin, and on an Uno these would be pin 2 and 3. Can you try them?

When calling getPinCapabilities() it’ll return a std::map with all the supportedPinTypes, so I guess you could check the pins (getPinCapabilities()[2].encoderSupported ?) you want to use for encoder support (/interrupt support). (I’m reading ofArduino.h in libs/openFrameworks/communication).

I haven’t tested anything of this, so I may be wrong. Hopefully I’m not, and this solves it all.

Hi Jildert,

Thanks for the quick reply !!
I’ve looked at the ofArduino source as well by now, and you seem to be onto the problem, none of my digital pins have that ‘encoderSupported’ flag (even 2 and 3 unforunately), which seems to be what prevents this from working.
The board I have should be identical to an arduino uno, so I don’t know what the issue could be…
Unfortunately I’m not that familiar with cpp/oF, so I’m having a hard time figuring out how that getPinCapabilities() function collects the information it returns, I guess it’s getting it from firmata on the board so maybe there is an issue there?

I guess at this point it may just be easier to drop firmata, get the encoder value on the arduino and send it over via serial, kind of a shame since the whole encoder part of ofArduino seems specifically for that, but I don’t really have to time to dig into the source and figure it out right now.

Thanks for your help anyway!

Hi Jonas,
I’ve figured it out. The StandardFirmata.ino sketch wasn’t able to handle the encoder support.
Therefore responds with encoderSupported 0 for all pins.
So I’ve tried firmata/FirmataEncoder which works with the following code (add or replace):
ofApp.h

    void postEncoderValue(const std::vector<Firmata_Encoder_Data> &vec);
    ofArduino a;

ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){
    a.connect("/dev/cu.usbmodem14101");
    ofSetFrameRate(60);
}

//--------------------------------------------------------------
void ofApp::update(){
    a.update();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    switch(key){
        case 'i': // Wait for the LEDs on the Arduino to stop blinking, then press i for init (attaching the encoder)
            a.sendPinCapabilityRequest();
            for(int i=0; i<a.getPinCapabilities().size(); i++)
                cout << i << " " << a.getPinCapabilities()[i].encoderSupported << endl;
            a.attachEncoder(2, 3);
            ofAddListener(a.EEncoderDataReceived, this, &ofApp::postEncoderValue); // From examples/events/simpleEventsExample
            break;
    }
}

//--------------------------------------------------------------
void ofApp::postEncoderValue(const std::vector<Firmata_Encoder_Data> &vec){
    cout << vec[0].position << endl; // Should check all elements from vector?
}

My readings aren’t very stable (direction gets misread when dialing quick), but I guess an MCU with a faster clock may fix this.

1 Like

Ohhh wow that’s amazing, it actually works really well for me, way better than the dodgy serial parsing thing I put together…

Thank you so much for your help !! I really appreciate it

1 Like