Closed s56jsc closed 1 year ago
Have you declare the variable as uint32_t on your code? try to use standard prefix for Nextion attributes (page name, object name) like n11 instead of nastavitve.pop_sonda
I use uint32_t and if I use +1 I got answer 10 (correct) If I use -1 I got 4294967286 (in hex FF FF FF F6 ) this is also correct respond from nextion. Look my previews mail. I still think that there is bug in lib. I can recalculate this in negative number, but if you fix this in lib, it will be great also for other users
can you please send me Arduino code and HMI for Nextion? seithagta@gmail.com
Will be send
Solution:
convert unsinged variableuint32_t
to signed using long
type orint32_t
to print the negative integer with a minus sign (-) in a serial monitor.
example:
int32_t myValue = myNex.readNumber("n0.val");
Serial.print(myValue);
Sending a negative unsigned variable via Serial.print()
you will see the overflow number for the negative integer, this is default for Serial.print()
Note:
Regardless of the value you see on a serial display, the value stored in the Arduino is the negative number you want.
Arduino will handle the variable as follows:
Math with unsigned variables may produce unexpected results, even if your unsigned variable never rolls over. The MCU applies the following rules: The calculation is done in the scope of the destination variable. E.g. if the destination variable is signed, it will do signed math, even if both input variables are unsigned. However with a calculation which requires an intermediate result, the scope of the intermediate result is unspecified by the code. In this case, the MCU will do unsigned math for the intermediate result, because both inputs are unsigned!
unsigned int x = 5;
unsigned int y = 10;
int result;
result = x - y; // 5 - 10 = -5, as expected
result = (x - y) / 2; // 5 - 10 in unsigned math is 65530! 65530/2 = 32765
// solution: use signed variables, or do the calculation step by step.
result = x - y; // 5 - 10 = -5, as expected
result = result / 2; // -5/2 = -2 (only integer math, decimal places are dropped)
copied from here: https://www.arduino.cc/reference/en/language/variables/data-types/unsignedint/
Thanks for all your help Works now
Dear I was away for long time. I'm sorry, for not respond for a long time. Problem was not solved. Like I say before. When you send to display it work fine and you got negative number on display But when you wont to read this number from display you got overflow (display give you example for -1 --> F6 FF FF FF) I think that command
myNex.readNumber("nastavitve.pop_sonda.val");
cannot handle this