Rotary Encoder Arduino Library (debounced)
Rotary Encoder Arduino Library for measuring quadrature encoded signals
ARDUINO Family
#include "RotaryEncoder.h"
// -- Default initializing of Rotary Encoder
// CLK = pin 3, DAT = pin 2, SW = pin A1, CW = count up, CCW = count down
// One instance only is allowed due to plain ISR usage with global data
RotaryEncoder rotaryEncoder = RotaryEncoder();
// -- Aternative initialization
// Individual initializing of Rotary Encoder
// CLK = pin 2, DAT = pin 3, SW = pin 4, CW = count down, CCW = count up`
RotaryEncoder rotaryEncoder = RotaryEncoder(2,3,4);
// -- Apply Rotary Encoder methods
void loop() {
...
// -- Has to be inside loop() to enable rotary debouncer
rotaryEncoder.startDebouncer();
// -- Enable Button to clear recent encoder position if pushed, set to 0
rotaryEncoder.updateClearButton();
// -- Verify if encoder position has changed
if (rotaryEncoder.hasChanged()) {
// -- Update counter for the dial (values 0..15)
volatile byte encoderPos = rotaryEncoder.encodedPos() % 16;
do_something_with(encoderPos);
}
...
}
Rotary Encoder Library for measuring quadrature encoded signals
For KY-040 Rotary Encoder Module
Web Search String: "ky-040 rotary decoder encoder module for arduino"
The library is tested with some ARV Arduino only. It may possibly work with other MCUs.
This code was inspired by
"Another Interrupt Library THAT REALLY WORKS" by rafbuff http://playground.arduino.cc/Main/RotaryEncoders
"Encoder Library, for measuring quadrature encoded signals" by Paul Stoffregen http://www.pjrc.com/teensy/td_libs_Encoder.html
Thanks to all.
Copyright (c) 2015 Thomas Walloschke. All rigths reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.