I have a project in which I control a display using an Arduino Due microcontroller.

The user can navigate on the TFT display (RA8875 driver ic) with a rotary enconder/pushbutton. The display shows a certain menu. You can the selected item in the menu by rotating the rotary encoder and you can move into a submenu by pressing the rotary encoder (because it is also a button). This is how the menu looks like:

Menu example

The problem is that sometimes when rotating the encoder the menu doesn't refresh the screen properly and seems to have gotten stuck while drawing the screen. Though when rotating the encoder some more the screen gets properly rendered so the code isn't actually 'stuck'. This is een example of a bad rendering after rotating the encoder. (Sometimes no text gets written, sometimes some text gets written):

bad rendering example

I also made a video to make my problem more clear: Video with rendering problem.

The code for my menu is rather big so I'll try to explain the working principle and attach the used Arduino code.

I attached an interrupt to the encoder signal and to the push button.

attachInterrupt(digitalPinToInterrupt(BTN_ROT), push_button_interrupt, FALLING); attachInterrupt(digitalPinToInterrupt(ROT_A), rotary_enc_int, LOW);

In the interrupt function I update a 'virtual position' value. In my main loop I run a function that checks if the virtual position is different from the last seen position and updates the 'current_index'-value of the menu accordingly. It also sets 'refresh_menu' to True. In the main loop the variable 'refresh_menu' gets checked and if true the screen gets redrawn with the menu item selected according to the value 'current_index'.

Link to full project code: Arduino due code. All the relevant code for this problem is in 'Menus.h'.

[Side note] I am not sure if my approach to build a menu is 'good'. It's not super responsive and the code is hard to maintain (changing a menu-item from position requires a lote of changes in the code). Any advice in a better approach is appreciated!

  • the video shows serious signal bounce ... rotary_enc_int should be called on signal transition, not on signal level .... increase the time difference in the ISR to 50 ms from 5 ms ... i think that you accidentally forgot the zero – jsotola Oct 31 '20 at 18:13
  • Hi @jsotola, thank you for looking into my code and giving feedback. When I started the project I tested every component seperatly. I also thought I should use FALLING or RISING instead of a signal level. But for some reason - I didn't and stil do not understand - it didn't work but it worked when using the LOW state as I had seen in a tutorial. I tried your feedback and this is what I got (it didn't transition properly jumped back and forth) video: drive.google.com/file/d/1ELb0AcuUl-Lxjt6BBYF45CzOaRhEknRN/… (both rising and falling worked like this). – Sven Onderbeke Oct 31 '20 at 19:00
  • menus.h, line 122, lastInterruptTime = interruptTime; .... this needs to be inside the if block ..................... the if may work better as if (interruptTime - lastInterruptTime > 50) – jsotola Oct 31 '20 at 19:05
  • Thank you @jsotola, you are right about the position of lin 122. Thank you for your help, I positioned it inside the if block. As from what I experience I do not have any debouncing issues but I changed the debounce time to 50 an it still worked as before but when rotating fast it didn't see as many rotations as before. So I did reset it to 5. – Sven Onderbeke Nov 1 '20 at 13:18
  • As for my problem: I didn't find the bug in my program but I tried another approach: instead of redrawing the screen for every change of my index (which requires some time to do) I threw away the color indication of my index and just prepended the selected index with '>'. I do not refresh the whole screen when the index changes but I draw a cube over the old '>' and draw the new one. This also elminated the refreshflickering involved with redrawing the screen. – Sven Onderbeke Nov 1 '20 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.