0

I am having an issue with my Nextion display. I have uploaded the following code via Arduino IDE to my Arduino MEGA 2560.

My Nextion Editor shows the button as ID:9 on page0 named floor. I am not entirely sure if the code below is correct. I had one issue where the upload from the laptop to the Mega wouldn't finish, but I managed to bypass that issue by disconnecting the Nextion and swapping RX and TX cables from Nextion while the upload was in progress. Not sure how else I would fix that issue? (If anyone has any ideas haha)

All that is meant to happen is when the dual button state is changed, the onboard LED_BUILTIN is set to HIGH and if in its second state, is set to LOW.

CODE

#include "Nextion.h"


NexDSButton bt0 = NexDSButton(0, 9, "floor");


int ledPin = LED_BUILTIN;


char buffer[100] = {0};


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



void bt0PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b0PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));

    if(dual_state) 
    {
        digitalWrite(ledPin, HIGH);
    }
    else
    {
        digitalWrite(ledPin, LOW);
    }
}

void setup(void)
{    
    nexInit();

    bt0.attachPop(bt0PopCallback, &bt0);

    dbSerialPrintln("setup done"); 
    pinMode(ledPin, OUTPUT);
}

void loop(void)
{   
    nexLoop(nex_listen_list);
}

My issue is that the MEGA is identifying the touch event (as the TX LED on the MEGA flashes) however it is not turning on the MEGA's onboard built-in LED.

Any help would be greatly appreciated!

  • You forgot to explain what your issue is. – Dmitry Grigoryev Jul 9 '18 at 13:17
  • I do apologise, I will update the initial question. My issue is that the Mega is identifying the touch events (the TX onboard LED flashes) however the button on the Nextion is not activating the Mega's onboard LED light :/ – Jesse Jul 9 '18 at 13:19
  • Did you select a hardware serial port for nexSerial? Do you see the text "setup done"? Do you see the text "b0PopCallback"? Did you enable the event in the nextion editor? Please update your initial question with more information. – Jot Jul 9 '18 at 14:06
  • I have done no coding in the Nextion editor as I thought Arduino IDE handles all the coding. How would I go about enabling the event in Nextion? – Jesse Jul 10 '18 at 0:35
  • I will admit that I did load this code from a sample sketch provided by Nextion, however I ONLY used the code and never looked into the .HMI file. I might browse over that and try and understand what you mean by enabling the event in Nextion :) – Jesse Jul 10 '18 at 0:40
0

This is part of the code I have been using for an extraction fan, The button text is tested and if the button matches the command the fan is turned on or off and the button text is changed accordingly. Hope this helps.

void b19PopCallback(void *ptr) //Extraction Fan
{
  NexButton *btn = (NexButton *)ptr;
  memset(buffer, 0, sizeof(buffer));
  btn->getText(buffer, sizeof(buffer));

  if (strcmp(buffer, "ON"))
  {
    //COMES BACK AS A FALSE (0) IF BOTH STRINGS MATCH
    digitalWrite(b_motor, LOW);
    strcpy(buffer, "ON");
    btn->setText(buffer);
  }

  else 
  {
    digitalWrite(b_motor, HIGH);
    strcpy(buffer, "OFF");
    btn->setText(buffer);
  }  

}
  • strcmp doesn't return true or false so your comment about it returning FALSE if both strings match is a bit misleading. See strcmp - strcmp is designed to return a number < 0 if string1 is less than string2, 0 if they are the same, and > 0 if string1 is greater than string2 (in the collating sequence). – Nick Gammon Jan 10 at 6:11

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.