3.- App sends several information at the same time.
-
In this example we will use the same App as in the previous example.
-
Suppose we want to send two information at the same time, for example we want to turn on LED12 and turn off LED13.
-
We write in the TextBox: on12, off13 [the asterisk will be added automatically at the end of this text]
-
The Arduino code will check if the text sent contains the string: on12, off12, on13, off13
-
To check if a substring is in a text we use indexOf: if(palabra.indexOf("on12")>= 0)
-
We can also put the text in another order: off13, on12
// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm
#define Pin12 12
#define Pin13 13
char caracter;
String palabra;
void setup() {
Serial.begin(9600);
pinMode(Pin12, OUTPUT);
pinMode(Pin13, OUTPUT);
}
void loop() {
if(Serial.available()) {
caracter = Serial.read();
palabra = palabra + caracter;
if(caracter == '*') {
palabra = palabra.substring(0, palabra.length() - 1); // Delete last char *
Serial.println(palabra);
if(palabra.indexOf("on12")>= 0){digitalWrite(Pin12, HIGH);}
if(palabra.indexOf("off12")>= 0){digitalWrite(Pin12, LOW);}
if(palabra.indexOf("on13")>= 0){digitalWrite(Pin13, HIGH);}
if(palabra.indexOf("off13")>= 0){digitalWrite(Pin13, LOW);}
palabra = "";
delay(100);
}
}
}
- As we have seen in the previous section, if we send known words ("on12", "off12"...), it is not necessary to indicate the end of the message, in this case the asterisk.