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