Nextion button - arduino problem.

Hello,

I'v had a problem all day whit a button in my Nextion 2.4 inch. I only want to increment a variable in case "hour_" when the "but" button is pushed, but on the serial monitor hasn't showed the incremented number.

Could you help me about this problem?

I use:

  • The latest version of Arduino 1.8.
  • The latest version of nextion editor V0.52
  • The ITEADLIB_Arduino_Nextion-master's library

And code that I've used is:

#include <Nextion.h>
#include <Time.h>
#include <TimeLib.h>
#include <SoftwareSerial.h>
int hour_ = 16;
int min_ = 0;

SoftwareSerial nex_com(10, 11); // RX, TX

NexButton b0 = NexButton(3, 3, "b0");
NexNumber n2 = NexNumber(3, 2, "n2");
NexButton b1 = NexButton(0, 5, "but");

NexTouch *nex_listen_list[] =
{
&b0, // Button added
&b1,
NULL // String terminated
} ;

void setup() {
nex_com.begin(9600);
Serial.begin(9600);
delay(500);

nex_com.print("baud=115200");
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
//
nex_com.end();
nex_com.begin(115200);
setTime(hour_,min_,0,12,4,17);
b0.attachPush(b0PushCallback);
b1.attachPush(b1PushCallback);
}

void loop()
{
delay(10);
nexLoop(nex_listen_list);
Serial.println(hour_);
}
void b0PushCallback(void *ptr)
{

hour_= hour_ - 1;

nex_com.print("n0.val=");
nex_com.print(hour_);
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
}
void b1PushCallback(void *ptr)
{
hour_ = hour_ + 1;

nex_com.print("n2.val=");
nex_com.print(hour_);
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);

} // End of press event

SoftwareSerial will not work with 115200 baud.

Thank's for that.

I changed it to 9600 baud, but a problem hadn't solved.

There is something else that stop me. :frowning: :frowning:

Barca1991:
I only want to increment a variable in case "hour_" when the "but" button is pushed, but on the serial monitor hasn't showed the incremented number.

So why don't you increment/decrement hour_, but an undefined variable max_ ?

My mistake, but the problem isn't that :frowning:

So don't you think that you should post the new code?

And please use code tags (the </> button).

This looks strange too

NexButton b0 = NexButton(3, 3, "b0");
NexButton b1 = NexButton(0, 5, "but");

are you shure the page ids and component ids are correct?

Don't change already posted code, that renders the answers meaningless.

Post the code again if you change it.

I corrected my mistake in the code above. I've tried whit this code:

#include <Nextion.h>

#include <Time.h>
#include <TimeLib.h>
#include <SoftwareSerial.h>

int hour_ = 16;
int min_ = 0;

SoftwareSerial nex_com(10, 11); // RX, TX

NexButton b0 = NexButton(3, 3, "b0"); // Button added
//NexButton b1 = NexButton(3, 4, "b1"); // Button added
NexNumber n2 = NexNumber(3, 2, "n2");
NexButton b1 = NexButton(0, 5, "but");

NexTouch *nex_listen_list[] =
{
&b0,
&b1,
NULL
} ;

void setup() {
nex_com.begin(9600);
Serial.begin(9600);
delay(500);

nex_com.print("baud=9600");

nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);

setTime(hour_,min_,0,12,4,17);
b0.attachPush(b0PushCallback); // Button press
b1.attachPush(b1PushCallback); // Button press

}

void loop()
{
delay(10);
//nexLoop(nex_listen_list);
nex_com.print("n0.val=");
nex_com.print(hour());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
delay(2);
nex_com.print("n1.val=");
nex_com.print(minute());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
nexLoop(nex_listen_list);

Serial.println(hour_);

}

void b0PushCallback(void *ptr)
{

max_ = hour_ - 1;

nex_com.print("n0.val=");
nex_com.print(hour_);
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
}
void b1PushCallback(void *ptr)
{
hour_ = (hour_ + 1);

nex_com.print("n2.val=");
nex_com.print(hour());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
}

And when I use this functions

nex_com.print("n2.val=");
nex_com.print(hour());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);

I successfully send the date and visualize the hours and minutes.

But when I push a button on the screen the hour_ variable isn't increment.????

//nexLoop(nex_listen_list);

?

No listen - no action.

And why don't you use code tags?

NexTouch *nex_listen_list[] =
{
&b0,
&b1,
NULL
} ;

It's the touch event.

Is it correct that?

Which touch event?

You don't even listen to SoftwareSerial (you commented that statement, see #7).

void loop()
{
delay(10);
//nexLoop(nex_listen_list);
nex_com.print("n0.val=");
nex_com.print(hour());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
delay(2);
nex_com.print("n1.val=");
nex_com.print(minute());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
nexLoop(nex_listen_list); --------- I use this here.

Serial.println(hour_);

}

What happened. I'm so desperate. ;(;(

You don't answer my questions, you ignore the requested usage of code tags.

Remove all commented statements that you don't wish to use in the code later or for debug,
they only distract and confuse the reader (at least me).

Don't use delay for timing, you should try to keep loop running as fast as possible.

Ok Whandall, sorry.

#include <Nextion.h>
#include <Time.h>
#include <TimeLib.h>
#include <SoftwareSerial.h>
int hour_ = 16;
int min_ = 0;
SoftwareSerial nex_com(10, 11); // RX, TX

NexButton b1 = NexButton(0, 5, "but");

NexTouch *nex_listen_list[] =
{
&b1,
NULL
} ;

void setup() {
nex_com.begin(9600);
Serial.begin(9600);
delay(500);
nex_com.print("baud=9600");
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
setTime(hour_,min_,0,12,4,17);
b1.attachPush(b1PushCallback);
}

void loop()
{
nex_com.print("n0.val=");
nex_com.print(hour());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.print("n1.val=");
nex_com.print(minute());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
nexLoop(nex_listen_list);
Serial.println(hour_);
}

void b1PushCallback(void *ptr)
{
hour_ = (hour_ + 1);
Serial.println(hour_);
}

It's the compact code, Do you see anything wrong? Because I'm not able to do it so far. ;(

You did not use code tags, again.

I can not see your HMI, you don't answer questions regarding it.
Are you shure that the button is on page 0 with component id 5?

Hi, again.

You can see attachments, especially the pic.

You are writing the values of hour_ and min_ much too often,
they should only be written when they change.

If you have fixed that (that keeps you in a blocked state on SoftwareSerial),
print hour_ only when it has changed (or once a second) so you will not block on Serial.

After these changes press Ctrl-T in the IDE to get a better formatting and post the resulting
compiling code.

If you will not use code tags (the button </>), I will stop wasting time here.

Sorry, but I don't understand , when I should use "code tags (the button </>)", Is it here on the forum?

#include <Nextion.h>
#include <Time.h>
#include <TimeLib.h>
#include <SoftwareSerial.h>
int hour_ = 16;
int min_ = 0;
SoftwareSerial nex_com(10, 11);

NexButton b1 = NexButton(0, 5, "but");

NexTouch *nex_listen_list[] =
{
&b1,
NULL
} ;

void setup() {
nex_com.begin(9600);
Serial.begin(9600);
delay(500);
nex_com.print("baud=9600");
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
setTime(hour_, min_, 0, 12, 4, 17);
b1.attachPush(b1PushCallback);
}

void loop()
{
nex_com.print("n0.val=");
nex_com.print(hour());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.print("n1.val=");
nex_com.print(minute());
nex_com.write(0xff);
nex_com.write(0xff);
nex_com.write(0xff);

nexLoop(nex_listen_list);
}

void b1PushCallback(void *ptr)
{
hour_ = hour_ + 1;
Serial.println(hour_);
}

It is, but nothing............ ;( ;(

Yes, that </> button is here on the forum. You click the reply button and it opens a new page to compose a reply; you will see it there.

Not familiar with nexion but ain't you supposed to have a callback attached to the but button?

Barca1991:
Sorry, but I don't understand , when I should use "code tags (the button </>)", Is it here on the forum?

I you had bothered reading

Read this before posting a programming question ... #6

you knew.

Whandall:
You are writing the values of hour_ and min_ much too often,
they should only be written when they change.
..
After these changes press Ctrl-T in the IDE to get a better formatting and post the resulting
compiling code.

sterretje:
Not familiar with nexion but ain't you supposed to have a callback attached to the but button?

It is in there.

 b1.attachPush(b1PushCallback);

I don't use SoftwareSerial with the Nextion, but I think one will run into problems
when blocking on the transmit and hoping to receive something at the same time.

Maybe a different SoftwareSerial works better, like AltSoftSerial or the like.

A better communication strategy (sending only changes) would help anyway.