- Edited
Hi jarm,
Yeah, I had a series of mechanical encoders running for a synth interface that ended up half built and that I still haven't had the time to get back to.
I used these ones with a switch on top and without notches – http://uk.rs-online.com/web/p/mechanical-rotary-encoders/6234237/
They're pretty close the ones used on the Akai mpcs if you know the feel.
Quite simple approach for reading the 2-bit gray code to sense direction - same as you would do for Arduino but without interrupts. With these mechanical encoders, as I understand it, the time difference between the two out of phase square waves that you compare to tell the direction of motion is dependent on the speed at which you turn the dial so comparing the readings from the pins every sample is definitely fast enough.
A consideration is number of digital pins available as each encoder takes up two so eight encoders and you're full up. That's where shift registers are necessary – I have half working code for daisy-chained 74HC165 if you're interested (it needs some work). P.s. code below cobbled together and not tested running as I don't have a Bela handy atm.
#include <Bela.h>
int encoder0PinA = P8_08;
int encoder0PinB = P8_07;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int status = LOW;
float gInterval = 0.25;
float gSecondsElapsed = 0;
int gCount = 0;
bool setup(BelaContext *context, void *userData)
{
// Set the mode of digital pins
pinMode(context, 0, P8_08, INPUT);
pinMode(context, 0, P8_07, INPUT);
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n=0; n<context->digitalFrames; n++){
status = digitalRead(context, n, encoder0PinA);
if ((encoder0PinALast == LOW) && (status == HIGH)) {
if (digitalRead(context, n, encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
}
encoder0PinALast = status;
// Increment a counter on every frame
gCount++;
// Print a message every quarter second indicating the current encoder count
if(gCount % (int)(context->audioSampleRate*gInterval) == 0) {
gSecondsElapsed += gInterval;
rt_printf("Encoder reading: %d\n",encoder0Pos);
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}