Need help with code for mega board and nextion

parts

Mega Board
Nextion 5.0 NX8048T050 TFT
8 Relay board

Hello and good evening. Im looking for a little guidance as i cant seam to figure out where my problem is.
Im trying to use PushCallback and PopCallbacks to fire pins on the Arduino. Any guidance would be greatly appreciated

Nothing happens when i touch the button

  #include <doxygen.h>
#include <NexButton.h>
#include <NexCheckbox.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexGpio.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexRadio.h>
#include <NexRtc.h>
#include <NexScrolltext.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>
#include <NexWaveform.h>






//pins conected

const int LF_UP = 2;
const int LF_DW = 3;
const int RF_UP = 4;
const int RF_DW = 5;
const int LR_UP = 6;
const int LR_DW = 7;
const int RR_UP = 8;
const int RR_DW = 9;

//Declare your Nextion objects , pageid. component name
// front controls
NexButton b2 = NexButton(3, 3, "b2"); // front up button
NexButton b3 = NexButton(3, 4, "b3"); // front left up button
NexButton b4 = NexButton(3, 5, "b4"); // front right up button
NexButton b5 = NexButton(3, 6, "b5"); // front left down button
NexButton b6 = NexButton(3, 7, "b6"); // front right down button
NexButton b7 = NexButton(3, 8, "b7"); // front down button

// back controls
NexButton b8  = NexButton(3, 9, "b8"); // rear up button
NexButton b9  = NexButton(3, 10, "b9"); // rear left up button
NexButton b10 = NexButton(3, 11, "b10"); // rear right up button
NexButton b11 = NexButton(3, 12, "b11"); // rear left down button
NexButton b12 = NexButton(3, 13, "b12"); // rear right down button
NexButton b13 = NexButton(3, 14, "b13"); // rear down button

// Gauges
NexNumber n0 = NexNumber(1, 3, "n0"); // front left psi
NexNumber n1 = NexNumber(1, 4, "n1"); // rear left psi
NexNumber n2 = NexNumber(1, 5, "n2"); // front right psi
NexNumber n3 = NexNumber(1, 6, "n3"); // rear right psi
NexNumber n4 = NexNumber(1, 7, "n4"); // tank psi

// register a buton object to the touch event

NexTouch *nex_listen_list[] = {
  &b2,
  &b3,
  &b4,
  &b5,
  &b6,
  &b7,
  &b8,
  &b9,
  &b10,
  &b11,
  &b12,
  &b13,
  NULL
};

void b2PushCallback(void *ptr) // press event for front up
{
  digitalWrite(LF_UP, LOW);
  digitalWrite(RF_UP, LOW);
} // End of Event

void b2PopCallback(void *ptr) // Release event for front up
{
  digitalWrite(LF_UP, HIGH);
  digitalWrite(RF_UP, HIGH);
}


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);


b2.attachPush(b2PushCallback, &b2);
b2.attachPop(b2PopCallback, &b2);

pinMode(LF_UP, OUTPUT);

}


void loop() {
  // put your main code here, to run repeatedly:
nexLoop(nex_listen_list); //check for any touch events
}

Hello williamchivers, Welcome to the forum.

Far too many new people on here don't post their code or don't post it properly, as you have properly posted code on your first post ++Karma.

I know the Nextion well but I don't use the libraries, so the help I can offer to you is limited. There are others here who know the libraries, but it's up to them if they want to reply. You could try the methods I use in Using Nextion displays with Arduino , if you do I can be more helpful.

He's what I can offer: I had no idea there were quite so many Nextion library files! I can't imagine you need them all. I suspect that, as you have included so many, you don't know what they really do. Find out what they do and use the one(s) you actually need.

What do you think this does?

void loop() {
  // put your main code here, to run repeatedly:
nexLoop(nex_listen_list); //check for any touch events
}

As I said, I don't know the Nextion libraries, but to me that looks like it checks for anything from the Nextion then doesn't do anything with the result, so I would not expect anything to happen when you touch a button, you are not doing anything with the resulting output.

You have created a fairly big sketch with lots in it and discovered it doesn't do anything. Strip it down to 1 button doing 1 thing, get that working then add to it, testing as you add new bits. Don't try to do everything in one go.

Why do you have so much code, when something as simple as triggering the callback when a button becomes pressed, or released, is not working?

Are you sure that buttons generate push and pop events?

Thank you for the reply. The (nex_listen_list) is as far as i know the only thing needed in the loop section as the arduino is listening for one of said buttons to be pressed or released

PaulS: Why do you have so much code, when something as simple as triggering the callback when a button becomes pressed, or released, is not working?

Are you sure that buttons generate push and pop events?

PerryBebbington: Hello williamchivers, Welcome to the forum.

Far too many new people on here don't post their code or don't post it properly, as you have properly posted code on your first post ++Karma.

I know the Nextion well but I don't use the libraries, so the help I can offer to you is limited. There are others here who know the libraries, but it's up to them if they want to reply. You could try the methods I use in Using Nextion displays with Arduino , if you do I can be more helpful.

He's what I can offer: I had no idea there were quite so many Nextion library files! I can't imagine you need them all. I suspect that, as you have included so many, you don't know what they really do. Find out what they do and use the one(s) you actually need.

What do you think this does?

void loop() {
  // put your main code here, to run repeatedly:
nexLoop(nex_listen_list); //check for any touch events
}

As I said, I don't know the Nextion libraries, but to me that looks like it checks for anything from the Nextion then doesn't do anything with the result, so I would not expect anything to happen when you touch a button, you are not doing anything with the resulting output.

You have created a fairly big sketch with lots in it and discovered it doesn't do anything. Strip it down to 1 button doing 1 thing, get that working then add to it, testing as you add new bits. Don't try to do everything in one go.

As of right now im not sure if the arduinois rec. The button events As for so much code. I was setting everything up for later as i have everything in the code for buttons and val that i have on the nextion. I was just trying to get the callback working then apply it to the rest of the buttons then move to the values

PaulS: Why do you have so much code, when something as simple as triggering the callback when a button becomes pressed, or released, is not working?

Are you sure that buttons generate push and pop events?

Thank you for the reply. The (nex_listen_list) is as far as i know the only thing needed in the loop section as the arduino is listening for one of said buttons to be pressed or released

Probably so, but even if this is true and it's working and it's picking up that the button is being pressed or released you don't do anything afterwards that would inform you that something is happening, so you wouldn't know.

Ill try and add a serial.println to get it to the serial monitor to try and veiw whats going on. Would i add that to the loop section or setup after the buton command?

PerryBebbington: Probably so, but even if this is true and it's working and it's picking up that the button is being pressed or released you don't do anything afterwards that would inform you that something is happening, so you wouldn't know.

PerryBebbington: Probably so, but even if this is true and it's working and it's picking up that the button is being pressed or released you don't do anything afterwards that would inform you that something is happening, so you wouldn't know.

OP is turning an output pin on or off, and is turning a pullup resistor on another pin on or off (why beats the hell out of me). Presumably, OP has something, like an LED with resistor, connected to the pin so that he/she can see that the callbacks were called.

You don't need that long list of #includes. The only file you need to include is Nextion.h.

Do you have the "Send Component ID" checked for both Press and Release events on the buttons, in the Nextion editor? This is what enables it to send the event messages to the "host" device.

Also, double-check that your button IDs and component names are correct. Unfortunately, the Nextion editor will sometimes re-number components without telling you, especially when you add or delete components.

My guess is your problem is almost certainly one of the two above issues, as your code looks otherwise correct.

Also, the Nextion library has some SERIOUS problems and it WILL NOT work reliably as they provide it. I STRONGLY suggest you replace the version you're using with the one I've modified here:

https://forum.arduino.cc/index.php?topic=620821.0

My modified library actually works VERY reliably.

For others not familiar with this library, calling nexLoop(nexListenList) periodically is all one has to do. That function parses any events or other messages received from the display, and calls the appropriate callbacks to handle them.

Regards, Ray L.

Thank you so much.

I have both checkboxs checked for press and release. Ill replace the library with your when i get home and see if that helps

RayLivingston: You don't need that long list of #includes. The only file you need to include is Nextion.h.

Do you have the "Send Component ID" checked for both Press and Release events on the buttons, in the Nextion editor? This is what enables it to send the event messages to the "host" device.

Also, double-check that your button IDs and component names are correct. Unfortunately, the Nextion editor will sometimes re-number components without telling you, especially when you add or delete components.

My guess is your problem is almost certainly one of the two above issues, as your code looks otherwise correct.

Also, the Nextion library has some SERIOUS problems and it WILL NOT work reliably as they provide it. I STRONGLY suggest you replace the version you're using with the one I've modified here:

https://forum.arduino.cc/index.php?topic=620821.0

My modified library actually works VERY reliably.

For others not familiar with this library, calling nexLoop(nexListenList) periodically is all one has to do. That function parses any events or other messages received from the display, and calls the appropriate callbacks to handle them.

Regards, Ray L.

williamchivers: Thank you so much.

I have both checkboxs checked for press and release. Ill replace the library with your when i get home and see if that helps

Next step would be to put this in your setup(), and run with the Serial Monitor open, so you can see what is coming in from the display:

nexSerial.begin(9600);

while(1)
{
    if (nexSerial.available())
    {
        int c = nexSerial.read() & 0xff;
        Serial.println(c, HEX);
    }
}

This will show, on the Serial Monitor, exactly what the display is sending. Press/Release event messages are seven bytes long, and will start with 65, and end with three FFs. Each button press, for buttons with "Send Component ID" checked for either Press or Release, will send a message when the button is Pressed and/or Released. If you do NOT see the expected messages, then the problem is in your HMI file, or your serial connection is not wired correctly, so you are not communicating with the display. If you DO see the message, check that the three bytes following the 65 are correct. Those three bytes should be the page the button is on, the ID of the button and a "01" for a press event and a "00" for a release event.

Regards, Ray L.

Thank you again. I will add this to the code and verifythat it is comunicating. Im starting to think i have it wired to the wrong serial port rx tx but i have tried all 3 sets. But i do know that the rx tx lights are not flashing like they would on my other sketches on pin 0 and 1 rx tx.

RayLivingston: Next step would be to put this in your setup(), and run with the Serial Monitor open, so you can see what is coming in from the display:

nexSerial.begin(9600);

while(1) {     if (nexSerial.available())     {         int c = nexSerial.read() & 0xff;         Serial.println(c, HEX);     } }




This will show, on the Serial Monitor, exactly what the display is sending. Press/Release event messages are seven bytes long, and will start with 65, and end with three FFs. Each button press, for buttons with "Send Component ID" checked for either Press or Release, will send a message when the button is Pressed and/or Released. If you do NOT see the expected messages, then the problem is in your HMI file, or your serial connection is not wired correctly, so you are not communicating with the display. If you DO see the message, check that the three bytes following the 65 are correct. Those three bytes should be the page the button is on, the ID of the button and a "01" for a press event and a "00" for a release event.

Regards,
Ray L.

But i do know that the rx tx lights are not flashing like they would on my other sketches on pin 0 and 1 rx tx.

The RX/TX lights are controlled by the Serial-to-USB converter chip, not the Arduino. So, Serial1, Serial2, and Serial3 incoming and outgoing data are not going to cause the TX and RX lights to blink.

Ok thank you

PaulS: The RX/TX lights are controlled by the Serial-to-USB converter chip, not the Arduino. So, Serial1, Serial2, and Serial3 incoming and outgoing data are not going to cause the TX and RX lights to blink.

Oh and i did not mention that all of my press comands need to happen instantly and that is why i choose this style of commands to keep the loop free. I hope that makes sense. ie: when i tuch the screen the relay it triggered and when i release it it shuts off. Please let me know if im head down the correct path

williamchivers: Oh and i did not mention that all of my press comands need to happen instantly and that is why i choose this style of commands to keep the loop free. I hope that makes sense. ie: when i tuch the screen the relay it triggered and when i release it it shuts off. Please let me know if im head down the correct path

That should work fine.

Keep in mind the Nextion powers up at 9600 BAUD, and their library provides no support for changing that. IF you want change the baud rate, my library has a method, nexBAUD, you can call to reliably switch the baud rate at both ends of the connection at any time. If you don't do it correctly, you WILL lose communications with the Nextion. With my library, you can also just call nexInit(baudrate), and it will set the baudrate for you. I always run mine at 115200.

Regards, Ray L.

Sweet. I cant wait to get home and get this going. Thank you again

RayLivingston: That should work fine.

Keep in mind the Nextion powers up at 9600 BAUD, and their library provides no support for changing that. IF you want change the baud rate, my library has a method, nexBAUD, you can call to reliably switch the baud rate at both ends of the connection at any time. If you don't do it correctly, you WILL lose communications with the Nextion. With my library, you can also just call nexInit(baudrate), and it will set the baudrate for you. I always run mine at 115200.

Regards, Ray L.

PaulS: OP is turning an output pin on or off, and is turning a pullup resistor on another pin on or off (why beats the hell out of me). Presumably, OP has something, like an LED with resistor, connected to the pin so that he/she can see that the callbacks were called.

Thank you Paul, I always hesitate to answer when people are using the Nextion libraries in the hope that someone else who knows them will offer to help.

I dont undstand... thats why i did not respond. Can i get clarification on this. Am i doing something wrong or is there a better way to do something? Nextion touchscreen button to a arduino pin connected to a relay board connected to a 12v solenoid

PerryBebbington: Thank you Paul, I always hesitate to answer when people are using the Nextion libraries in the hope that someone else who knows them will offer to help.

I don't understand... that's why i did not respond.

Apologies, as I said before my knowledge of Nextion libraries is limited, if what Ray Livingston and Paul S say about the libraries contradicts what I said then assume they are right and I am mistaken.

To me it looks like you don't do anything in loop other than read the display, according to Paul S doing something is taken care of already. Paul S will have to clarify for you as he clearly understands better than me. Sorry I can't be more help.