easy arduino menus for rotary encoders

by:HENGXIANG     2020-05-27
Rotary encoder with center
Buttons are cool input hardware for projects, especially when you want to scroll through the menu and select options.
Hopefully this Instructure will give you the confidence to try a basic menu system and combine it with a rotary encoder to provide a professional feel for your next project!
Why code is needed for menus and rotary encoders. I want to have a menu in the upcoming project and use the rotary encoder with the center button as my input hardware.
This is very similar to the LCD controller for the ramp and Arduino MEGA 3D printer.
The rotary encoder allows me to scroll through the menu option I. e.
Navigation menu, select sub-
Menu and change the value in the submenuMenu/settings-
A very common hardware interface!
Managing all of this requires a micro-controller, which requires instructions, that is, code!
Other options my problem with the existing Arduino menu library and menu code is that they are too complicated for simple menus.
Another drawback of many alternatives is that the code is designed for LCD screens and instant buttons, not rotary encoders, for other display outputs.
These menus revolve around selecting between a small number of modes and add value relatively slowly.
We know that rotary encoders are a good hardware input option because they provide relatively fast input value changes while maintaining good control at low speeds.
I want to write code that allows clear top-level menu navigation, but also allows you to quickly scroll through the large range of values in each submenu
Menu/settings, take advantage of the rotary encoder.
ApproachI decided to take some of the suggestions when using ()
Statement of simple menu structure and keep sketch-based.
The result code is based on my previous Instructable, which tries to reliably read the spin pulse and direction.
Please check the background.
In this sketch, we added the reading of the center button on the axis of the rotary encoder, using the code developed by Nick Gammon to record the change of the button state without relying on the Arduino delay ()
Prevent the micro-controller from performing other code functions and may introduce obvious delays in our sketches, E. G. g.
Slow display refresh rate.
When selecting once with a button, the button status change code is more useful than just reading the digital logic high or low, such as the menu option, because it can help you prevent multiple selections accidentally when each button is pressed.
Let\'s take a look at what needs to be set up to use this sample code in step 1.
If you haven\'t already, please see my other instructions on rotating encoder readings for how to set up your hardware and Arduino IDE software.
The hardware picture shows the additional hardware connection needed to use the center button.
I drew this drawing with Fritzing, but it doesn\'t have a rotary encoder assembly, it represents the most likely pin layout, so just use the drawing in conjunction with the annotation, and look at the photo of the rotary encoder to see what you are more likely to look for in terms of the rotary encoder pin layout.
Rotate one of the two pins on one side of the encoder (
And there are three pins next to it)
Needs to be connected to the ground and the other is connected to the digital pin on the Arduino.
I used D4 in the sample sketch.
If you choose a different pin, don\'t forget to change the value of the button pin in the sketch.
Next is the code in step 2.
This is the code.
By looking at the structure and reviews, I hope you will find it easy to adapt to your specific needs!
I used \"debug\" at the beginning of each comment on any line, which is not important for the menu\'s work.
If you are satisfied with the functionality of the menu, you may need to comment or delete these lines for smaller compiled sketch size.
Note that a key part of menu navigation is to give feedback to users as they scroll through options and settings.
So if you choose not to include a debug line, you should probably use another visual indicator (e. g.
LCD text display (led)
Encoder input is navigating the menu and changing the settings.
If I comment out the debug line (
Note that some visual feedback is still needed for menu navigation)
The compiled code is about 1,650 bytes of Arduino Uno, hoping to leave enough space for your ATMEGA328P to make your sketch more exciting!
Go to step 3 to see how the menu system works.
Operation if you open the serial monitor in Arduino after uploading this sketch and start turning the encoder axis, you should see the top-level menu go through the sub-
The menu/options you have (
Limit using modeMax variable).
If you press center-
Press the button and you will see the mode/sub mode
The menu you scroll to is selected and now you are free to scroll to 0-
255 values in this submenu.
Now, if you press center-
Press the button and you set the value to setting1, setting2, or setting3, etc.
Once this happens, Arduino will automatically and immediately return you to the top menu.
When powered on, Arduino will remember why you set each setting, if you go back to sub-
Menu for the settings you have set the value to, it will start the encoder adjustment from the last value you selected!
I started to write some sketches-
The code-based allows the rotary encoder to navigate the basic menu of ardunos.
I\'m also trying to make it readable so that, unlike some alternatives, someone can see the menu structure and know what changes they need to make to the code to customize the menu to their needs.
This piece of code is basic and generic, especially to demonstrate functionality while it\'s easy to adapt to your own application.
It uses a serial monitor as a basic debugging tool, and it doesn\'t need a separate display if you want to see how the code works.
I hope you find it useful and inspired by editing, tweaking and improving it!
Please share anything you do with it in the comments!
Custom message
Chat Online
Chat Online
Chat Online inputting...