Help me figure out the code please

#define LCD_CS 33 // Chip Select goes to Analog 3
#define LCD_RS 15 // LCD_RS = Register Select or LCD_CD = Command/Data goes to Analog 2
#define LCD_WR 4 // LCD Write goes to Analog 1
#define LCD_RD 2 // LCD Read goes to Analog 0
#define LCD_RESET 32 // Can alternately just connect to Arduino's reset pin

//#include <SPI.h>          // f.k. for Arduino-1.5.2
//#include "Adafruit_GFX.h"// Hardware-specific library  ----------------------------------------------- 
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

String tempstr;

void setup() {
  
  Serial.begin(9600);
  Serial1.begin(9600);
    tft.reset();                 //hardware reset
    uint16_t ID = tft.readID(); //

    if (ID == 0xD3D3) ID = 0x9481; // write-only shield

    tft.begin(ID);
    tft.setRotation(3); 
    tft.fillScreen(BLUE);
    pinMode(22, INPUT); 
    digitalWrite(22, LOW);
    delay(3000); Serial1.println("AT+CM03");


}

void loop() {



  



if (Serial.available()>0) {     
    Serial1.write(Serial.read());  
  }

  if (Serial1.available()>0) {    
      //Serial.write(Serial1.read()); 
    tempstr=Serial1.readString();  
    Serial.println(tempstr);
  }
  
 
 if(digitalRead(22)==HIGH) {
  





    tft.setCursor(0, 10);
    tft.setTextColor(WHITE);  tft.setTextSize(2);
  
    tft.fillScreen(BLUE);
    tft.print(tempstr);
 
 


  
  }
    
}

A certain text arrives at the port. I write it to the tempstr variable.
When I read this variable it is printed on the tft screen with spaces. I am using the command tft.print(tempstr);
Also, when the text in the port changes, everything remains in the variable and is not overwritten.

I want the contents of the variable to be displayed on the TFT screen without spaces between the letters. I also want it to replace the previous one every time new information enters the port. That would overwrite the variable. I can't understand my mistake.

This will read the first character and print it to Serial; after that, the character is gone and readString will read the remainder of what arrives. Is the 'loss" of that first character acceptable?

What is sending the data to Serial1?

1 Like

I have corrected. I apologize, I was already so confused that I posted a piece of code instead of the complete one. When I output a variable to the port, there are no spaces, but if I output a variable to the TFT screen, there are spaces between the letters. Well, new information coming from the port is not overwritten. That is, I recorded the information, but when I update it with a command, the old one remains there.

The text from the MP3 player comes there.

As per @sterrerje's #5, your serial reading is removing characters before they get to tempstr. Anything you see echoed back from the MP3 player and into Serial/ the serial monitor didn't make it into tempstr.

Read up on:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.