Unofficial Nextion/TJC User Forum

Change button color arduino

I am trying to change the arduino sketch to use the nextion.h library. I can get it to work for sending a letter from the nextion to my computer. It is also working to use the arduino serial monitor to send a color change to the nextion button.
My issue is that I can’t figure out how to send a color change from serial monitor to nextion with the library.
I am using a promicro with SoftwareSerial.h

#include <Nextion.h> // Nextion Library
#include <SoftwareSerial.h> // Library for Software Serial Port
#include <Keyboard.h> // Keyboard Emulation

SoftwareSerial mySerial(9, 10); // RX, TX

char incomingByte; // for incoming serial data from computer

// This part is used to declare all the objects we can receive from the Nextion Display
// (page, ID, Object name)
NexButton b0 = NexButton(0, 1, “b0”); // “TEST” Button on Page 0

// This part is used to list the possible touchscreen events in an Array
NexTouch *nextion_touch_events[] =
{
&b0, // Page 0 Button 0
NULL // End string
};

// This part is for the different functions for Main Screen Touch events (page zero)
void b0PushCallback(void *ptr)
{
Keyboard.press(‘t’); // ‘t’ for TEST
delay(250); // Delay of 250ms to register the key press
Keyboard.release(‘t’); // stop pressing the key
}

//________________________________________________________
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Keyboard.begin();
nexInit(); // Nextion Display initalize

// Link the touchscreen events to their relative functions in the code
// attachPush for press events or attachPop for release events
b0.attachPush(b0PushCallback);

}

//_______________________________________________________________________________
void loop() {

delay(100); // small delay before checking again for touch events
nexLoop(nextion_touch_events); // Check for any touch event and run the associated function

// send data only when you receive data:
if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();
        }
        
 if (incomingByte == 'b' || incomingByte == 'B')  // turn button 0 white
{
   mySerial.print("b0.bco=65535");
              mySerial.write(0xff);
              mySerial.write(0xff);
              mySerial.write(0xff);
}

         if (incomingByte == 'o' || incomingByte == 'O') // turn button 0 green
{
   mySerial.print("b0.bco=28320");
              mySerial.write(0xff);
              mySerial.write(0xff);
              mySerial.write(0xff);
}

}

Forget the library. It’s easier to just send the desired colors:

const char* bt1_bco = "pageConfig.bt1.bco=7158";
const char* nextionEnd = "\xFF\xFF\xFF";

	// Update Nextion
	SerialPrint(bt1_bco);
	SerialPrint(nextionEnd);

I use the ASCII value to check the code sent from the Nextion which can be installed on one of several serial ports. The MCU can be connected to a PC for debugging.

if ((Serial1.available() > 0) || (Serial2.available() > 0)|| (Serial3.available() > 0))
{
	incomingByte = SerialRead(serialId);

//#ifdef DEBUG
Serial.print("serialId: ");
Serial.println(serialId);
Serial.print("incomingByte: ");
Serial.println(incomingByte);
//#endif // DEBUG

	// Allow processing time for serial data
	MilliDelay(15);


	switch (incomingByte)
	{
		//case 33 through 42: Not Used.
		//{
		//	break;
		//}
		case 41: // ) - Not Used
		{
			// Do something here;
			break;
		}
		//...

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).