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

Hi,

I want to display serial data to my 2.8" TFT, i have the data in my string "inputString" im using TFTLCD lib. and the "tft.drawString(170, 213, xxxxxx, RED, 2);" i think that i have to convert the "inputString" to some thing and then put it where the "xxxxxx" are but dont now how :frowning:
Can you masters please teach me how?

Thanks,

Paulo

i think that i have to convert the "inputString" to some thing and then put it where the "xxxxxx" are but dont now how

Maybe. Maybe not. It depends on what type inputString is. Is inputString a string or a String?

" i have the data in my string "inputString" im using TFTLCD lib. and the "tft.drawString(170, 213, xxxxxx, RED, 2);"

It can be a string, you can please post the library

PaulS:

i think that i have to convert the "inputString" to some thing and then put it where the "xxxxxx" are but dont now how

Maybe. Maybe not. It depends on what type inputString is. Is inputString a string or a String?

How can i see the differance?

void QW() {
   Wire.requestFrom(2, 200);    // request  200 bytes from slave device #2
 
  while(Wire.available())    // slave may send less than requested
   { 
    char inChar  = (char)Wire.read(); // receive a byte as character
    inputString += inChar;
    if (inputString.startsWith("12")) {
 
   if (inChar == 't') {
      stringComplete = true;
      // Serial.println(inputString); 
        // Serial.println(inputString[3]); Serial.println(inputString[4])  ; Serial.println(inputString[5] ); 
        if (inputString[2]=='3'&&inputString[3]=='4'&&inputString[4]=='5')
        tft.drawString (170, 213, "OK", RED, 2);
        
        tft.drawCircle(110, 213 , 16, BLUE);
       delay (1600); //DAR TEMPO PARA VER VALORES                      
       tft.fillRect(110, 213, 126, 16, BLACK);
       // Serial.println("1 OK"); 

      }  
   }
 }
  delay(500); 
  inputString = "";
 }

Thanks,

Paulo

NI$HANT:

" i have the data in my string "inputString" im using TFTLCD lib. and the "tft.drawString(170, 213, xxxxxx, RED, 2);"

It can be a string, you can please post the library

The lib. is this GitHub - adafruit/TFTLCD-Library: Arduino library for 8-bit TFT LCDs such as ILI9325, ILI9328, etc

Thanks,

Paulo

Posting the whole sketch would have made it obvious as it would have shown the definition, but

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

startsWith() is a String function.

How can i see the differance?

Not all that easy in that snippet. Where is inputString defined?

It must be a String, though, or

     inputString += inChar;

wouldn't work.

The String class has a toCharArray() method to get the char array that the object wraps. That char array can then be passed to the function to display text on the touch screen.

PaulS:

How can i see the differance?

Not all that easy in that snippet. Where is inputString defined?

It must be a String, though, or

     inputString += inChar;

wouldn't work.

The String class has a toCharArray() method to get the char array that the object wraps. That char array can then be passed to the function to display text on the touch screen.

I got this " String inputString = ""; ""at start in the code

Can you please write a example because i dont understand what you candly tell.

Thanks,

Paulo

Can you please write a example because i dont understand what you candly tell.

I could, but I can tell from your quick post that you did no research, so I won't.

The toCharArray() method is very easy to use.

PaulS:

Can you please write a example because i dont understand what you candly tell.

I could, but I can tell from your quick post that you did no research, so I won't.

The toCharArray() method is very easy to use.

I got this " String inputString = ""; ""at start in the code
And im researching but cant find by the key words that you write any hellp.

Thanks

Paulo

PaulS:
toCharArray() - Arduino Reference

I if i do inputString.toCharArray(buf, len) it will do?
If yes can you please hellp me on defining the buf and len?

Thanks,

Paulo

If yes can you please hellp me on defining the buf and len?

buf needs to be a char array:

char buf[n]; // where n is some number

len is the number of characters the array can hold.

You need to make buf large enough to hold all the data in inputString. Only you know how big it needs to be.

Many thanks master, it works great all credits to you Sir :slight_smile:

Thanks,

Paulo

Hi,

I have a bug in the code, im sending 2 diferent wire: Wire.write("1234567890qweyuiopt") and Wire.write("1234567890XXXXXXXXt") to simulate diderent incoming data.

MASTER:

#include <Wire.h>
 
void setup()
 {
   Wire.begin(); // join i2c bus (address optional for master)
 }
 void loop()
 {
   Wire.beginTransmission(2); // transmit to device #2
   Wire.write("1234567890qweyuiopt");
   Wire.endTransmission();    // stop transmitting
   delay(300);  
   Wire.beginTransmission(2);   
   Wire.write("1234567890XXXXXXXXt");
  Wire.endTransmission();    // stop transmitting
   delay(300); 
 }

TFT ARDUINO SLAVE:

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

String inputString = "";
boolean stringComplete = false;
byte atm = 50;
char* tok  = "";

TimedAction timedAction = TimedAction(11,QW);

char value1[5] ;
char value2[5] ; 
char value3[50] ;
void setup(void) {
  Wire.begin(2); 
  Serial.begin(9600);  // start serial for output
  }

void loop() {
 
  timedAction.check();
  }   //           FIM DE LOOP                 ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
 
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
    inputString += inChar;
    if (inputString.startsWith("12")) {
   if (inChar == 't') {
      stringComplete = true;
      if (inputString[2]=='3'&&inputString[3]=='4'&&inputString[4]=='5')
      Serial.println(inputString); 
     inputString.toCharArray(tok,atm); // THE LINE PROBLEM   ++++++++++++
     tft.drawString (1, 113, tok, RED, 2); 
      }  
   }
 }
  inputString = "";
  tok = "";  
  atm = 0;
 }

Im using here a simplifide code and Serial.println(inputString); to debug. If i tacke off this line "inputString.toCharArray(tok,atm);" that i need to displas incoming data to tft it works great i get "1234567890qweyuiopt" and "1234567890XXXXXXXXt" over and over again, with the "inputString.toCharArray(tok,atm);" it stacks on the 1st Wire.write that gets.
Please hellp me.

Thanks,
Paulo

char* tok  = "";
     inputString.toCharArray(tok,atm); // THE LINE PROBLEM   ++++++++++++

Of course it's a problem. And you've already been told why and what to do about it.

The tok variable is a pointer. It points to one byte of memory. You are lying to the toCharArray function, telling it that it is OK to extract up to 50 characters into this one element memory location. IT IS NOT.

PaulS:

char* tok  = "";

inputString.toCharArray(tok,atm); // THE LINE PROBLEM   ++++++++++++



Of course it's a problem. And you've already been told why and what to do about it.

The tok variable is a pointer. It points to one byte of memory. You are lying to the toCharArray function, telling it that it is OK to extract up to 50 characters into this one element memory location. IT IS NOT.

I change to char tok[50]; and Serial.println(inputString); but when do Serial.println(tok); for debuging i still get the same starting problem, do i need to empty char tok[50];? if yes please teache how.

Thanks

Paulo

Frankly, I can't figure out why you need the String class in the first place. Get rid of the String object. Get the data from the Wire object, and store in in the char array, with the resource wasting actions of the String class.

PaulS:
Frankly, I can't figure out why you need the String class in the first place. Get rid of the String object. Get the data from the Wire object, and store in in the char array, with the resource wasting actions of the String class.

Hi,

Im very confuse now :frowning: can you please show me a example Dear Sir ?

Thanks,

Paulo

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); 
}