简体   繁体   中英

Nextion Touch Screen Arduino Mega 2560 Turning a relay off and on

I am having a bit of a problem guys.

I am trying to use a Nextion touch screen to control my off road lights. I am using a Arduino Mega 2560 board. A 8 module relay board. and the Nextion screen. The sketch I have written everything works except after I fire the relay I am unable to turn it off. In the sketch I am also posting in the first button toggle I am having the state of the pin read then based on the state turn it off or on. Well it now does nothing. It neither fires the relay or turns it off.

In the rest of the button toggles I just have it fire the relay when the button toggles.

The message that is called for in the button toggles does not change based on button state. Otherwise I would use that to turn the relay off.

Guys I am at a loss. Any guidance would be greatly appreciated.

/* This is my Sketch for the Touch Screen interface for the relay box that will be installed in my Jeep
        Robert L. Wardecker

        This code is in public domain
    */

    #include <SoftwareSerial.h>
    #include <doxygen.h>
    #include <Nextion.h>


    const int relayaPin = 52;
    const int relaybPin = 53;
    const int relaycPin = 50;
    const int relaydPin = 51;
    int val = 0;


    SoftwareSerial nextion(10, 11);// Nextion TX to pin 10 and RX to pin 11 of Arduino

    Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

    boolean button1State;
    boolean button2State;
    boolean button3state;
    boolean button4state;
    boolean button5state;

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      myNextion.init(); // send the initialization commands for Page 0

      pinMode(relayaPin, OUTPUT);
      pinMode(relaybPin, OUTPUT);
      pinMode(relaycPin, OUTPUT);
      pinMode(relaydPin, OUTPUT);

    }

    void loop() {
      // put your main code here, to run repeatedly:

      String message = myNextion.listen(); //check for message
      if (message == "65 0 1 1 ffff ffff ffff") {
        myNextion.buttonToggle(button1State, "b0", 0, 2);
      }
     val = digitalRead(relayaPin);
     Serial.print(val);

      if (val == HIGH) {
        // turn Relay on:
        digitalWrite(relayaPin, HIGH);
      } else {
        // turn Relay off:
        digitalWrite(relayaPin, LOW);
      }

      if (message == "65 0 2 1 ffff ffff ffff") {
        myNextion.buttonToggle(button2State, "b1", 0, 2);
        digitalWrite(relaybPin, HIGH);
      }
      if (message == "65 0 3 1 ffff ffff ffff") {
        myNextion.buttonToggle(button3state, "b2", 0, 2);
        digitalWrite(relaycPin, HIGH);
      }
      if (message == "65 0 4 1 ffff ffff ffff") {
        myNextion.buttonToggle(button3state, "b3", 0, 2);
        digitalWrite(relaydPin, HIGH);
      }
      if (message == "65 0 5 1 ffff ffff ffff") {
        myNextion.buttonToggle(button3state, "b4", 0, 2);
        digitalWrite(relayaPin, HIGH);
        digitalWrite(relaybPin, HIGH);
        digitalWrite(relaycPin, HIGH);
        digitalWrite(relaydPin, HIGH);
      }

    }

There are multiple issues with this:

1) Your first check for "65 0 1 1 ffff ffff ffff" is only toggling the button on your Nextion screen, it isn't actually doing anything with the relayaPin . Once that check has completed, then your code, regardless of the check, will toggle your relay a because it isn't encapsulated in the if statement. What you want to do is this:

  if (message == "65 0 1 1 ffff ffff ffff") {
    myNextion.buttonToggle(button1State, "b0", 0, 2);

    val = digitalRead(relayaPin);
    Serial.print(val);

    if (val == HIGH) {
      // turn Relay on:
      digitalWrite(relayaPin, HIGH);
    } else {
      // turn Relay off:
      digitalWrite(relayaPin, LOW);
    }
 }

This will only toggle the relayA if you've pressed the button.

2) For all the other relay check statements, you aren't checking its current state like you do in the above point. Just setting it HIGH again won't toggle it, if it is already on, it'll only ever stay on at that point. There is no exit criteria. You need to do the check like you do for the first one.

3) For the love of god, use some #define RELAY_A_MESSAGE "65 0 1 1 ffff ffff ffff" kind of deal to improve readability. Then you can just use if( message == RELAY_A_MESSAGE ) statement instead of this weird ascii garbage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM