Hi everyone, I'm having a problem with the program of a flow and pressure sensor communicating with a nextion display. The sensors work on the arduino's serial monitor, but the time I select to display the values on the display will stop sending them. Could someone help me with this situation ??
My problem is in the tester checksensorflow on t4 that i can not send the value of the sensor to the display it locks the reading!!
thank you
#include "Nextion.h"
long lastUpdate;
// vars
int buttonvalue = 0;
char buttonvalue_char[2];
int buttonvalue1 = 0;
char buttonvalue1_char[2];
float pressao = 0;
float valpressao = 0;
float val = 0;
float fluxo;
int contaPulso; //Variável para a quantidade de pulsos
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int led7 = 7;
NexText t2 = NexText(0, 15, "t2");
NexText t3 = NexText(0, 16, "t3");
NexText t0 = NexText(0, 18, "t0");
NexText t4 = NexText(0, 17, "t4");
NexButton b0 = NexButton(0, 9, "b0");
NexButton b1 = NexButton(0, 10, "b1");
NexPicture p7 = NexPicture(0, 8, "p7");
/*
- Register a dual state button object to the touch event list.
*/
NexTouch *nex_listen_list[] =
{
&b1,
&b0,
&p7,
NULL
};
void p7PopCallback(void *ptr)
{
uint32_t number = 0;
dbSerialPrintln("p7PopCallback");
p7.getPic(&number);
if (number == 1)
{
number = 13;
}
else
{
number = 12;
}
p7.setPic(number);
}
void b0PopCallback(void *ptr)
{
// JUST PRINT SOMETHING THAT WE KNOW THE BUTTON IS PRESSED AND THE FEEDBACK IS OK
Serial.println("botao desliga pressionado");
// INCREASE THE VALUE WE WANT TO SET AS TEXT TO THE BUTTON
buttonvalue++;
Serial.print("value: ");
Serial.println(buttonvalue);
// CREATE A CHAR OF THE INTEGER TO SEND IT TO THE BUTTON AS TEXT
itoa(buttonvalue, buttonvalue_char, 10);
// SET THE TEXT TO THE BUTTON
b0.setText(buttonvalue_char);
if (buttonvalue!=0)
{
t0.setText("OFF");
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
p7.setPic(12);
}
}
void b1PopCallback(void *ptr)
{
// JUST PRINT SOMETHING THAT WE KNOW THE BUTTON IS PRESSED AND THE FEEDBACK IS OK
Serial.println("botao liga pressionado");
// INCREASE THE VALUE WE WANT TO SET AS TEXT TO THE BUTTON
buttonvalue1++;
Serial.print("value: ");
Serial.println(buttonvalue1);
// CREATE A CHAR OF THE INTEGER TO SEND IT TO THE BUTTON AS TEXT
itoa(buttonvalue1, buttonvalue1_char, 20);
// SET THE TEXT TO THE BUTTON
b1.setText(buttonvalue1_char);
if (buttonvalue1!=0)
{
t0.setText("ON");
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
p7.setPic(13);
}
}
void checkSensor()
{
val = analogRead(pressao);
valpressao = (((val - 0.2)/ 0.0064)/1000-6);
String displayText = String(valpressao);
//Serial.println (valpressao);
t2.setText(displayText.c_str());
}
void checkSensorfluxo()
{
fluxo = digitalRead(fluxo);
//delay (1000); //Aguarda 1 segundo
cli(); //Desabilita interrupção
fluxo =( contaPulso / 7.5); //Converte para L/min
String displayText2 = String(fluxo);
Serial.print(fluxo); //Escreve no display o valor da vazão
t4.setText(displayText.c_str());
Serial.println(" L/min "); //Escreve L/min
contaPulso = 0;//Zera a variável
sei(); //Habilita interrupção
}
void incpulso ()
{
contaPulso++; //Incrementa a variável de pulsos
}
void setup(void)
{
lastUpdate = millis();
pinMode(pressao, A0);
pinMode(fluxo, 2);
nexInit();
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
p7.attachPop(p7PopCallback);
// SHOULD HAVE bauds=115200 AS PREINITIALIZE EVENT IN NEXTION EDITOR
//Serial.begin(9600);
b0.attachPop(b0PopCallback, &b0);
b1.attachPop(b1PopCallback, &b1);
Serial.println("setup done");
attachInterrupt(0, incpulso, RISING); //Configura o pino 2(Interrupção 0) interrupção
}
void loop(void)
{
nexLoop(NULL);
if (millis() - lastUpdate > 100)
{
checkSensor();
//checkSensorfluxo();
lastUpdate = millis();
}
/*
- When a pop or push event occured every time,
- the corresponding component[right page id and component id] in touch event list will be asked.
*/
nexLoop(nex_listen_list);
}