Start a new topic

NexVariable cannot setValue

In Arduino UNO R3 code


Arduino Code :

.......

// Initial 
uint32_t number;

// Config NexComponent  local = NexComponent(pgno, compid, "objname");

NexVariable bv0 = NexVariable(0,1,"bv0");


// When button callback, I set some value to bv0

void buttonCallback(void *ptr)

{

uint32_t number;
number=1;
bv0.setValue(number);   // not crash but not work

}


Nextion HMI Code :

// In Timer Code run every 100 millisec

if(bv0.val==1)

{

t0.txt="ON"

}



Full ino code please

Also HMI ... the code is a snippet that I provided

but may not match what your HMI needs

   Follow below

#include "Nextion.h"


NexButton b0 = NexButton(0, 5, "b0");
NexButton b1 = NexButton(0, 13, "b1");
NexText ta0 = NexText(0, 2, "ta0");
NexText ta1 = NexText(0, 4, "ta1");
NexProgressBar mj0 = NexProgressBar(0, 15, "j0");

NexVariable b0Val = NexVariable(0, 7, "b0_val");
NexVariable b1Val = NexVariable(0, 8, "b1_val");
NexVariable refresh = NexVariable(0, 9, "refresh");
/*
* Register a button object to the touch event list.
*/
NexTouch *nex_listen_list[] =
{
	&b0,
	&b1,
	NULL
};

void b0Callback(void *ptr)
{
	uint16_t len;
	uint16_t number;

	uint32_t vNum;

	

	b0Val.getValue(&vNum);
	b0Val.setValue(1 - vNum);
}

void b1Callback(void *ptr)
{
	uint16_t len;
	uint16_t number;

	uint32_t vNum;




	b1Val.getValue(&vNum);
	b1Val.setValue(1 - vNum);
}

void setup()
{
	Serial.begin(9600 );
	nexInit();

	b0.attachPop(b0Callback);
	b1.attachPop(b1Callback);

	/* add setup code here */


}

void loop()
{

	/* add main program code here */

	nexLoop(nex_listen_list);

} 

    

Your code has changed from what you posted above.


Assuming Variable in HMI with objname b0_val has id of 7

and other assumptions

 .. press and release of b0,  ...


Nah I have to stop.

  your callbacks are not defined properly

Too many issues, I have no idea what you understand.

len and number defined as int16_t's for what reasons?


if(bv0.val==1) .... in timer - nothing in Arduino side matches


And why Serial.begin(9600);  ?

 - NexInit() suppose to take care of

 UNO only has 1 serial, suppose to be taken up by Nextion


You are all over the place.

 - coding MCUs is exacting - they do exactly as you tell them


But again, I have no idea from your changing code

   you can't just throw copied lines and hope it works

   you have to program purposefully telling what you want done.

For now  

I have set wire GND from HMI connect to USB and MCU. It works.


Thank you

Login or Signup to post a comment