App inventor 2 en español
Cómo programar los teléfonos móviles con Android.
Bluetooth, Arduino y App inventor 2 - Juan Antonio Villalpando
-- Tutorial de iniciación de App Inventor 2 en español --
Volver al índice del tutorial
___________________________
9BA.- Bluetooth, Arduino y App Inventor. Teclado. Pantalla LCD I2C.
p9A0i_bluetooth_teclado.aia
- El Arduino genera cuatro valores aleatorios y se los envía por Bluetooth al Android.
_______________________________________________
- Conexión.
LiquidCrystal_I2C+KeyPad.zip
arduino_bt.ino |
// Juan A. Villalpando
// http://kio4.com/appinventor/9BA_bluetooth_teclado_LCD.htm
#include <Wire.h>
#include <Keypad_I2C.h>
// Pantalla LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
String clave = "";
String clave_old = "";
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};
int i2caddress = 0x20; // Module I2C Keyboard.
Keypad_I2C kpd = Keypad_I2C( makeKeymap(keys), rowPins, colPins, ROWS, COLS, i2caddress);
void setup() {
Serial.begin(9600);
kpd.begin();
lcd.begin(16,2);// Columnas y filas de LCD.
}
void loop() {
char key = kpd.getKey();
clave = clave + (String) key;
if (key == '#') {
clave = clave.substring(0, clave.length() - 1); // Delete last char #
lcd.clear();
lcd.setCursor(0,0);
lcd.print(clave);
lcd.setCursor(0,1);
lcd.print(clave_old);
clave_old = clave;
Serial.println(clave);
clave = "";
}
delay(100);
}
|
_______________________________________________
- Diseño.
_______________________________________________
- Bloques.
___________________________________________________________
- Código del hombre pobre.
- Podemos quitar la pantalla LCD, el teclado y el conversor I2C y no quedamos solo con el Bluetooth y el Arduino.
- Además el Bluetooth le quitamos el cable del terminal 0 del Arduino 0 -> RX
arduino_hombre_pobre.ino |
// Juan A. Villalpando
// http://kio4.com/appinventor/9BA_bluetooth_teclado_LCD.htm
char key = 0;
String clave = "";
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
key = Serial.read();
clave = clave + key;
if (key == '#') {
Serial.print(clave);
// Example sends: 123#
clave = "";
}
}
}
|
- Ahora vamos al Diseño de la aplicación y cambia ponemos el 35, que es el cuadradito.
- Cuando llegue el cuadradito indicará fin del mensaje.
- Conectamos y vamos al Monitor Serie.
- Escribimos el número, cuadradito y pulsamos "Enviar"
______________________________________________
- Código para obtener las direcciones I2C.
- Tanto la pantalla LCD como el teclado a través del módulo conversor 8 bit I2C, están conectado al bus I2C, por lo cual necesitamos conocer la dirección de estos dos módulos. Podemos obtener esa dirección mediante este código. Conectamos la pantalla y el teclado con su móduclo I2C y ejecutamos este código. Obtendremos sus direcciones.
obtencion_direcciones_i2c.ino |
#include <Wire.h>
void setup() {
Serial.begin (115200);
// Leonardo: wait for serial port to connect
while (!Serial)
{
}
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 1; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
} // end of setup
void loop() {}
|
_______________________________________________
__________________________________
|