Nextion触摸屏Arduino Mega 2560关闭和打开继电器

时间:2016-05-16 16:17:21

标签: c++ arduino

我遇到了一些问题。

我正在尝试使用Nextion触摸屏来控制我的越野灯。我使用的是Arduino Mega 2560主板。一个8模块继电器板。和Nextion屏幕。我写过的草图一切正常,除非我开火后继电器我无法将其关闭。在草图中我也在第一个按钮切换中发布我正在读取引脚的状态然后根据状态将其关闭或打开。好吧现在什么也没做。它既不会触发继电器也不会将其关闭。

在按钮的其余部分切换时,我只需在按钮切换时触发继电器。

按钮切换时调用的消息不会根据按钮状态而改变。否则我会用它来关闭继电器。

伙计我不知所措。任何指导将不胜感激。

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

    }

1 个答案:

答案 0 :(得分:0)

这有多个问题:

1)你第一次检查&#34; 65 0 1 1 ffff ffff ffff&#34;只是切换Nextion屏幕上的按钮,它实际上并没有对relayaPin做任何事情。一旦检查完成,那么无论检查什么,您的代码都会切换您的继电器,因为它没有封装在if语句中。你想要做的是:

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

如果您按下按钮,这只会切换继电器A.

2)对于所有其他继电器检查语句,您不会像在上面那样检查其当前状态。只需将其设置为HIGH就不会切换它,如果它已经打开,它只会保持在那一点上。没有退出标准。你需要像第一个那样进行检查。

3)为了爱上帝,使用一些#define RELAY_A_MESSAGE "65 0 1 1 ffff ffff ffff"种交易来提高可读性。然后你可以使用if( message == RELAY_A_MESSAGE )语句而不是这个奇怪的ascii垃圾。