Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

button callback not working #32

Open
rangathedon opened this issue Aug 15, 2017 · 18 comments
Open

button callback not working #32

rangathedon opened this issue Aug 15, 2017 · 18 comments

Comments

@rangathedon
Copy link

I have followed the example but button callbacks dont seem to work; Any idea? does anything special need to be done on the nextion editor to the HMI file to get callbacks working? This is both on arduino nano and mega;

@DanNixon
Copy link
Owner

Have you enabled the button press/release events in the Nextion Editor?

@rangathedon
Copy link
Author

I have enabled the release event since I only need that; do both events need to be enabled? I am using the latest nextion editor;

@DanNixon
Copy link
Owner

That should be enough.

Not too sure of the issue then, I haven't used these displays for a while now and have no hardware to test with.

@rangathedon
Copy link
Author

rangathedon commented Aug 16, 2017 via email

@viktor1970
Copy link

I'm using this library since a while, no issues in button callback (or other components)
Check the component ID in variable declaration, I experienced that 90% of times the issue is a wrong component id.

@ranganathp
Copy link

I figured the issue; it does work with simple examples but it does not work with examples where I read or write values from other components in the loop() method after nex.poll(); Not sure how to fix this issue; I need to be able to set values to some components in loop method and also be able to handle button click callbacks; Please share your insights;

@viktor1970
Copy link

I experienced is not a good method to update components in loop: the serial will be always (or almost) committed to send data (anche check return values).
In my projects, components update is always done either after a touch event or on a timer basis (using millis or TimeAlarm library).

@rangathedon
Copy link
Author

rangathedon commented Sep 9, 2017 via email

@viktor1970
Copy link

viktor1970 commented Sep 9, 2017

Yes, loop is like a timer but, even with get sensor data and other operations, it's too fast.
If you update data, for example, every 2-3 seconds there will be no conflict because the library (and the display) can deal with that amount of data speed.

In one of my project I've a timer on screen and, of course, it's updated every second (using TimeAlarms library, but will be the same using millis() ). The serial is set to 38400 (software serial, cannot be faster), and the touch buttons (stop, pause, restart) are working perfectly without any conflict.

@viktor1970
Copy link

Another tip, I don't know if it's your case, is to update components only on the page currently displayed (even with components set as global).
I've a variabile that tracks the current page (page switch is managed by Arduino) so if, for example, I'm on the page 2 it's not necessary to update date/time and sensor data on page 0. When I'll go back on page 0, the callback attached will call the function to update the display.

@ranganathp
Copy link

ranganathp commented Sep 9, 2017 via email

@viktor1970
Copy link

The most probable issue that can cause what you're experiencing is wrong ID component in variable declarations.
But are you sure the display is communicating with Arduino?
A simple code to show what Nextion is sending
while(sr.available()) { Serial.print((uint8_t)sr.read()); }

Where sr is the serial attached to Nextion.

@rangathedon
Copy link
Author

rangathedon commented Sep 9, 2017 via email

@rangathedon
Copy link
Author

Any insights Dan?

@viktor1970
Copy link

viktor1970 commented Sep 10, 2017

I did: 3 buttons, 3 number field and 2 text field.
2 numbers are updated every seconds by RTC; as expected, the callbacks ARE fired, so there's no issue on the library.

Here's the video: https://youtu.be/ozAMaVkeW9c

And here's the code:

#include <SoftwareSerial.h>
#include <Nextion.h>
#include <NextionNumber.h>
#include <NextionText.h>
#include <NextionButton.h>
#include <RTClib.h>

RTC_DS3231 rtc;
SoftwareSerial sr(3,2);
Nextion nex(sr);

NextionNumber n0(nex,0,3,"n0");
NextionNumber n1(nex,0,4,"n1");
NextionNumber n2(nex,0,6,"n2");
NextionText t1(nex,0,8,"t1");
NextionText t2(nex,0,9,"t2");
NextionButton b0(nex,0,1,"b0");
NextionButton b1(nex,0,2,"b1");
NextionButton b3(nex,0,7,"b3");

unsigned long tm1;
char bf[20];

void cbData(NextionEventType type, INextionTouchable *widget)
{
 if (type == NEX_EVENT_POP)
 {
  sprintf(bf,"Val: %d",n1.getValue()); t1.setText(bf);
  sprintf(bf,"%d",n0.getValue()+n2.getValue()); t2.setText(bf);
 }
}

void cbUp(NextionEventType type, INextionTouchable *widget)
{
 if (type == NEX_EVENT_POP) { n2.setValue(n2.getValue()+1);}
}
void cbDown(NextionEventType type, INextionTouchable *widget)
{
 if (type == NEX_EVENT_POP) { n2.setValue(n2.getValue()-1);}
}

void setup() {
  rtc.begin();
  sr.begin(38400);
  nex.init();
  Serial.begin(115200);
  b0.attachCallback(&cbData);
  b3.attachCallback(&cbUp);
  b1.attachCallback(&cbDown);
  tm1=millis();
}

void loop() {
 nex.poll();
 if((millis()-tm1)>1000)
 {
  Serial.println(rtc.now().second());
  n1.setValue(rtc.now().second()); n0.setValue(rtc.now().minute());
  tm1=millis();
 }
}

@DanNixon
Copy link
Owner

@rangathedon You are probably expecting things to happen much faster than what they can. Nextion displays are really slow.

There are not many reasons to be updating values periodically let alone in the main program loop, save this for when the values actually change (or at least change a sufficient amount to be relevant to the user).

Even if you do your updating periodically I'd guess you have a cluster of update calls which is probably leading to the display not reading the full payloads of subsequent messages.

Without looking at your code it is difficult to tell and the best I can offer is speculation.

If you really need a value to be updated at less than 1 second intervals then Nextion displays are not the solution.

@viktor1970
Copy link

@DanNixon you're right. That's the same I wrote (loop too fast); I modified my code above: it works even with a 100ms interval, below that value it's unresponsive.

But @rangathedon wrote he has 2-3 seconds delays (I hope not delay(2000) ) so my thoughts is an error in his code, since no one else has reported this "issue".

@kebien6020
Copy link

I had a similar problem and in my case it was because I did a page x command in the Press (Push) event on the Nextion editor, and was listening for the Release (Pop) event on the arduino program. It never triggered since the screen was no longer there.

Moving the code from the Press event to the Release event in the editor solved it for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants