2A.- App sends text. Arduino on-off LED12 and LED13. Serial Monitor. With asterisk.
p9A0i_bluetooth_texto.aia (2.6 KB)
- App sends text: on12*, off12*, on13*, off13*, I have used the astarisk to indicate end of message.
- We can also send a text, the astarisk will automatically be inserted at the end of the text.
- Check Serial Monitor.
- Arduino receives characters up to the asterisk. Example: off12*
- Delete the asterisk, last char: off12
- Turn off the LED12
// 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 == "on12"){digitalWrite(Pin12, HIGH);}
if (palabra == "off12"){digitalWrite(Pin12, LOW);}
if (palabra == "on13"){digitalWrite(Pin13, HIGH);}
if (palabra == "off13"){digitalWrite(Pin13, LOW);}
palabra = "";
delay(100);
}
}
}
oooooooooooooooo0000000000000000ooooooooooooooooooo
2B.- App sends text. Arduino on-off LED12 and LED13. Serial Monitor. Without asterisk.
p9A0i_bluetooth_texto_2.aia (2.6 KB)
- If we expect to receive known words ("on12", "off12",...), it is not necessary to use the asterisk as the end of message.
- Using palabra.indexOf(...), we can obtain if the received message contains the "known words".
// 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;
Serial.println(palabra);
if (palabra.indexOf("on12")>= 0){digitalWrite(Pin12, HIGH); palabra = "";}
if (palabra.indexOf("off12")>= 0){digitalWrite(Pin12, LOW); palabra = "";}
if (palabra.indexOf("on13")>= 0){digitalWrite(Pin13, HIGH); palabra = "";}
if (palabra.indexOf("off13")>= 0){digitalWrite(Pin13, LOW); palabra = "";}
delay(100);
}
}