【发布时间】:2016-05-16 16:17:20
【问题描述】:
伙计们,我遇到了一些问题。
我正在尝试使用 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);
}
}
【问题讨论】: