How to "print" serial data to 2.8" TFT ?

PaulS: You know that you are sending 50 or fewer bytes. So, try something like this:

char buffer[50];
byte index;

void receiveEvent(int howMany) {   index = 0;   while(Wire.available())    // slave may send less than requested   {      char inChar  = (char)Wire.read(); // receive a byte as character      buffer[index++] = inChar; // Store the character and advance the pointer      buffer[index] = '\0'; // NULL terminate the string   ]

  // All the received data is now in buffer. Send it to the TFT   tft.drawString (1, 113, buffer, RED, 2); }

Hi,

Thanks 1st for all you sport :) , i used your way off doing and works good but i need to have a starting char a ending char ike "if (inputString.startsWith("12")) { if (inChar == 't') stringComplete = true;..." i will get a diferent number of bytes can get 14 or 20...sow i need to now whwn it finishedand finaly empty the char buffer[50]; because diferent data will came on. The general idea is receiving data display it on tft in 2 ways: the string printed in on tft and as bargraf. Sow i need to read, display and do things with the fiferent Wire.read

Thanks

Paulo

if (inputString.startsWith("12")) {

You are collecting the data in an array. Arrays don't have members like startsWith. You should look at strncmp(), though.

and finaly empty the char buffer[50]; because diferent data will came on.

You need to read up on C strings. It is not necessary to "empty the buffer". A string is a NULL terminated array of characters. So, simply put a NULL in position 0, and the "buffer is empty"

index  = 0;
buffer[index] = '\0';

PaulS:

if (inputString.startsWith("12")) {

You are collecting the data in an array. Arrays don't have members like startsWith. You should look at strncmp(), though.

and finaly empty the char buffer[50]; because diferent data will came on.

You need to read up on C strings. It is not necessary to "empty the buffer". A string is a NULL terminated array of characters. So, simply put a NULL in position 0, and the "buffer is empty"

index  = 0;
buffer[index] = '\0';

Hi,

I have a problem, if i dont send same amount of bytes it prints the data all messed up , can i do a dynamic one i say if gets 5 bytes print then i get 12 print and do not wait for the full 19 bytes to clear and start from zero again here is part of the code

void QW(){

 Wire.onReceive(receiveEvent); // register event
 }
 void receiveEvent(int howMany)
 {
   
   while(Wire.available())    // slave may send less than requested
   { 
    char inChar  = (char)Wire.read(); // receive a byte as character
      buffer[index++] = inChar; // Store the character and advance the pointer
      
      buffer[index] = '\0'; // NULL terminate the string
      
     tft.drawString (1, 178, buffer, RED, 2);
     delay(5000);
     tft.fillRect(1, 178, 230, 20, BLACK);
    
     }
   
   }

Thanks, God Easter :)

Paulo

I have a problem, if i dont send same amount of bytes it prints the data all messed up , can i do a dynamic one i say if gets 5 bytes print then i get 12 print and do not wait for the full 19 bytes to clear and start from zero again here is part of the code

You can do anything you want EXCEPT post snippets of code. Nothing in that code sets index initially, or sets buffer[0] to NULL to reset everything. Perhaps that is not done anywhere, which could be the root of your problem. Or, perhaps it is done somewhere, but that is not the right place.

Writing the string to the screen after every character addition seems silly.

Hi,

Sory about the snippet here is “my” starting code:

#include <Wire.h>
#include "TFTLCD.h"
#include "TouchScreen.h"
#include <EEPROM.h>
#include <TimedAction.h>
TimedAction timedAction = TimedAction(1000,QW);
TimedAction prim = TimedAction(10000,ew);

#define YP A0  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM A2   // can be a digital pin
#define XP A1   // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940


char value1[5] ;
char value2[5] ; 

char buffer[19];
byte index;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CD 13  // RS
#define LCD_WR 12
#define LCD_CS 11
#define LCD_RD 10 
#define LCD_RESET 1
// Color definitions - in 5:6:5
#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 TEST            0x1BF5
#define JJCOLOR         0x1CB6
#define JJORNG          0xFD03

TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup(void) {
 prim.enable( );
 Wire.begin(2); 
  Serial.begin(9600);  
  tft.setRotation(1);
  tft.reset();
  tft.initDisplay();
  tft.fillScreen(BLACK);
  tft.drawString(1, 1, "CAMOLAS 12:30H 23.01.12", WHITE,2); 
  
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000

// ***********************************************************************************************************************************************


void loop() {
  
  timedAction.check();
 prim.check();
   
  
  Point p = ts.getPoint();
  // if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
   
    // turn from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);
    itoa (p.x, value1, 10);
    itoa (p.y, value2, 10);
    
    tft.drawString(110, 213,+value1, WHITE, 2);
    tft.drawString(170, 213, +value2, WHITE, 2);
    delay (600); //DAR TEMPO PARA VER VALORES                      
       tft.fillRect(110, 213, 126, 16, BLACK);
       
       
    // Serial.print("p.y:"); // this code will help you get the y and x numbers for the touchscreen
    // Serial.print(p.y);
    // Serial.print("   p.x:");
    // Serial.println(p.x);
  }   
    

void QW(){     
 
 Wire.onReceive(receiveEvent); // register event
 }
 void receiveEvent(int howMany)
 {
   
   while(Wire.available())    // slave may send less than requested
   { 
    char inChar  = (char)Wire.read(); // receive a byte as character
      buffer[index++] = inChar; // Store the character and advance the pointer
      buffer[index] = '\0'; // NULL terminate the string
     }
   }
   
void ew()
{
tft.fillRect(1, 175, 201, 20, BLACK);
tft.drawString (1, 175, buffer, RED, 2);
 }

Writing the string to the screen after every character addition seems silly.

Í want to update data in ever 10000

Thanks

Paulo

Calling QW, once a second to register the callback when the Wire class receives data is a waste. Register the even ONCE in setup, and get rid of QW and the TimedAction that calls it.

receiveEvent() is called when there is data to read. You should initialize the array and index when the function is called. You should not return until you have read howMany characters/bytes.

What is ew()? Function names like digital;Read() or analogWrite() say exactly what the function is going to do. ew() doesn't say any such thing.

Hi,

I found a way ( with the hellp of master PaulS) solve my problem i now that me be not the best way to do it but it works for me :slight_smile: i want to move forward and learn other things in this new arduino world for me.

#include <Wire.h>
#include "TFTLCD.h"
#include "TouchScreen.h"
#include <EEPROM.h>
#include <TimedAction.h>

TimedAction timedAction = TimedAction(1000,QW);
// TimedAction prim = TimedAction(1500,ew);

boolean stringComplete = false;

#define YP A0  // must be an analog pin, use "An" notation!
#define XM A3  // must be an analog pin, use "An" notation!
#define YM A2   // can be a digital pin
#define XP A1   // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000

char value1[5] ;
char value2[5] ; 
byte inByte;
String inputString = "";

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CD 13  // RS
#define LCD_WR 12
#define LCD_CS 11
#define LCD_RD 10 
#define LCD_RESET A5

#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 TEST            0x1BF5
#define JJCOLOR         0x1CB6
#define JJORNG          0xFD03

TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

void setup(void) {
  // prim.enable( );
  Serial.begin(9600);
  Serial.write("TFT");
  tft.setRotation(1);
  tft.reset();
  tft.initDisplay();
  tft.fillScreen(BLACK);
  tft.drawString(1, 1, "CAMOLAS 12:30H 23.01.12", WHITE,2+0.8);
  tft.fillRect(100, 40, 65, 50, JJCOLOR);
  tft.drawString(105, 54, "CAM", WHITE,3);
  }
  
 void loop() {
  timedAction.check();
  // prim.check();
  Point p = ts.getPoint();
  // if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
   
    // turn from 0->1023 to tft.width
  p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
  p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);
  itoa (p.x, value1, 10);
  itoa (p.y, value2, 10);
    
  tft.drawString(110, 213,+value1, WHITE, 2);
  tft.drawString(170, 213, +value2, WHITE, 2);
  delay (600); //DAR TEMPO PARA VER VALORES                      
  tft.fillRect(110, 213, 126, 16, BLACK);
  
  if (p.y >87  && p.y < 255 && p.x > 70 && p.x <100 ) { 
    limpa();  
  }    
  }

void QW(){
  while (Serial.available() > 0) {
  char inByte = Serial.read();
  inputString += inByte;
  if (inputString.startsWith("12")) {
  if (inByte == 't') {
  stringComplete = true;
  
  if (inputString[2]=='3'&&inputString[3]=='4'&&inputString[4]=='5'&&inputString[5]=='6'&&inputString[6]=='7')
  ew();
  if (inputString[2]=='P'&&inputString[3]=='A'&&inputString[4]=='U'&&inputString[5]=='7'&&inputString[6]=='8')
  DD();
  
   }
   }
   }
   }
  
void ew()
  {
  tft.setCursor(0, 190); 
  tft.setTextSize(2);
  tft.fillRect(0, 190, 226, 16, BLACK);
  tft.println(inputString);
  inputString = "";
  }
  
  void DD()
  {
   tft.setCursor(0, 100); 
   tft.setTextSize(2);
    tft.fillRect(0, 98, 205, 18, RED);
  tft.println(inputString);
  inputString = "";
  }

void limpa(){
tft.fillScreen(BLACK);  
tft.drawString(1, 1, "CAMOLAS 12:30H 23.01.12", WHITE,2+0.8);
tft.fillRect(100, 40, 65, 50, JJCOLOR);
tft.drawString(105, 54, "CAM", WHITE,3);
}

Thanks,

Paulo

Hi,

I want display bmp images but i dont have free ports to conect a sd card can i put the sample code in the sketch and display it? if yes how?

unsigned char car [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x10, 0x00,
0x10, 0x08, 0x80, 0x38, 0x00, 0x2C, 0x00, 0x04, 0x1C, 0x20, 0x14, 0xA8, 0x12, 0x24, 0x0A, 0x34,
0x0A, 0x24, 0x1A, 0xE4, 0x1A, 0xE4, 0x0A, 0x14, 0x0A, 0x14, 0x0A, 0x24, 0x1A, 0x24, 0xA2, 0x0C,
0x02, 0x14, 0x0C, 0x00, 0x0C, 0x00, 0x04, 0x08, 0x10, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x30,
0x06, 0x20, 0x80, 0x20, 0x00, 0x28, 0x9B, 0x20, 0x10, 0x68, 0x20, 0xC8, 0x20, 0x48, 0x70, 0x05,
0xDA, 0x20, 0x20, 0x40, 0x20, 0x50, 0x2A, 0x44, 0x1B, 0xA8, 0x10, 0x28, 0x50, 0x08, 0xA8, 0x10,
0xA0, 0x10, 0xAB, 0x10, 0x28, 0x50, 0xA0, 0x10, 0x20, 0xA0, 0x00, 0x20, 0x20, 0x40, 0x30, 0x05,
0x70, 0x20, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xF0, 0xAD, 0x90, 0x62, 0x08, 0xF2, 0x00, 0xF6, 0x00, 0xF4, 0x02, 0x78, 0x84, 0x69, 0x14, 0xE8,
0x10, 0xC9, 0x33, 0x48, 0x92, 0x64, 0x89, 0x30, 0xC1, 0x24, 0x49, 0xD0, 0x01, 0xD4, 0x00, 0xD1,
0x02, 0x50, 0x82, 0x28, 0x00, 0xC2, 0x88, 0x20, 0x41, 0xA0, 0x4A, 0x80, 0x50, 0x08, 0xC0, 0x11,
0x24, 0xC0, 0x2A, 0x80, 0x20, 0xCC, 0x10, 0x40, 0x94, 0x20, 0x08, 0xE0, 0x08, 0x20, 0xC8, 0x20,
0x08, 0x60, 0x08, 0x60, 0x88, 0x40, 0xD0, 0x20, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x33, 0x4C, 0x93, 0xFE, 0x20, 0xED, 0xD2, 0x0D, 0xF2, 0x04, 0xDB, 0x24, 0xDB,
0x00, 0xFF, 0x00, 0xB7, 0x48, 0xB6, 0x09, 0xF2, 0x6D, 0xD0, 0x3F, 0xD8, 0xB2, 0xED, 0x22, 0xD9,
0x06, 0xF0, 0x0F, 0xD0, 0x2D, 0xD2, 0x2D, 0xD2, 0x2D, 0xD2, 0x21, 0xD2, 0x21, 0x1C, 0xC3, 0x10,
0xF5, 0x08, 0xF4, 0x05, 0xD8, 0x24, 0xDA, 0x24, 0xD4, 0x2A, 0xD4, 0x2A, 0x54, 0xAA, 0x56, 0xA8,
0x56, 0xAA, 0x54, 0xAA, 0x55, 0xAA, 0x56, 0xA9, 0x16, 0xEA, 0x94, 0x2C, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x03, 0x04, 0x03, 0x04, 0x0B, 0x14, 0x3B, 0xEC, 0xB7, 0x4B, 0xBD, 0x42,
0xBD, 0x42, 0xBD, 0x53, 0xAE, 0xF8, 0x83, 0x3C, 0x03, 0xBC, 0x03, 0x1C, 0x23, 0x9C, 0x0B, 0x34,
0x0B, 0x14, 0xAB, 0x14, 0x8A, 0x11, 0x8E, 0x91, 0x8E, 0x90, 0x0B, 0xD4, 0x0B, 0xC0, 0x4F, 0x48,
0x43, 0xCC, 0x43, 0x84, 0x6D, 0x82, 0x65, 0xA2, 0x45, 0x32, 0xC5, 0x72, 0xD1, 0x32, 0x11, 0x00,
0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07,
0x1A, 0x35, 0x2A, 0x35, 0x6A, 0xB5, 0x6A, 0xD5, 0x7E, 0xDB, 0x74, 0x2F, 0x3B, 0x0E, 0x03, 0x07,
0x01, 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x04, 0x03, 0x04,
0x03, 0x00, 0x05, 0x03, 0x00, 0x02, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

Many thanks,

Paulo

Any hellp please.

Thanks,

Paulo