Problem with myNex.readNumber in Easy Nextion Library

Hi,
I am trying to read a value from a slider on Nextion 10 inch smart screen with teensy 4.1 but the value is not always correct. I receive -1 between the correct values.
this is the related part of the code:

void trigger3(){
ruddertrim=myNex.readNumber("h0.val");
if(ruddertrim != 777777){       
  myNex.writeNum("page2.n4.val", ruddertrim);
}
}

and I have defined the variable this way:

unsigned int ruddertrim=50; 

I also tried different format of the variable but the same problem exists.
If I write the code on the screen it works perfect but I need the correct value in teensy so I wonder what is the problem.

Welcome to the forum

Please post your full sketch so that the function can be seen in context

Thanks, I guess the rest is not relevant but here it is all:



Thanks for posting the whole sketch

Maybe not, but suppose for instance, ruddertrim had been declared with an inappropriate data type ?

Do you mean that you continuously get -1 alternating with the correct value ?

Is it possible that the value is being sent from the Nextion with an extra character appended, such as Carriage Return or Linefeed ?

What do you see if you print the value received to the Serial monitor ?

Yes I continuously get -1 instead of the correct value. I mean sometimes I have correct value and sometimes -1.
I have not used Carriage Return or Linefeed, in fact I don't know what they are .
Sending to serial port I see again -1 in between the correct values.

What is generating trigger3() in the Nextion HMI code?

A touch or release event? Is there any thing else in the event other than the sending of the trigger function.

You can certainly modify the error checking from the !=777777 to include your -1 or other values out of range.

It's interesting that .readNumber() returns a uint32_t , so its not clear where the negative number (-1) is coming from.

You may also find it more reliable to use a text field instead of a number field for ruddertrim.

EDIT:
It appears that the reading of ruddertrim is the only read function in your code. Are you certain that the Serial1 wiring NextionTx to ArduinoRx is rock solid?

Hi, trigger 3 is generating by "touchmove" as I guess it should change the value if the slider is not released yet.
there is nothing else defined on the slider events.
Yes Indeed i add -1 to my if check but it seems there is still another value in between less common than -1 but it happens, that is -167.
that really confused me, minus values for uint variable. so much unreliable.
Thanks for your point i will change all my number fields to text fields. and thanks for your hint about wiring I checked seems ok but still that may be the problem so I would not use reading values anymore. I will find another way.
Thanks indeed for all your help.

Hi, trigger 3 is generating by "touchmove" as I guess it should change the value if the slider is not released yet.

This does not makes sense to me, as the value you read is displayed on another page. How do you use the dynamic update of the slider value?

I have never used the trigger functions is this fashion, and there may be an issue with the constant writing to and reading from the Arduino in such a tight loop.

If you take your HMI code and turn it into a .zip file, you can attach it to a forum post.

If you do that, I will take a look at it.

Sorry for delay i did not expect any more reply
the value is displayed on the same page.
I changed everything and omit the slider instead put two button to increase or decrease the value so no more read function is used.
Thanks

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