Start a new topic

External push button VS Nextion push button

Hi all

Can someone help me about the code for arduino,


I alreay make push button on Nextion on b0 and make extenal push button connect on pin 2 for arduino and led pin3 for output


I need when i push extenal push button. The Nextion push button will effect too.

It mean the  Led pin3 will work when I push both external button and Nextion push button


Thank you




Kcbike


You will have to capture the state of the pin.  Store this in a variable to compare against (ie: extButton)

Then when it changes .. update the nextion button to reflect the same.  Pseudo code below.


if pinState <> extButton then

  begin

    send("b0.val="+itoa(pinState));

    // don't forget to send the three 0xFF byte data terminator

    extButton = pinState  // set extButton variable to new pinState

    // set pin for the led to reflect pin state

  end


or IF this is an Enhanced Nextion you could simply bind an IO to the button

cfgpio 2, 1, b0 ... see cfgpio in the Nextion Instruction Set.



You will also have to capture the state of the Nextion Button when the button is pressed on the Nextion

You could check the Send Component ID under the button's Touch Pressed Event and then capture the Nextion return codes for the event - this will automatically include it the button is being pressed or if it was released in the return code .. simply parse that value and turn the LED on or off based on its value

see Nextion Return Codes in the Nextion Instruction Set.


If help is needed to learn to program the Arduino, there are many many sources on the net.

I should add, it is a long wait for Arduino code questions to get answered on the board.

Thank you Patrick Martin very much

I am very weak both english and Arduino BTW you answer is very help ful  and clear for concept


but when I try your code it error . Can you help me slove my code


Now I can change the picture by using  external button pin 2

but I still do not know  how to change state of button b0 when I press the external button pin2


Thank for your kind

 

 
#include "Nextion.h"

NexPicture p0 = NexPicture(0, 1, "p0");
NexPicture p1 = NexPicture(0, 4, "p1");
NexButton b0 = NexButton(0, 2, "b0");
NexButton b1 = NexButton(0, 3, "b1");
const int buttonPin = 2; 
int buttonState = 0;  
const int ledPin =  13; 
char buffer[10] = {0};

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

void p0PopCallback(void *ptr)
{
    uint32_t number1 = 0;
    dbSerialPrintln("p0PopCallback");

    p0.getPic(&number1);

    if (number1 == 1)
    {
        number1 = 2;    
    }
    else
    {
        number1 = 1;
    }
    
    p0.setPic(number1);
}
void b0PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
 
    btn->getText(buffer, sizeof(buffer));

 
    if (strcmp(buffer,"ON"))
    {
       // digitalWrite(ledPin, HIGH);
        strcpy(buffer, "ON");
    }
    else
    {
        digitalWrite(ledPin, HIGH);

        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
    
}

void b1PopCallback(void *ptr)
{
    NexButton *btn = (NexButton *)ptr;
    memset(buffer, 0, sizeof(buffer));
    btn->getText(buffer, sizeof(buffer));
    if (strcmp(buffer,"BACK"))
    {
        //digitalWrite(ledPin, HIGH);
        strcpy(buffer, "BACK");
    }
    else
    {
        //digitalWrite(ledPin,HIGH);
        strcpy(buffer, "OFF");
    }
    btn->setText(buffer);
   
}

void setup(void)
{
   dbSerial.begin(9600);
    nexInit();
    p0.attachPop(p0PopCallback);
    b0.attachPop(b0PopCallback, &b0);
    b1.attachPop(b1PopCallback, &b1);
    
    dbSerialPrintln("setup done");
     pinMode(buttonPin, INPUT);
     pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin,HIGH);
}

void loop(void)
{
    nexLoop(nex_listen_list);
  buttonState = digitalRead(buttonPin);
 uint32_t number2 = 0;
 
     if (buttonState == LOW) {
    p1.getPic(&number2);
  
   if (number2 == 1)
    {
        number2 = 3; 
        digitalWrite(ledPin,LOW); 
        
       
    } 

       else
    {
        number2 = 1;
    }
  p1.setPic(number2);

   }


}

 


HMI

Pseudo code is never expected to be used verbatim (exactly as written).  Pseudo code - is code that gives the concept but could most likely fail the grammar and syntax of a particular programming language. 


Personally, I program best in the pascal language and not in Arduino C, so I am not really skilled to be able to locate syntax errors in Arduino.  As I do not have Arduino to test your code, I am not able to see where it is failing.


If the Nextion button is to have a state, you should be using the dual-state button component rather than the button component.  The dual-state button has a .val attribute (values of 0 and 1) that the button component lacks.


Thank you for your kind reply Patrick Maritin

finally I get the answer to use external push botton to control the button on nextion by using


this code


 nexSerial.print("click b1,1"); // command base on Nextion Instruction Set
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);

I try only button it work but for picture can not effect


my be can help you

Understand the click command.


click triggers the code in a components Touch event (1 for Touch Pressed, 0 for Touch Released)

which component? - every component has an .objname attribute.


So, if picture's name in .objname is p3 _and_ there is code in its Touch Released event, you trigger with

click p3,0


Not all components auto-refresh yet.  So to update the changes you made to picture p3

ref p3



Login or Signup to post a comment