Go Down

Topic: m2tklib and encoder problem (Read 1 time) previous topic - next topic


I have m2tklib working with my encoder. My code is below and is based on the sketch located here http://code.google.com/p/m2tklib/source/browse/arduino/LiquidCrystal/RecEnc/RotEnc.pde?r=27061897ecbcd61a7c9904d0934f173f57c8a120.

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: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html.

Is there anything I can do?

Code: [Select]
#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

// 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() {
  if ( m2.handleKey() )



Maybe you can visit http://code.google.com/p/m2tklib/wiki/rotenc and check the ino files there. Will these files work better?

You can also tweak variables inside http://code.google.com/p/m2tklib/source/browse/dev/arduino/m2esarduino.c:
Try to use smaller values for
Code: [Select]


Finally you can rewrite "uint8_t m2_es_arduino_rotary_encoder(m2_p ep, uint8_t msg)" and replace the internal rotary encoder code with something more suitable: http://code.google.com/p/m2tklib/source/browse/dev/arduino/m2esarduino.c



Thanks for the tips! I have it working reasonably well now.

Initially I reduced the debounce and division variables you suggested down to 1. It was working ok but would usually miss an event on a change in encoder direction. I tried v2 of the encoder file (at http://code.google.com/p/m2tklib/wiki/rotenc) and found it to be similar.

Then I noticed that the m2esarduino.c file contained both versions of the encoder state table. I changed the if/else statement so that it would use the v1 table and that got it working near perfect. I have since had to increase the debounce (still playing around with it) but it is much better.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131