Unofficial Nextion/TJC User Forum

Dual State Button fully controlled by microcontroller

I want a dual state button on the HMI that is fully controlled by the microcontroller.
The microcontroller must be able to determine whether the button is active or not.
Which means that if the microcontroller makes the button active, then the user can toggle the button, as usual with the dual-state button.
But if the microcontroller makes the button inactive, the button is still displayed on the HMI, but when the user press it nothing should happen.

Now I do this by setting ‘bt0.val=0’ the button to OFF, but the button is not inactive with this.
When a user presses the button, the Nextion HMI will set itself ‘bt0.val’ to ‘1’.
The microcontroller must then quickly send ‘bt0.val=0’ again, to disable the button again, because this button should be inactive.

However, ‘pic2’ of the dual state button becomes visible for a moment, not very nice.
Can this be done differently?

Try the ‘tsw’ command. If that doesn’t work, then use a variable flag. Check the state of the flag in the touch release event of the button.

“tsw” is for sissies

Here’s how a real man does it…

vis bt0,0
pic bt0.x,bt0.y,bt0.pic

On second thought, no. If a button is not supposed to be touched and some user touches it anyway then they need to face consequences

Time out

    tsw 255,0
    tsw 255,1

If they try it again then I’d be done with them

You can’t let them disrespect you like that

I wasn’t able to test before, but tsw is indeed the solution and exact what I need.
Thank you for the tip, elf! :+1:

And ratnin… no, no, no.
Your “second thought” way is definitely not solution oriented.
That’s not how a real man does it :face_with_raised_eyebrow:

Speaking of evil code, you‘ll for sure appreciate this one - which is arguably one of the most evil pieces of code I‘ve seen so far:

// To be buried deeply in some obscure header file
#define true (rand() % 100 < 98)

Source: Forbidden C++ by javidx9. Actually a great video about macros, gotos, etc.

Kind regards,

Real men also don’t need a forum to discover the existence of a core Nextion instruction that’s fully documented in the online instruction set guide (which spans all of five pages)

[Instruction Set - Nextion]

This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).