Ardiono loop code using a Nextion serial but to trigger internal bit

Good morning again, great help was achieved yesterday, thanks Patrick Martin from Nextion site but

I now need a hand taking this a little bit further, I have changed the buttons from DS to single buttons, for some reason the Arduino was not acting on every serial bit received, which means the buttons were sometimes on when they were off outputs,

  1. what causes that, I am still finding it with the b0’s that the Nextion misses or does not respond the serial bit, I can see it in the serial monitor for every press, but the processor responds to the second, third or forth receive ???

  2. I can successfully turn the relay on and off via on button or an off button (CODE 1 below) on the Nextion when the Arduino is driving the digital output directly, or conversely via a single button (CODE 2 below) using the else command, they work great, thanks again Patrick

  3. with the point 2 above being said, I really need the Arduino to use the serial input as an internal bit, ie button (b0) sets internal bit to HIGH or LOW, using the same logic as setting a digiatal output. (CODE 3 below does not work) I can then use the internal bit to drive the output but cant get it to turn off on the next press or the off button press. later I will be using logic to use the bits to work out what output to drive, hence the direct 1 to 1 will not be required to progress the project

thanks in advance Ben


CODE 1 Pin13 on / off by two buttons on the nextion bo and b1


#include <Arduino.h>
#include “Nextion.h”

// parameters page #, component .id, component .objname

NexButton myButton0 = NexButton(0,17, “b0”);
NexButton myButton1 = NexButton(0,18, “b1”);
uint32_t digitalstate11, digitalstate12 = 0;

const int relay1Pin = 13;
byte pump1;
byte pump2;

NexTouch *nex_listen_list = {
&myButton0, &myButton1,
NULL
};

void myButton0ReleaseCallback(void *ptr) {
digitalstate11 = 1-digitalstate11;
if(digitalstate11==1){
digitalWrite(relay1Pin, HIGH);
}
}
void myButton1ReleaseCallback(void *ptr) {
digitalstate12 = 1-digitalstate12;
if(digitalstate12==1){
digitalWrite(relay1Pin, LOW);

}
}
void setup(void) {
pinMode(relay1Pin,OUTPUT);
nexInit();
myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
myButton1.attachPop(myButton1ReleaseCallback, &myButton1);
}

void loop(void) {
nexLoop(nex_listen_list);

}


CODE 2 turns Pin13 on / off by a single button on the nextion bo


#include <Arduino.h>
#include “Nextion.h”

// parameters page #, component .id, component .objname

NexButton myButton0 = NexButton(0,17, “b0”);

uint32_t digitalstate11 = 0;

const int relay1Pin = 13;

NexTouch *nex_listen_list = {
&myButton0,
NULL
};
void myButton0ReleaseCallback(void *ptr) {
digitalstate11 = 1-digitalstate11;
if(digitalstate11==1){
digitalWrite(relay1Pin, HIGH);
}else{
digitalWrite(relay1Pin, LOW);
}
}

void setup(void) {
pinMode(relay1Pin,OUTPUT);
nexInit();
myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
}

void loop(void) {

nexLoop(nex_listen_list);
}


CODE 3 I need to turn relay1Pin by an internal bit that the Nextion sets HIGH or LOW, can get it to work past the first on, output stays on or the output pulses or cant drive the output at all, I have tried the code in the top section or in the void loop section

???



#include <Arduino.h>
#include “Nextion.h”

// parameters page #, component .id, component .objname

NexButton myButton0 = NexButton(0,17, “b0”);
NexButton myButton1 = NexButton(0,18, “b1”);
uint32_t digitalstate11, digitalstate12 = 0;

const int relay1Pin = 13;
byte pump1;

NexTouch *nex_listen_list = {
&myButton0, &myButton1,
NULL
};

void myButton0ReleaseCallback(void *ptr) {
digitalstate11 = 1-digitalstate11;
if(digitalstate11==1){
digitalWrite(pump1, HIGH);
}
}
void myButton1ReleaseCallback(void *ptr) {
digitalstate12 = 1-digitalstate12;
if(digitalstate12==1){
digitalWrite(pump1, LOW);

}
}
void setup(void) {
pinMode(relay1Pin,OUTPUT);
nexInit();
myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
myButton1.attachPop(myButton1ReleaseCallback, &myButton1);
}

void loop(void) {

pump1 = digitalRead(pump1);

if (pump1==HIGH){
digitalWrite(relay1Pin, HIGH);
}
if (pump1==LOW){
digitalWrite(relay1Pin, LOW);
}
nexLoop(nex_listen_list);

}


1 Comment

Patrick Martin

said 5 minutes ago

Ben the code in the Code #3 button b0 release and b1 release

1 digitalstate11 = 1-digitalstate11;

2 if(digitalstate11==1){

3 digitalWrite(pump1, HIGH);

4 }

line 1 is a toggle, if value was 1 it will be 0, if it was 0 it will be 1

but the logic that follows is incomplete.

You must choose

a) button press turns on and next press turns off

b) button b0 turns on and button b1 turns off

If a) your logic would need to add

4 else {

5 digitalWrite(pump1, LOW);

6 }

If b) your logic between buttons b0 and b1 need to drop the toggle

and b0 sets HIGH while b0 sets LOW.

I will try to assist answering the user Nextion side questions

but the MCU and coding side - you need to dig in a bit

There are a number of reasons outside of code why an MCU

may not be able to drive things by code alone, there will be

many many manuals and datasheets to read as you go.

Pulsing can be a sign of improper power supply, you will need to

investigate. MCU may be asked to supply more than it can.

But many of the Arduino side questions - Arduino Forum

You like this

B

Ben

More topics in Free Chat

Get current value of progressbar for Text value

how to upload from Nextion Editor to the display ?

how to make the types of gauges seen in the demo ?

Wrong link in Nextion Wiki Tutorial

Nextion and Arduino

Weather example ported to Particle platform

Arduino DUE Support for Arduino Library

Upload of a new firmware via serial from an Arduino

5 displays via software serial possible?

Nextion 4.3 maximum baudrate

See all 564 topics

Home -Solutions -Forums -Tickets Cookie policy

My finger just about fell off scrolling down to the reply box. POST YOUR CODE PROPERLY!

You clearly did not read the stickies at the top pf the forum. Do so now, and fix your post.

Hi,
I copy code 2 but need to insert 2 line to get good compile but it is unable of working.
Please show me lines TX and RX of Nextion display connect to pin 0;1 or 10;11 of the Nano Arduno board. Thanhk you.

#include <Arduino.h>
#include “Nextion.h”
#include “NexButton.h”// just insert
// parameters page #, component .id, component .objname
SoftwareSerial HMISerial(10,11); //just insert
NexButton myButton0 = NexButton(0,2, “b0”);

uint32_t digitalstate11 = 0;

const int relay1Pin = 13;

NexTouch *nex_listen_list = {
&myButton0,
NULL
};
void myButton0ReleaseCallback(void *ptr) {
digitalstate11 = 1-digitalstate11;
if(digitalstate11==1){
digitalWrite(relay1Pin, HIGH);
}else{
digitalWrite(relay1Pin, LOW);
}
}

void setup(void) {
pinMode(relay1Pin,OUTPUT);
nexInit();
myButton0.attachPop(myButton0ReleaseCallback, &myButton0);
}

void loop(void) {

nexLoop(nex_listen_list);
}

CHINHCUONG: ) void myButton0ReleaseCallback(void *ptr) { digitalstate11 = 1-digitalstate11; if(digitalstate11==1){ digitalWrite(relay1Pin, HIGH); }else{ digitalWrite(relay1Pin, LOW);

nexInit(); myButton0.attachPop(myButton0ReleaseCallback, &myButton0); //......

It needs to read: myButton0.attachPop(myButton0PopCallback, &myButton0).

Nextions release event is stated as Pop