I have m2tklib working with my encoder. My code is below and is based on the sketch located here Google Code Archive - Long-term storage for Google Code Project Hosting..
My encoder is not working smoothly. It is taking, on average, a 2-detent rotation of the encoder to register any change. There doesn't seem to be anything I can change within the sketch to make this work. I have had this encoder working smoothly in other sketches with code borrowed from here: Buxtronix: Rotary encoders, done properly.
Is there anything I can do?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "M2tk.h"
#include "utility/m2ghnlc.h"
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//=================================================
// Forward declaration of the toplevel element
M2_EXTERN_ALIGN(top_menu);
//=================================================
// Simple dialog: Input two values n1 and n2
uint8_t n1 = 0;
uint8_t n2 = 0;
M2_LABEL(el_l1, NULL, "value 1:");
M2_U8NUM(el_u1, "c2", 0, 99, &n1);
M2_LABEL(el_l2, NULL, "value 2:");
M2_U8NUM(el_u2, "c2", 0, 99, &n2);
M2_LIST(list) = { &el_l1, &el_u1, &el_l2, &el_u2 };
M2_GRIDLIST(el_gridlist, "c2", list);
M2_ALIGN(top_menu, "-1|1W64H64", &el_gridlist);
// m2 object and constructor
// Note: Use the "m2_eh_4bd" handler, which fits better to the "m2_es_arduino_rotary_encoder"
M2tk m2(&top_menu, m2_es_arduino_rotary_encoder, m2_eh_4bd, m2_gh_nlc);
//=================================================
// Arduino Setup & Loop
void setup() {
m2_SetNewLiquidCrystal(&lcd, 16, 2);
// define button for the select message
m2.setPin(M2_KEY_SELECT, 4);
// The incremental rotary encoder is conected to these two pins
m2.setPin(M2_KEY_ROT_ENC_A, 10);
m2.setPin(M2_KEY_ROT_ENC_B, 9);
}
void loop() {
m2.checkKey();
m2.checkKey();
if ( m2.handleKey() )
m2.draw();
m2.checkKey();
}