Hola!
La idea de este pequeño proyecto es leer un string desde el pin Rx de un arduino y mostrarlo en el TFT junto con un botón que en este caso solo enciende un led interno, pero no estaría funcionando. Al habilitar la lectura en serie la pantalla se pone con el color de fondo y no muestra ningún botón. Si no habilito la lectura escritura en serie la pantalla funciona perfectamente.
Mi hardware es Arduino Uno V3 con un display TFT LCD shield mcufriend y un esp8266 nodemcu que envía datos en serie al Arduino.
Detalles del circuito y programación:
El lamentable TFT LCD shield mcufriend ocupa casi todos los pines del Arduino y solo quedan libres los pines 0 (Rx) , 1 (Tx) , 18 y 19. Los pines 0 (Rx) y 1 (Tx) no se pueden usar por que se entra en conflicto con en puerto USB así que usando la librería SoftwareSerial.h convertí los otros 2 pines libres, los 18 y 19 en los nuevos Rx y Tx.
Lo mismo pasa con el Esp8266 donde los pines marcados como Rx y Tx no se pueden usar para no entrar en conflicto con el puerto Usb. Una vez más mediante SoftwareSerial.h convertí los pines D1 y D2 en Rx y Tx respectivamente.
El circuito es simple:
puse en conector desde el pin 18 Arduino hasta el pin D2 del Esp8266, aquí la diferencia de voltaje no importa.
Otro conector desde el pin 19 el hacia un divisor resistivo con salida el pin D1 del Esp8266, el divisor es porque aquí la diferencia de tensión importa.
Hice pruebas con unos sketchs simples para ver si el circuito funciona y si el arduino y el esp8266 se leen uno al otro y funcionó de maravillas. Si hacen falta subo estos 2 sketchs de prueba también.
El sketch que falló:
Diseñé un botón para usar con la pantalla táctil que aparece en el centro de la pantalla y cambia de color cuando se lo presiona a la vez que enciende un led interno del arduino. Encima de este botó aparece un recuadro donde se lee un string fijo que dice Hola, la idea es cambiar este string por uno leído desde el Rx. Cuando habilito la función Mensaje() que es la va a leer el string desde Rx no funciona mySerial.available() siempre es igual a cero y entonces nunca lee a RX.
Cuando modifico la función y queda como Mensaje_2() Rx se puede leer y se puede transmitir por Tx pero el display se pone todo gris.
Pregunta ¿Como puedo hacer para leer rx y escribir su contenido en el TFT con este hardware?
Gracias desde ya!
#if 1
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
#define MINPRESSURE 10 //200
#define MAXPRESSURE 1000
#define LED_PIN 13
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
//const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int XP=7,XM=A1,YP=A2,YM=6; //240x320 ID=0x1602
//const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;
const int TS_LEFT=172,TS_RT=914,TS_TOP=92,TS_BOT=900;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
///DECLARAR EL BOTON
Adafruit_GFX_Button onoff_btn, off_btn;
bool botononoff = false;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
//#define GRAY 0xD3D3D3
#define GRAY 0xFF5733
SoftwareSerial mySerial = SoftwareSerial(18, 19);
uint16_t ID = 0;
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
void Mensaje()
{
if (mySerial.isListening())
{
if (mySerial.available())
{
Serial.println("mySerial is listening!");
Serial.println(mySerial.readString());
tft.print(mySerial.readString());
mySerial.println("Hola soy Arduino UNO");
//delay(1200);
}else{
Serial.println("Not available");
if (mySerial.overflow())
{
Serial.println("mySerial overflow!");
}
}
}else{Serial.println("Not listening");}
}
void Mensaje_2()
{
if (mySerial.isListening())
{
Serial.println("mySerial is listening!");
Serial.println(mySerial.readString());
tft.print(mySerial.readString());
mySerial.println("Hola soy Arduino UNO");
//delay(1200);
}
}
void setup()
{
ID = tft.readID();
//Serial.print("TFT ID = 0x");
//Serial.println(ID, HEX);
//Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(0xe9f5f9);
//tft.setFont(&FreeSans12pt7b);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
onoff_btn.initButton(&tft, 120, 170, 100, 40, GRAY, WHITE, BLACK, "ON/OFF", 2);
//off_btn.initButton(&tft, 180, 200, 100, 40, GRAY, RED, WHITE, "OFF", 2);
onoff_btn.drawButton(false);
//off_btn.drawButton(false);
tft.fillRect(40, 30, 160, 80, WHITE);
// tft.setCursor(50, 55);
// tft.setTextColor(BLUE);
// tft.setTextSize(4);
// tft.println("-27.5");
//tft.fillRect(40, 30, 160, 80, WHITE);
pinMode(18, INPUT);
pinMode(19, OUTPUT);
mySerial.begin(9600);
Serial.begin(9600);
}
/* two buttons are quite simple
*/
void loop()
{
//Mensaje();
Mensaje_2();
tft.setFont(&FreeSans12pt7b);
tft.setCursor(60, 85);
tft.setTextColor(BLUE);
tft.setTextSize(2);
//tft.print("Hola");
tft.setFont();
bool down = Touch_getXY();
onoff_btn.initButton(&tft, 120, 170, 100, 40, GRAY, WHITE, BLACK, "ON/OFF", 2);
onoff_btn.press(down && onoff_btn.contains(pixel_x, pixel_y));
if (onoff_btn.justPressed())
{
botononoff = !botononoff;
if (botononoff)
{
onoff_btn.initButton(&tft, 120, 170, 100, 40, GRAY, GREEN, BLACK, "ON", 2);
onoff_btn.drawButton();
digitalWrite(LED_PIN, botononoff);
}else{
onoff_btn.initButton(&tft, 120, 170, 100, 40, GRAY, RED, WHITE, "OFF", 2);
onoff_btn.drawButton();
digitalWrite(LED_PIN, botononoff);
}
}
delay(150);
}
#endif