Seithan / EasyNextionLibrary

A simple library for Nextion display that uses only four functions. You can easily benefit from Nextion's wide range of features and advantages in just a few easy steps. The library uses a custom protocol that can prove to be a powerful tool for advanced users as it can be easily modified to meet one’s needs.
MIT License
116 stars 29 forks source link

Reading from dual-state button #63

Closed hayderismael closed 11 months ago

hayderismael commented 1 year ago

I am so grateful for all the efforts put into developing this library.

I use myNex.readNumber

to read the status of the dual-state button, for example

int sw1 = myNex.readNumber("mpid.msv1.val");
 digitalWrite(Rly1, sv1 == 1 ? HIGH : LOW);

It is okay when reading one or two buttons, but when there are many buttons, in my case 16 ones, the loop becomes slow and sometimes when hit sw1, sw5 or any other one turn on for a moment.
Would you please advice what is the best way to read the status of the dual-state button?

Thank you

Rocket-Rod commented 1 year ago

Hello hayderismael, Is your code meant to read;

int sw1 = myNex.readNumber("mpid.msv1.val"); digitalWrite(Rly1, sw1 == 1 ? HIGH : LOW);

or

int sv1 = myNex.readNumber("mpid.msv1.val"); digitalWrite(Rly1, sv1 == 1 ? HIGH : LOW);

hayderismael commented 1 year ago

Hello,

My sincere apologies for that typo, and I really appreciate your support.

Yes, I meant

int sv1 = myNex.readNumber("mpid.msv1.val");
digitalWrite(Rly1, sv1 == 1 ? HIGH : LOW);

However, I solved that problem by changing the variables of the switch to byte instead of int. It sounds as if when the serial can't read, it returns -8655, and the Arduino translates it as LOW. I also added two if conditions to only respond to 0 and 1.

In addition, can you please recommend if it is possible to reduce the reading time to make the code faster?

Rocket-Rod commented 1 year ago

If all of your myNex.readNumber() are in the loop you will experience delays as the documentation points out. My suggestion would be to use the predefined triggers to only call your code when needed, leaving the loop to just listen for the Events. Example: In your Nextion project for the Dual-state button, write in the Touch Press Event or Touch Release Event (the choice is yours) printh 23 02 54 00

Now in the Arduino code after the loop (which is doing the listening) write something like the following: void trigger0() { //Event: printh 23 02 54 00 - Touch Press Event for Dual-state button msv1 on Page mpid int sv1 = myNex.readNumber("mpid.msv1.val"); digitalWrite(Rly1, sv1 == 1 ? HIGH : LOW); }

You can do this for 50 pre-defined events. It is explained in the documentation how to increase this number if required.