10.- Three LEDS on-off according to a sequence saved in a file.
p9A0i_bluetooth_secuencia3LED.aia (5.0 KB)
-
In file rutina2.csv this content:
-
Levels of: LED11 - LED12 - LED13 - Time in seconds of this combination.
-
App sends 1-1-0-2*
-
Arduino set LEDs on, on, off
-
and wait 2 seconds.
-
then Arduino sends to App “send_me_next”
-
When App receives “send_me_next”
-
App “send_next” combination: 1-0-0-2*
// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm
char caracter;
String palabra;
int LED11 = 11;
int LED12 = 12;
int LED13 = 13;
int valor11;
int valor12;
int valor13;
int Tiempo = 1000000;
unsigned long currentMillis;
unsigned long previousMillis = currentMillis;
int k1;
int k2;
int k3;
int k4;
void setup() {
Serial.begin(9600);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
}
void loop() {
if(Serial.available()) {
caracter = Serial.read();
palabra = palabra + caracter;
Serial.println(palabra);
if(caracter == '*') {
palabra = palabra.substring(0, palabra.length() - 1); // Delete last char *
k1 = palabra.indexOf('-');
valor11 = palabra.substring(0, k1).toInt();
k2 = palabra.indexOf('-', k1+1);
valor12 = palabra.substring(k1+1, k2).toInt();
k3 = palabra.indexOf('-', k2+1);
valor13 = palabra.substring(k2+1, k3).toInt();
k4 = palabra.indexOf('-', k3+1);
Tiempo = 1000 * palabra.substring(k3+1, k4).toInt();
palabra = "";
digitalWrite(LED11, valor11);
digitalWrite(LED12, valor12);
digitalWrite(LED13, valor13);
}
previousMillis = currentMillis;
} // =>Fin del available
tiempo();
} // =>Fin del loop
void tiempo() {
currentMillis = millis();
if (currentMillis >= (previousMillis + Tiempo)) {
previousMillis = currentMillis;
Serial.println("send_me_next"); // Envíame el siguiente.
}
}