Off-Road Light Control Panel

I am trying to use an Arduino Mega with a Nextion Touch Screen as the switch input. The Touch screen will trigger the Arduino to send a signal to a relay board to fire the relays thus turning on my off-road lights. I have kinda got a sketch written but am at a loss as to how to fire the appropriate outputs to fire the relays. I am not looking for anyone to write the sketch. I want to do that myself, all I would like is some pointers and hopefully some resources as to where I can research and figure it out myself. Please take a look at my sketch and tell me what you think. Am I headed in the right direction? Any and all pointers greatly appreciated. I am new at this and am learning as I go. I want to get as good as I possibly can.

/* Off-Road Light Relay Box

    This program is for a touch screen input in place of mechanical switches to actuate several Off-Road Lights.

    Program is Propery Of Robert L. Wardecker
*/

#include <Arduino.h>
#include <doxygen.h>
#include <NexButton.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDownload.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexWaveform.h>
#include <SoftwareSerial.h>
#include <doxygen.h>
#include <NexButton.h>


SoftwareSerial nextion(19, 18); // Nextion TX to pin 19 and RX to pin 18 of Arduino
int myPins[]  = {2, 3, 4, 5, 6};//Pin 2 52 in Light Bar, 3 6in Light Bars, 4 Pods, 5 Rock Lights, 6 open

/*
   Declare a dual state button object [page id:0,component id:1, component name: "bt0"].
*/
NexDSButton bt1 = NexDSButton(1, 1, "bt1");//52 inch bar

NexDSButton bt2 = NexDSButton(1, 2, "bt2");//4 inch pods

NexDSButton bt3 = NexDSButton(1, 3, "bt3");//Rock Lights

NexDSButton bt4 = NexDSButton(1, 4, "bt4");//6 in Pods

NexDSButton bt0 = NexDSButton(1, 6, "bt0");//all on

NexText t1 = NexText(1, 1, "t1");

NexText t2 = NexText(1, 2, "t2");

NexText t3 = NexText(1, 3, "t3");

NexText t4 = NexText(1, 4, "t4");

NexText t0 = NexText(1, 6, "t0");


char buffer[100] = {0};



NexTouch *nex_listen_list[] =
{
  &bt1,
  &bt2,
  &bt3,
  &bt4,
  &bt0,
  NULL
};
/*
   Dual state button component pop callback function.
*/
void bt1PopCallback(void *ptr)
{
  uint32_t dual_state;
  NexDSButton *btn = (NexDSButton *)ptr;
  dbSerialPrintln("b1PopCallback");
  dbSerialPrint("ptr=");
  dbSerialPrintln((uint32_t)ptr);
  memset(buffer, 0, sizeof(buffer));

  /* Get the state value of dual state button component . */
  bt1.getValue(&dual_state);
  if (dual_state)
  {
    t1.setText("");
  }
  else
  {
    t1.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt2.getValue(&dual_state);
  if (dual_state)
  {
    t2.setText("");
  }
  else
  {
    t2.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt3.getValue(&dual_state);
  if (dual_state)
  {
    t3.setText("");
  }
  else
  {
    t3.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt4.getValue(&dual_state);
  if (dual_state)
  {
    t4.setText("");
  }
  else
  {
    t4.setText("");
  }
}

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));

  /* Get the state value of dual state button component . */
  bt0.getValue(&dual_state);
  if (dual_state)
  {
    t0.setText("");
  }
  else
  {
    t0.setText("");
  }
}

void setup() {
  // put your setup code here, to run once:

  /* Set the baudrate which is for debug and communication with Nextion screen. */
  nexInit();

  /* Register the pop event callback function of the dual state button component. */
  bt1.attachPop(bt1PopCallback, &bt1);
  bt2.attachPop(bt2PopCallback, &bt2);
  bt3.attachPop(bt3PopCallback, &bt3);
  bt4.attachPop(bt4PopCallback, &bt4);
  bt0.attachPop(bt0PopCallback, &bt0);

  dbSerialPrintln("setup done for now Bob");

}

void loop() {

  nexLoop(nex_listen_list);

}

The number of libraries included is obscuring what your current code does for me. Presumably somewhere in there you are testing button states but where ?

UKHeliBob:
The number of libraries included is obscuring what your current code does for me. Presumably somewhere in there you are testing button states but where ?

Yea there are a ton of libraries. I am posting the code again as I cut down on the number of libraries. Any Pointers would be greatly appreciated.

This is where I am testing button states;

/*
   Dual state button component pop callback function.
*/
void bt1PopCallback(void *ptr)
{
  uint32_t dual_state;
  NexDSButton *btn = (NexDSButton *)ptr;
  dbSerialPrintln("b1PopCallback");
  dbSerialPrint("ptr=");
  dbSerialPrintln((uint32_t)ptr);
  memset(buffer, 0, sizeof(buffer));

  /* Get the state value of dual state button component . */
  bt1.getValue(&dual_state);
  if (dual_state)
  {
    t1.setText("");
  }
  else
  {
    t1.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt2.getValue(&dual_state);
  if (dual_state)
  {
    t2.setText("");
  }
  else
  {
    t2.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt3.getValue(&dual_state);
  if (dual_state)
  {
    t3.setText("");
  }
  else
  {
    t3.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt4.getValue(&dual_state);
  if (dual_state)
  {
    t4.setText("");
  }
  else
  {
    t4.setText("");
  }
}

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));

  /* Get the state value of dual state button component . */
  bt0.getValue(&dual_state);
  if (dual_state)
  {
    t0.setText("");
  }
  else
  {
    t0.setText("");
  }
}
/* Off-Road Light Relay Box

    This program is for a touch screen input in place of mechanical switches to actuate several Off-Road Lights.

    Program is Propery Of Robert L. Wardecker
*/

#include <Arduino.h>
#include <doxygen.h>
#include <NexButton.h>
#include <NexConfig.h>
#include <NexDualStateButton.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexText.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <SoftwareSerial.h>


SoftwareSerial nextion(19, 18); // Nextion TX to pin 19 and RX to pin 18 of Arduino
int myPins[]  = {2, 3, 4, 5, 6};//Pin 2 52 in Light Bar, 3 6in Light Bars, 4 Pods, 5 Rock Lights, 6 open

/*
   Declare a dual state button object [page id:0,component id:1, component name: "bt0"].
*/
NexDSButton bt1 = NexDSButton(1, 1, "bt1");//52 inch bar
NexDSButton bt2 = NexDSButton(1, 2, "bt2");//4 inch pods
NexDSButton bt3 = NexDSButton(1, 3, "bt3");//Rock Lights
NexDSButton bt4 = NexDSButton(1, 4, "bt4");//6 in Pods
NexDSButton bt0 = NexDSButton(1, 6, "bt0");//all on
NexText t1 = NexText(1, 1, "t1");
NexText t2 = NexText(1, 2, "t2");
NexText t3 = NexText(1, 3, "t3");
NexText t4 = NexText(1, 4, "t4");
NexText t0 = NexText(1, 6, "t0");


char buffer[100] = {0};


NexTouch *nex_listen_list[] =
{
  &bt1,
  &bt2,
  &bt3,
  &bt4,
  &bt0,
  NULL
};
/*
   Dual state button component pop callback function.
*/
void bt1PopCallback(void *ptr)
{
  uint32_t dual_state;
  NexDSButton *btn = (NexDSButton *)ptr;
  dbSerialPrintln("b1PopCallback");
  dbSerialPrint("ptr=");
  dbSerialPrintln((uint32_t)ptr);
  memset(buffer, 0, sizeof(buffer));

  /* Get the state value of dual state button component . */
  bt1.getValue(&dual_state);
  if (dual_state)
  {
    t1.setText("");
  }
  else
  {
    t1.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt2.getValue(&dual_state);
  if (dual_state)
  {
    t2.setText("");
  }
  else
  {
    t2.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt3.getValue(&dual_state);
  if (dual_state)
  {
    t3.setText("");
  }
  else
  {
    t3.setText("");
  }
}

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

  /* Get the state value of dual state button component . */
  bt4.getValue(&dual_state);
  if (dual_state)
  {
    t4.setText("");
  }
  else
  {
    t4.setText("");
  }
}

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));

  /* Get the state value of dual state button component . */
  bt0.getValue(&dual_state);
  if (dual_state)
  {
    t0.setText("");
  }
  else
  {
    t0.setText("");
  }
}

void setup() {
  // put your setup code here, to run once:

  /* Set the baudrate which is for debug and communication with Nextion screen. */
  nexInit();

  /* Register the pop event callback function of the dual state button component. */
  bt1.attachPop(bt1PopCallback, &bt1);
  bt2.attachPop(bt2PopCallback, &bt2);
  bt3.attachPop(bt3PopCallback, &bt3);
  bt4.attachPop(bt4PopCallback, &bt4);
  bt0.attachPop(bt0PopCallback, &bt0);

  dbSerialPrintln("setup done for now Bob");

}

void loop() {

  nexLoop(nex_listen_list);

}

  bt1.getValue(&dual_state);This looks like it gets the current state of button 1. If that is true then what value does the function return in each state ? Probably 0 or 1 but if you print it and find out then you can use the values to activate the relay(s)

Hello,
This is my first post here! I'm new to Arduino programming.
I try to set the initial state of a NexDSButton in function setup (). The button is declared as:

NexDSButton ndsb1 = NexDSButton (2.1 "bt0");

Also, we have defined the function:

void ndsb1PopCallback (void * ptr){....}

where I receiving message by pressing the button on the screen.
I tried to set the initial state of the button like this:

void setup(void)
{
nexInit();
ndsb1.attachPop(ndsb1PopCallback, &ndsb1);
[b]ndsb1.setValue(1);[/b] //I need to be pressed as default!!!!
}

but no change in the status button (as pressed) ... What am I doing wrong?
Thank you!

What am I doing wrong?

Not posting your whole program.
Trying to use bold tags inside code tags

That's all:

#include "NexText.h"
#include "NexDualStateButton.h"

NexText t1 = NexText(2, 2, "t0");

NexDSButton ndsb1 = NexDSButton(2,1,"bt0");

char buffer[10] = {0};

NexTouch *nex_Listen_List[] = 
{
    &ndsb1,
    NULL
};

const int buzzer  =  2; 

void Buzz()
 {
  digitalWrite(buzzer,HIGH);
  delay(100);
  digitalWrite(buzzer,LOW);
 }
 
void ndsb1PopCallback(void *ptr)
{ 
  if (ptr == &ndsb1)
  {
   Buzz();
   uint32_t dual_state; 
   ndsb1.getValue(&dual_state);
   if(dual_state == 1) {
     t1.setText("1");
   }
   else{
    t1.setText("0");
   }  
  } 
}

void setup(void)
{
  pinMode(buzzer,OUTPUT);
  Buzz();
  nexInit();
  ndsb1.attachPop(ndsb1PopCallback, &ndsb1);
  indsb1.setValue(1);
}

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