RobotUI Toggle Button Example

This guide assumes you have already performed the setup and the development guide

Create a new Arduino example project

Within the Arduino IDE start a new project by using the menu File->New. Add the code shown below. Make sure to change the BT_BAUDRATE value if you are no longer using the default baudrate for your HC-06 module. This code will create a toggle button to manipulate a variable.
#include <RobotUI.h> #define BT_BAUDRATE 9600 bool thingyIsOn = false; RobotUI robotUI; RobotUIToggleButton toggleButton; void LayoutDef() { robotUI.StartLayout(); robotUI.Add(toggleButton); robotUI.EndLayout(); } void RecoverStatus() { toggleButton.SetNames("The thingy is on", "The thingy is off"); toggleButton.ForceIsPressed(thingyIsOn); } void setup() { robotUI.Init(ROBOTUI_PIN_RX, ROBOTUI_PIN_TX, BT_BAUDRATE, DEFAULT_COMMAND_BUFFER_SIZE, LayoutDef, RecoverStatus); } void loop() { unsigned long ms = robotUI.Update(); if(toggleButton.HasChanged()) { toggleButton.ConsumeHasChanged(); thingyIsOn = toggleButton.IsPressed(); } }
Below you can see the RobotUI app running when connected to the above example code.



LayoutDef function

Here we declare a toggle button to show the internal state of the arduino project. The LayoutDef function defines the layout of that single widget.
RobotUI robotUI; RobotUIToggleButton toggleButton; void LayoutDef() { robotUI.StartLayout(); robotUI.Add(toggleButton); robotUI.EndLayout(); }

RecoverStatus function

The RecoverStatus function is where the Arduino project informs the RobotUI app of its current status. The only state that this example has is "is the thingy on or off", which is the value contained in the thingyIsOn variable. When the RobotUI app connects it needs to set up the UI to reflect whatever is currently going on in the Arduino project. You can try this yourself with this example. The starting value of thingyIsOn is false, so the first time you connect to your Arduino project the text on the toggle button will be "The thingy is off". Click the button to turn it on. When you do this the text shown on the toggle button is "The thingy is on". Now forceably shutdown the app by bringing up the app switcher and swiping away the RobotUI app. This is to pretend you've wandered off for a few hours and the Android OS has dismissed the RobotUI as not being used. Open up the app again and reconnect to your Arduino project. You'll notice that the text says "The thingy is on" which is the state in which you left it. That is the purpose of the RecoverStatus function, to correctly set up the RobotUI app with the most recent internal state, as the app may not have been connected for a very long time. RecoverStatus needs to set the on and off names of the toggle button object using the SetNames function. The first parameter is the on name, the second is the off name. It also needs to set the toggle button object status with the ForceIsPressed function. We need to use ForceIsPressed instead of SetIsPressed as the latter avoids sending unnecessary state changes if the RobotUI app and Arduino project are already in the correct state. As this is a recovery operation the RobotUI app and Arduino project do not know each others states, so the Arduino project needs to force the RobotUI app.
void RecoverStatus() { toggleButton.SetNames("The thingy is on", "The thingy is off"); toggleButton.ForceIsPressed(thingyIsOn); }

The main loop

The loop function is where we monitor the toggle button object and record changes to the thingyIsOn variable. First we call robotUI.Update() every frame so it can manage communications with the app. Then we check if the toggle button object has been changed by the user of the RobotUI app. If it has then toggleButton.HasChanged() will return true. It will continue to return true until you call toggleButton.ConsumeHasChanged() to consume that last event. If toggleButton.HasChanged() returns true then the contents of the if statement will be executed. So the first thing we do is to consume the toggle button event with toggleButton.ConsumeHasChanged(). We then record the new value of the toggle button by using thingyIsOn = toggleButton.IsPressed(). We need to record thingyIsOn so we can remember and recover the correct UI when the RobotUI app reconnects to the Arduino project after an absence.
void loop() { unsigned long ms = robotUI.Update(); if(toggleButton.HasChanged()) { toggleButton.ConsumeHasChanged(); thingyIsOn = toggleButton.IsPressed(); } }

Other Examples

Here are some other examples for various widgets

button example
check box example
seek bar example
spinner example
switch example
text example
toggle button example