display nextion leitura esta muito devagar

hello everyone I'm doing a program with the display nextion and everything was going well until for some reason the readings are getting slower and slower and the graph is passing very slowly, and when I took the other readings the graph returned to be fast , how can I have several readings without slowing down can someone help me? Thank you in advance ^^

program[]

/*

autor: Lucas Alexandre Silva

data: 18/06/2019

//=================================NEUROPAX====================================== */
// --- Bibliotecas ---
#include <Nextion.h> //biblioteca display nextion
#include <dht.h> //biblioteca sensor DHT
#include <LM35.h>

// ===============================================================================
// --- Mapeamento de Hardware ---
#define pot A5 //potenciômetro na analógica 0
#define dht_pin A1 //pino de sinal do DHT11
dht my_dht;
LM35 sensor(A2);
#define dedo A3

// ===============================================================================
// --- Declaração de Objetos ---
//page id:0, id componente:1, nome do componente: "bt0"
NexText pot_val = NexText(0, 1, "t0");
NexText txt_temp = NexText(0, 2, "t1");
NexText txt_umid = NexText(0, 3, "t2");
NexText txt_temp2 = NexText(0, 4, "t3");
NexText coracao = NexText(1, 3, "t0");

NexWaveform s0 = NexWaveform(1, 2, "s0"); // grafico batimento cardiaco

// ===============================================================================
// --- Variáveis Globais ---
int pot_value = 0; //valor ad do potenciômetro

int temperatura = 0x00, //armazena a temperatura em inteiro
umidade = 0x00; //armazena a umidade em inteiro

int temp2 = 0;

int cora = 0;

char txt1[10], //texto para conversão e exibição
txt2[10],
txt3[10],
txt4[10],
txt5[10];

// ===============================================================================
// --- Configurações Iniciais ---

void setup() {

nexInit(); //inicializa o tft

}

// ===============================================================================
// --- Loop Infinito ---
void loop() {

txt_temp2.setText(txt4);
temp2 = analogRead(A2);
//Converte inteiros para string
memset(txt4, 0, sizeof(txt4));
itoa(temp2, txt4, 10);

int leitura = analogRead (dedo);
s0.addValue (0, map (leitura, 0, 1024, 0, 255));

pot_val.setText(txt1);
pot_value = analogRead(pot);
//Converte inteiros para string
memset(txt1, 0, sizeof(txt1));
itoa(pot_value, txt1, 10);

txt_temp.setText(txt2);
txt_umid.setText(txt3);

//Lê parâmetros de temperatura e umidade relativa do ar no DHT11
my_dht.read11(dht_pin);
temperatura = my_dht.temperature;
umidade = my_dht.humidity;

//Converte inteiros para string
memset(txt2, 0, sizeof(txt2));
itoa(temperatura, txt2, 10);

memset(txt3, 0, sizeof(txt3));
itoa(umidade, txt3, 10);

}

txt_temp2.setText(txt4);
temp2 = analogRead(A2);
//Converte inteiros para string
memset(txt4, 0, sizeof(txt4));
itoa(temp2, txt4, 10);

I do not understand writing some old garbage to the nextion, and then getting the current value and converting it to a string.

If it were MY program, I'd get the current value, convert it to a string, and then send that to the nextion.

As a result, I'd recognize that I did not need 5 strings, since only one contains useful data at a time.

I also know that itoa() NULL terminates the string, so I'd know that the memset() call was useless.

Of course, I would have posted the code correctly, using code tags, and I would have used Tools + Auto Format before posting crappy looking code like that.

What data did you add that caused the slowdown? I'm going to guess that it was the temperature and humidity data. I'm also going to guess that you think that that data takes no time at all to gather.

That last assumption, on your part, if I'm correct, is completely wrong. There isn't a snowball's chance in hell that you need to read the temperature and humidity data anywhere near as often as you are.

I'm sorry if the program is bad I'm a beginner, I try to understand a project of someone and I copy the part that interests me, but I do not understand very well what you wrote, how do I solve my problem?

Hello RaquelAlexandre,
Welcome to the forum.

I'd like to help if I can however there are too many things in your post stopping me being helpful.

Obviously English isn't your first language, that's not a problem as such but if you want a response from English speakers then please post in English, use Google translate if you need to. Your mixture of English and, I think, Portuguese is difficult to follow.

I know about the Nextion but I don't use the libraries. I am happy to help if you base your project on my tutorial using Nextion displays with Arduino , which does not use Nextion libraries. If you want to use the libraries there are other people here who might help, but that's up to them.

As PaulS has commented, please post your code correctly, as indicated in 'How to use this forum - please read', you will get more helpful responses if you follow the forum rules.

thanks perry, I'll take a look at the articles that indicated ^ ^ (yes it's portugues)

To add to what Perry said, IF you are using the Nextion library, you should be aware they have some SERIOUS problems, that can cause you to miss events (touch events, timers events, etc.), and otherwise give you very unpredictable and inconsistent performance. I have corrected these problems, and posted an updated library here:

https://forum.arduino.cc/index.php?topic=620821.0

IF you are using the Nextion library, I suggest you replace it with the one posted in that thread. It will save you hours of tearing your hair out.

Or, read the thread Perry linked to, and re-write your code to use the approach he uses. Either will give you a MUCH better result than using the libraries directly from Nextion.

Regards,
Ray L.

Yes I've had many experiences with these errors, I'll test your library, thank you ray ^^

Do you understand what these 5 lines are doing?

txt_temp2.setText(txt4);
temp2 = analogRead(A2);
//Converte inteiros para string
memset(txt4, 0, sizeof(txt4));
itoa(temp2, txt4, 10);
  1. Write to the nextion
  2. Get data to write to the nextion
  3. Comment
  4. Uselessly put a NULL in every spot in the array
  5. Convert the integer value to a string with the only useful NULL in the proper place

If it were me, I'd delete the useless comment and the useless call to memset.
Of the remaining three lines (1, 2, and 5), I'd put them in order 2, 5, 1.

temp2 = analogRead(A2);
itoa(temp2, txt, 10);
txt_temp2.setText(txt);
  1. Get the current value
  2. Convert it to a string, in the same buffer used for all other int to string conversions
  3. Write current data to the nextion

But, that's just me.