Together, the interface panel visualizer and our Arduino command handler provide a simple method to update variables in your Arduino sketch. Register the variable and its name with a command handler in your Arduino sketch then send !VariableName NewValue\r\n from MegunoLink to update the variable in your Arduino sketch.

For more complex tasks than simply updating a variable value, register a command name and Arduino function with the command handler. Then your function will be called whenever the command is received. The function could turn on a pin, move a servo, or update several variables. See getting started processing serial commands with an Arduino for more information.

Set configuration variables

Set configuration from an interface panel visualizer. Commands sent are in orange while the Arduino’s response is in black in the monitor visualizer.

You will need:

  1. a CommandHandler in your Arduino sketch,
  2. a variable in your Arduino sketch, and
  3. an interface panel visualizer with controls to enter the variable’s value and send the update command.

Getting Started Building Arduino Interfaces

New to MegunoLink? Check out guide to building an Arduino user interface panel and processing serial commands in your Arduino sketch.

Add a command handler

To create a command handler:

  1. Add #include "CommandHandler.h" to the top of your Arduino sketch.
  2. Create a CommandHandler variable: CommandHandler<> Commands.
  3. Call Commands.Process(); in your loop() function.

Arduino Library

You’ll need to install our Arduino Library for MegunoLink to use the CommandHandler.

By default, our command handler supports up to 10 variables. Refer to the Arduino command handler reference if you need more (or fewer) variables.

Register the variable

Register the variable in your Arduino sketch with the command handler in the setup() function: Commands.AddVariable(F("VariableName"), Variable);. Substitute VariableName for the name of your variable and Variable with the variable itself. Most variable types are supported. VariableName must not contain any spaces.

Variable must still be in memory when a command is received to change its value. Variables declared at the top of your sketch (also known as global variables), outside any function, are okay. But variables declared inside functions (local variables) generally won’t work, instead your program will crash in interesting and confusing ways!

Correct use: variable BlinkTime is global.

Wrong: variable BlinkTime is local to setup() function.

Add controls to your interface panel

Generally you need an input control for the user to enter the variable’s value and a button to send the value to your Arduino sketch. Controls for entering or selecting values include:

Each control provides a different user experience, so select the one which provides the user experience you’d like to create.

Set up the interface panel to set the variable in your Arduino sketch by:

  1. Adding the input control of your choice and a button to your interface panel by dragging them from the toolbox onto the design surface (see getting started building an Arduino interface for an introduction to creating interface panels).
  2. Select the new input control and set its name in the property editor. You can choose any name, but using the same VariableName will help keep everything organized.
  3. Select the new button control and set its OnClickSend property to !InputControlName [InputControlName.Value]\r\n. See creating serial commands for details.
  4. Apply the design changes to your interface panel and test with your Arduino sketch.
User interface for sending configuration variables

Set the name of the input-control and the OnClickSend property of the button control to send configuration values to variables in your Arduino sketch.

Input controls can also send a message when the user changes the value. For example, the numeric up/down control’s OnValueChangedSend sets a message to send when the number in the control is changed. These properties update your Arduino sketch immediately. Sending messages when a button is clicked gives the user more control over changes. Pick whichever method suits your application.

Leave a Comment

Start typing and press Enter to search