By dave | June 14, 2019

TcMenu base input plugin supports Rotary Encoders that are either connected directly to Arduino pins or via an I2C expander such as a PCF8574 or MCP23017. This support is provided by the IoAbstraction library that’s automatically included by the menu designer UI. The only limitation for rotary encoders is that PinA must be connected to an interrupt capable pin (or if connected to an i2c device that must be connected to an interrupt capable pin).

Configuring for your display

First, ensure your menu structure is saved and then choose Code -> Generate Code from the menu. Once the code generation dialog appears, to the right of the current input type will be a button named “Change”. Click this button and choose one of the following new drivers by clicking select on the right.

Choices for switches and rotary encoder

Choices for switches and rotary encoder

Controlling the menu with buttons

All switches UP/DOWN/OK are controlled by IoAbstraction’s switch library, as such they can be either PULL UP or PULL DOWN depending on the second parameter passed to switches.initialise. It’s easy to add additional switches for your own purposes too.

wiring a pull down switch to Arduino

Example wiring of a pull down switch to an Arduino

It’s quite common to connect the buttons on an i2c expander, and both PCF8574 and MCP23017 are supported for this purpose. Just wire through the interrupt pin from the IO expander to an interrupt pin on the main board.

Controlling the menu with a rotary encoder

In terms of rotary encoders this plugin can handle both direct connection to device pins and connection via an IO expander. Both PCF8574 and MCP23017 I2C IO expanders are supported and unless the wire runs are long, no pull up resistors will be required. PinA must be an interrupt capable pin, if an expander has been used, it’s interrupt pin must be connected to an interrupt capable pin on the main device.

Example of wiring a rotary encoder to an Arduino

Example of wiring a rotary encoder to an Arduino

Using more than one rotary encoder with tcMenu

You can use additional rotary encoders with tcMenu, but the menu library will always take the first one (slot 0). Read the IoAbstraction documentation, section: Advanced usage of rotary encoders.

Let’s say for example that you wanted an additional encoder that controlled a menu called ‘Volume’ with variable name menuVolume. What you’d do is configure the first encoder to control the menu as usual and it will take slot 0, then during your setup method register an additional encoder in slot 1.

// here we assume that menuVolume was defined in your menu project as an analog item

//
// declare a callback that just changes the menuVolume based on the value of the 2nd encoder.
//
void onVolChange(int newVol) {
    menuVolume.setCurrentValue(newVol);
}

//
// In setup we prepare and initialise the second encoder.
//
HardwareRotaryEncoder* volumeEncoder = new HardwareRotaryEncoder(volumePinA, volumePinB, onVolChange);
switches.setRotaryEncoder(1, volumeEncoder);
switches.changeEncoderPrecision(1, menuVolume.getMaximumValue() ,menuVolume.getCurrentValue());
    
//
// you must make sure that the additional encoder raises interrupts upon change
//
taskManager.addInterrupt(switches.getIoAbstraction(), volumePinA, CHANGE);

Controlling the menu using a Joystick

You can also control the menu using an analog joystick, choose the joystick option from the list of input devices during code generation. The joystick will be of the potentiometer type, with one end connected to Vcc and the other connected to ground. The middle pin will be connected to an analog input and that analog input should be configured in the code generator.

Back to tcMenu main page

Other pages within this category

comments powered by Disqus

We use cookies to analyse traffic and to personalise content. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Please see our privacy policy should you need more information or wish to adjust your settings.

Send a message
X

This message will be securely transmitted to Nutricherry LTD servers.