Welcome back to the RoboRemo tutorial.
In this tutorial I explain how to use the interface's connect action. It is basically a string that RoboRemo will send only once, immediately after connecting, so that the remote device (Arduino) gets notified when a connection is made. One use case is to restore the interface state: the Arduino receives the connect action string and replies with the commands that update the interface items state in RoboRemo.
For the demo I use the project from previous tutorial, with the RGB LED contorlled by 3 sliders. I improve that project so that when I connect with RoboRemo, the 3 sliders position is automatically updated to reflect the actual state of the LED color.
// RoboRemo Tutorial 7 - Connect action - update slider position
// (RGB LED used for demo)
// Interface's connect action - when connected, app sends this string (one time)
// www.roboremo.com
//
// Copyright 2020 roboremo.com
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// The above copyright notice and disclaimer shall be included
// in all copies or substantial portions of the Software.
// Hardware setup:
// NOTE: My LED has COMMON ANODE (+)
// RGB LED Arduino
// ANODE -------- 5V
// red ----|__|-- D9
// green --|__|-- D10
// blue ---|__|-- D11
// 3 x 100R
// Bluetooth Arduino
// GND ---------- GND
// VCC ---------- 5V
// TX-O --------- RX0
// RX-I --------- TX1
#define BAUDRATE 115200
#define NUM_COLORS 3
// sliders IDs in RoboRemo
char id[NUM_COLORS] = {'r', 'g', 'b'};
// R,G,B (LOW = ON, HIGH = OFF)
int pin[NUM_COLORS] = {9, 10, 11};
int brightness[NUM_COLORS] = {0, 0, 0};
char cmd[100];
int cmdIndex;
void execute(char* cmd) {
if(strcmp(cmd, "get")==0) {
// for each of 3 colors R,G,B
for(int i=0; i<NUM_COLORS; i++) {
Serial.print(id[i]); // id 'r','g','b'
Serial.print(" ");
Serial.print(brightness[i]);
Serial.print("\n");
}
return;
}
// cmd example:
// "r 107" => Slider id = "r", value = 107
if(cmd[1] != ' ') return; // unknown command
// for each of 3 IDs 'r','g','b'
for(int i=0; i<NUM_COLORS; i++) {
if(cmd[0] == id[i]) {
brightness[i] = atoi(cmd+2);
// 255-brightness because LED is COMMON ANODE
analogWrite(pin[i], 255-brightness[i]);
}
}
}
void setup() {
// for each of 3 pins R,G,B
for(int i=0; i<NUM_COLORS; i++) {
pinMode(pin[i], OUTPUT);
digitalWrite(pin[i], HIGH); // OFF
}
cmdIndex = 0;
delay(500);
Serial.begin(115200);
}
void loop() {
if(Serial.available()) {
char c = (char)Serial.read();
if(c=='\r' || c=='\n') {
cmd[cmdIndex] = 0;
cmdIndex = 0;
execute(cmd);
} else {
cmd[cmdIndex++] = c;
}
}
}