RobotUI Check Box 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 text box and a check box to manipulate the contents of text box.
#include <RobotUI.h> #define BT_BAUDRATE 9600 bool thingyIsOn = false; RobotUI robotUI; RobotUICheckBox checkBox; RobotUIText textBox; void LayoutDef() { robotUI.StartLayout(); robotUI.Add(textBox, 1, DEFAULT_TEXT_SIZE, JUSTIFY_CENTER); robotUI.Add(checkBox); robotUI.EndLayout(); } void RecoverStatus() { checkBox.SetName("Thingy status"); if(thingyIsOn) { textBox.SetText("The thingy is on"); checkBox.ForceIsPressed(true); } else { textBox.SetText("The thingy is off"); checkBox.ForceIsPressed(false); } } 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(checkBox.HasChanged()) { checkBox.ConsumeHasChanged(); if(checkBox.IsPressed()) { textBox.SetText("The thingy is on"); thingyIsOn = true; } else { textBox.SetText("The thingy is off"); thingyIsOn = false; } } }
Below you can see the RobotUI app running when connected to the above example code.



LayoutDef function

Here we declare a checkbox and a text box to show the internal state of the arduino project. The LayoutDef function defines the layout of those two widgets, simply adding them one after the other so they appear vertically. The default justification of the text box is JUSTIFY_LEFT so I specified JUSTIFY_CENTER to improve the visual appeal of the layout.
RobotUI robotUI; RobotUICheckBox checkBox; RobotUIText textBox; void LayoutDef() { robotUI.StartLayout(); robotUI.Add(textBox, 1, DEFAULT_TEXT_SIZE, JUSTIFY_CENTER); robotUI.Add(checkBox); 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 being displayed will be "The thingy is off". Check the check box to turn it on. When you do this the text shown 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 contents of the textBox object with SetText. It also needs to check or uncheck the checkBox object 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() { checkBox.SetName("Thingy status"); if(thingyIsOn) { textBox.SetText("The thingy is on"); checkBox.ForceIsPressed(true); } else { textBox.SetText("The thingy is off"); checkBox.ForceIsPressed(false); } }

The main loop

The loop function is where we monitor the checkbox object and change the contents of the textBox object. First we call robotUI.Update() every frame so it can manage communications with the app. Then we check if the checkBox object has been changed by the user of the RobotUI app. If it has then checkBox.HasChanged() will return true. It will continue to return true until you call checkBox.ConsumeHasChanged() to consume that last event. If checkBox.HasChanged() returns true then the contents of the if statement will be executed. So the first thing we do is to consume the checkBox event with checkBox.ConsumeHasChanged(). We then determine the new value of the checkBox by using checkBox.IsPressed(). If it is true then we change the textBox contents with a call to textBox.SetText("The thingy is on") and change the status of the thingyIsOn variable to true. If it was false then we change the textBox contents with a call to textBox.SetText("The thingy is off") and change the status of the thingyIsOn variable to false. 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(checkBox.HasChanged()) { checkBox.ConsumeHasChanged(); if(checkBox.IsPressed()) { textBox.SetText("The thingy is on"); thingyIsOn = true; } else { textBox.SetText("The thingy is off"); thingyIsOn = false; } } }

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