Acceleration support for Rotary Encoders

 /*
last edited: February 21, 2011, at 01:28 PM by slavianp;

17.02.2013:
i (Stefan: s-light ) have tried the library 17.02.2013
and it do not work out of the box for me with arduino 1.0.3.
first thing: make it arduino 1.0 compatible and include
  /**
    * Includes Core Arduino functionality 
    **/
  #if ARDUINO < 100
  #include <WProgram.h>
  #else
  #include <Arduino.h>
  #endif
in every library file.
i have done this but don't get it to work - i don't know why. at the moment i have no time to debug the thing.
*/


19 Feb 2013 - ajs001 - See this thread for some insight: http://arduino.cc/forum/index.php/topic,85547.0.html Also, note that "Acceleration" is spelled incorrectly in some places in this library. That would be really great to fix. I may make an attempt. Also, it worked for me when I moved the 3 folders out of the "libraries" folder and put them in the main ../Arduino app support/libraries/ I'm not sure whether that had to do with the mis-spelling on the wrapper folder. More later.


This is my attempt to make a more intelligent processing of a rotary encoder. I have gone through the article for rotary encoders http://playground.arduino.cc/Main/RotaryEncoders and tried the hints. The code did work, but the cheap encoder I got was bouncing, i.e. rotating the knob constantly only in increasing direction every now and then produced a signal for decreasing. So I needed to de-bounce the encoder readings somehow and I wanted it to be software only de-bouncing.

Ok. I made the software de-bouncing but in my application the encoder was used to count from 50 to 5000. This is quite a span and rotating the encoder 5000 steps is by far not user friendly. I needed to take into account the speed at which the encoder is rotated - the faster the encoder is rotated the greater the step of increment/decrement will be and keeping in mind that the encoder must be able to step by 1 if rotated slowly enough.

Next I wanted to make this as a re-usable library that can be used with and without attaching the encoder to an interrupt.

The attached zip file contains the needed libraries (maybe it became over-re-using)... Attach:RotaryEncoderAcceleration.zip

Bellow is a small sample code to implement an accelerated rotary encoder support in an Arduino sketch. The rotary encoder pinA is connected to Adruino pin 2 and pinB - to Arduino pin 3. A speaker connected to pin 8.

Code without using interrupt:


#include <WProgram.h> // This include should go first, otherwise does not compile.
#include <Button.h>
#include <TicksPerSecond.h>
#include <RotaryEncoderAcelleration.h>

const int rotorPinA = 2;
const int rotorPinB = 3;
const int speakerPin = 8;

RotaryEncoderAcelleration rotor;

void setup() {
	pinMode(speakerPin, OUTPUT);
	rotor.initialize(rotorPinA, rotorPinB);
	rotor.setMinMax(50, 5000);
	rotor.setPosition(500);
}

void loop() {
	rotor.update();
	tone(speakerPin, rotor.getPosition());
}

Code using interrupt:


#include <WProgram.h> // This include should go first, otherwise does not compile.
#include <Button.h>
#include <TicksPerSecond.h>
#include <RotaryEncoderAcelleration.h>

const int rotorPinA = 2;
const int rotorPinB = 3;
const int speakerPin = 8;

RotaryEncoderAcelleration rotor;

void UpdateRotor() {
	rotor.update();
}

void setup() {
	pinMode(speakerPin, OUTPUT);
	rotor.initialize(rotorPinA, rotorPinB);
	rotor.setMinMax(50, 5000);
	rotor.setPosition(500);
	attachInterrupt(0, UpdateRotor, CHANGE);
}

void loop() {
	tone(speakerPin, rotor.getPosition());
}

Share