Start a new topic

Change button colors via Arduino

Is there a way to change the button colors, and colors of all the other objects?  I looked around the documents, and found the "settext' to change the TEXT...  But I didn't find anything to SET COLOR.


It is not part of the library, but is easy enough to do...


  

// Change page0 button "bt0" State 1 background to white
// sending 3 "0xff" tell the nextion that the command is complete...
Serial.print("page0.bt0.bco0=65535"); 
Serial.write(0xff);
Serial.write(0xff);    
Serial.write(0xff);

// Change page0 button "bt0" State 2 background to black
// sending 3 "0xff" tell the nextion that the command is complete...
Serial.print("page0.bt0.bco1=0");  
Serial.write(0xff);
Serial.write(0xff);    
Serial.write(0xff);

  


1 person likes this

Thanks for the information.  If you have the page, can you or anyone send me a link to the command reference for this?  I'm lost.

Thanks for the information.  If you have the page, can you or anyone send me a link to the command reference for this?  I'm lost.

The command link:
https://www.itead.cc/wiki/Nextion_Instruction_Set

In the nextion editor, the attributes that can be changed at runtime are GREEN.
So for the below example,
b1.picc
b1.picc2
b1.pco
b1.pco2
b1.font
b1.xcen
b1.ycen
b1.txt

NextionAttributes.png


Then we using the command, just fully define the object you are refrencing...
Serial.print("page0.b1.picc=2"); // This means Page 0, button b1 attribute picc = 2

Hope that helps

 

The above button was set as a Crop Picture, so no bco or bco2 attribute was available.
If your button is set as a solid color, then the bco or bco2 attributes will be available.

 

Your example did not work.  I was trying to figure out why for several hours.  It took me a while to then figure out I can use the Nextion editor to test commands instead of sending using Arduino and debug through the 20 other things that could have gone wrong there.


Serial.print("page0.bt0.bco0=65535");


"BCO0" is not a property of button. BCO is. And the Nextion editor kept throwing

errors when I tried to use the line. I finally figure out, I didn't need "page0"

(which also kept throwing errors).

in v0.40 of the Nextion Editor:


bco0 and bco1 are attributes of the Dual State Button component

bco and bco2 are attributes of the Button component

It is necessary to specify the component you are using.

b0 is generally a Button component bt0 is generally a Dual State Button


page0.bt0.bco0=65535 will only be valid if page0 is the current page, other wise it is not in memory

if page0 is the current page, bt0.bco0=65535 is all that is needed

get dp will return the current page number using the numeric return code for 0x71


Serial is usually for the serial debug port or via USB

Serial2 is generally defined as the serial port for Nextion

You will have to check your configuration to know which is which

Examples with Serial.print() are generally used and a guideline, not necessarily verbatim

You will have to check your configuration to know which is which and adjust the example accordingly.


if your library has not been updated to include new changes, you will need to modify your library.


Most examples provided on the board are to provide guidelines - some hints of where to look

And not necessarily working code to be used verbatim.


2 people like this

I was not planning to change colors of anything on a page that wasn't the current one.  When I asked for help to change button colors, I guess I should have specifically said - I was only trying to test with a plain vanilla flat button, and did not use a dual-state button or anything else that was fancy.


Since there was a function in the library for "settext" I thought the library included all the rest of the functions to set colors and so on.  Now I know I can just set all the properties sending the serial strings, finally helps to move the project along.  


Thanks for the advice.

Login or Signup to post a comment