How to correct skipping menu item with encoder (item 2 to 4 to 6 instead of 1 to 2 to 3 to 4) in both directions

Dear all,
I started this year with programming Arduino boards and really like it. For a personal music project I am designing a hardware guitar/bass buffered switch (2 in 16 out). Each output channel goes to a specific audio pre-amp. Each channel has to be selected with an encoder and has to be switched on/of with the button of the encoder. Status of the related channel has to be projected on the LCD. I planned to do this with a Mega board, 1 encoder with an integrated button and a 16x2 LCD with I2C interface and a 16 channel relayboard. I made de code already. All elements are working except for the encoder part. Every tick of the encoder, it seams to skip 1 menu item: from channel 2 to 4 and 6 etc and vice versa. The menu is rotating to both sides (16 characters of the display). When I use the the same hardware with a simple interrupt encoder code, the encoder works fine. I can't find a proper solution. I can only think of increasing the arrays with empty "spaces", but this is not a proper programming solution. (The code also got a timer incorporated and has an overview screen and 16 composed channel pre-amp details screen (array data per channel).) So I am nearly there to start building the project, but I need some help with this encoder problem.
(I also tried to solve it with capacitors as de-bounce solution, without luck).
Regards, Rick

Only if you take both slopes of the clock signal as a tick. Use only the rising or falling slope, not both.

Is your handling of the encoder exactly the same in both codes?

Start with some diagnostic prints to see if the problem is your processing of the encoder pulses or how you use them to control / access the menu items.

1 Like

Thanks for your feedback. The code is different, however I used the working code (1 tick 1 action) with interrupts in my code, but then the interrupts don't works anymore. Code part is probably too long with a link to other functions. I tested this by adding and removing serial.print items in the code. The code part which looks at the encoder rotation is.

//function to check encoder status and update menu on lcd
void encoderStatus(){
  int newPos = myEnc.read();
  if (newPos != prevPos) {
      // Updates selected menu-item index based on rotation direction of encoder
     if (newPos > prevPos) {
        currentItem++;
       if (currentItem >= NUM_MENU_ITEMS) {  //num_menu_items is 16 (channels); current items is index of array with names of preamps
         currentItem = 0;
       }
     }
  else { 
    currentItem--;
    if (currentItem < 0) {
        currentItem = 15; 
      }
  }
  prevPos = newPos;
  previousTimeoutMillis = millis();
  printMenuItem(currentItem); // Print the last selected menu-item
  }
}

In the header of the code I used:

#include <Encoder.h>

// PinsCLK, DT en SW of the encoder
#define CLK_PIN 2 
#define DT_PIN 3
#define SW_PIN 6

// Initialize  encoder
Encoder myEnc(DT_PIN, CLK_PIN);

Any thoughts on incorrect parts or other approaches?

Thanks for your feedback. I just posted in a reaction to an other member the code part I use. I have basic understanding of encoders, but not specific knowledge looking at interaction of the encoder code with the encoder library and how to make a distinction between rising an falling slopes . Any suggestions how I can improve on this?

For mechanical (bouncing) contacts interrupts only make things more complicated.

Hello am_space

Welcome to the worldbest Arduino forum ever.

Take a view here to get some ideas.

Have a nice day and enjoy coding in C++.

What did you debug prints you added per my suggestion show?

Thanks. I will check this out. Looks good. Regards, Rick

Turning once CCW (1 tick) and the once CW (1 tick) give the following serial print data:

1 tick CCW:
21:31:19.588 -> rotation once ++: 1
21:31:19.639 -> rotation once ++: 2

1 tick CW:
21:31:24.865 -> rotation once --: 1
21:31:24.924 -> rotation once --: 0

I have used three different types. The best results are the above mentioned. With other encoders data goes all over place (skipping, jumping around)

encoder-code that uses a state-table has never problems with bouncing.
The state-table-approach does the debouncing.

In my experience this "standard"-encoder-library does not work reliably.

I always use the new-encoder written by user @gfvalvo. I had never any problems.
Not even with 50 cent cheap mechanical encoders.

best regards Stefan

Thanks for your feedback. I will try this!

Thanks for your direction to the NewEncoder library of gfvalvo. I was running in all kinds of programming circles, but could fix the jumping (+1) menu item with my encoder. Now it works after some testing and inching with the "void configure" function values.

Really appreciated your efforts to help me out!

Best regards,
Rick

I got the problem with the skipping menu item with my encoder fixed. I was directed to the NewEncoder.h library. Did you develop this one (looking at your name)? If so, thank you very much. This library fixed my encoder problem!

Best regards,
Rick

Glad to hear it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.