But I have done a little differently:
void loop()
{
nexLoop(nex_listen_list);
if (t == 10000)
{
weight = ...
t = 0;
}
t++;
Unfortunately, this is highly dependent on execution time (variable), not a timer (consistent). nexLoop or other code can block for different amounts of time while waiting for a reply, so you should use a timer like millis(), not a counter like t. What if you or someone reading this puts this code on an Arduino with a different clock speed?
I have already explained the general approach and given an example. If you need more information or examples, please see Useful Links, "blink without delay" and "how to do several things at a time".
Your simple program probably has a problem in this code:
void b0PopCallback(void *ptr)
{
...
NexButton *btn = (NexButton *)ptr;
...
btn->getText(buffer, sizeof(buffer));
...
btn->setText(buffer);
}
After looking at the code, I'm not sure the Nextion library can get and set the label of b0 during b0's callback. The Nextion library performs many blocking and flushing operations that could interfere with each other and the timing. ![]()
I fairly confident tha, during the callback, it's ok to call non-Nextion code or set a variable that is used elsewhere:
bool b0Pressed = false;
void b0PopCallback(void *)
{
b0Pressed = true; // set variables...
digitalWrite( LEDpin, HIGH ); // call non-Nextion code that is quick
}
void loop()
{
nexLoop(...);
if (b0Pressed) {
b0pressed = false;
b0.setText("foo"); // Nextion call from loop, not from inside the callback
}
That should be safe.
And using delay is bad. Quit using delay. If you want to delay some action, use the techniques we have already described. nexLoop must be called continuously:
uint32_t lastT0set;
uint8_t t0Value;
void loop(void)
{
nexLoop(nex_listen_list);
if (millis() - lastT0set > 2000UL) {
lastT0set = millis();
itoa(t0Value, buffer, 10);
t0.setText( buffer );
t0Value++;
}
}
This calls nexLoop over and over, until it's time to display a new t0 text and increment the value. No delay. This guarantees that nexLoop handles the received characters quickly.
Serial can not operate in full duplex, or I'm doing something wrong.
Serial can operate in full duplex, so you must be doing something wrong. I think you have to be careful with the Nextion library to avoid
(1) nested calls (calling setText inside a callback) and
(2) blocking operations (like delay and software serial prints).
And I don't see how this code could ever work:
void loop()
{
nexLoop(nex_listen_list);
Serial.println(readTemperature(SensorWater));
switch(page)
{
case 0:
if (pump_state && pump_state_show)
{
pump_state_show = 0;
bt00.setValue(pump_state);
}
break;
Do you set page=-1 at the end of loop? If not, you will set bt00 every time.
You can't read the temperature (1 second) and print the temperature (5ms) every time.
My previous reply had the correct structure. You didn't like it? ![]()
I haven't mentioned it so far, but Nextion shouldn't be using the String class. That's a real no-no in this environment, and it could be a contributing factor. I would eliminate String from the library if I were you. It would also save 1600 bytes of program space. Ugh.
Cheers,
/dev