我正在使用Arduino设计发电机的自动转换。当EB电源关闭时,我需要打开发电机启动电机五秒钟,然后关闭它然后接通接触器C2。当发电机接通时,如果EB电源进入,我必须关闭接触器C2,接通发电机停止电机五秒钟然后接通接触器C2。我的代码是
int ebin = 2; // EB input
int gin = 3; // Generator input
int GonR1 = 4; // Generator ON (Relay 1) starting motor
int GoffR2 = 5; // Generator OFF (Relay 2) stopping motor
int C1 = 6; // Contactor 1
int C2 = 7; //Contactor 2
int e = 0;
int g = 0;
void setup()
{
pinMode(ebin, INPUT);
pinMode(gin, INPUT);
pinMode(GonR1, OUTPUT);
pinMode(GoffR2, OUTPUT);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
}
void loop()
{
e = digitalRead(ebin);
g = digitalRead(gin);
if (e == LOW) {
digitalWrite(GoffR2, LOW);
digitalWrite(C1, LOW);
delay(4000);
digitalWrite(GonR1, HIGH);
delay(5000);
digitalWrite(GonR1, LOW);
digitalWrite(C2, HIGH);
}
else if (e == HIGH && g == LOW) {
digitalWrite(GoffR2, LOW);
digitalWrite(GonR1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C1, HIGH);
}
else if (g == HIGH && e == HIGH) {
digitalWrite(GonR1, LOW);
delay(5000);
digitalWrite(C2, LOW);
digitalWrite(GoffR2, HIGH);
delay(4000);
digitalWrite(GoffR2, LOW);
digitalWrite(C1, HIGH);
}
}
e==LOW
时,我想要这部分
digitalWrite(GoffR2, LOW);
digitalWrite(C1, LOW);
delay(4000);
digitalWrite(GonR1, HIGH);
delay(5000);
digitalWrite(GonR1, LOW);
只执行一次,我想要这个部分
digitalWrite(C2, HIGH);
连续执行,直至条件失败。
但实际情况是当条件(e==LOW
)为真时,整个循环会连续执行。这将使发电机启动马达每4秒运行一次。所以我需要帮助来解决这个问题。
答案 0 :(得分:0)
我没有尝试理解您的所有代码,但这证明了您提出的问题:
你需要保持先前的e
状态,如果在调用低if语句时它不是LOW
,那么你需要运行一次性代码:
int e = 0;
int e_previous = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 10; i++) {
if (e == 0) {
if (e_previous != 0) {
Serial.println("e LOW once off");
e_previous = 0;
}
Serial.println("e LOW continual");
}
else {
Serial.println("e HIGH continual");
e_previous = 1;
}
delay(2000);
// This emulates e changing state, it isn't important to understaning the code.
if (i == 3)
e = 1;
else if (i == 6)
e = 0;
}
}
会给你:
e LOW once off
e LOW continual
e LOW continual
e LOW continual
e LOW continual
e HIGH continual
e HIGH continual
e HIGH continual
e LOW once off
e LOW continual
e LOW continual
e LOW continual
int ebin = 2; // EB input
int gin = 3; // Generator input
int GonR1 = 4; // Generator ON (Relay 1) starting motor
int GoffR2 = 5; // Generator OFF (Relay 2) stopping motor
int C1 = 6; // Contactor 1
int C2 = 7; //Contactor 2
int e = 0;
int e_previous = High; //Change the initial value to whatever makes sense
int g = 0;
void setup() {
pinMode(ebin, INPUT);
pinMode(gin, INPUT);
pinMode(GonR1, OUTPUT);
pinMode(GoffR2, OUTPUT);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
}
void loop() {
e = digitalRead(ebin);
g = digitalRead(gin);
if (e == LOW)
{
if (e_previous != LOW) { //This part will be run once
digitalWrite(GoffR2, LOW);
digitalWrite(C1, LOW);
delay(4000);
digitalWrite(GonR1, HIGH);
delay(5000);
digitalWrite(GonR1, LOW);
e_previous = LOW; // This stops this inner if from being run again
}
digitalWrite(C2, HIGH);
}
else if (e == HIGH && g == LOW)
{
digitalWrite(GoffR2, LOW);
digitalWrite(GonR1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C1, HIGH);
e_previous = HIGH; // Reset it
}
else if (g == HIGH && e == HIGH)
{
digitalWrite(GonR1, LOW);
delay(5000);
digitalWrite(C2, LOW);
digitalWrite(GoffR2, HIGH);
delay(4000);
digitalWrite(GoffR2, LOW);
digitalWrite(C1, HIGH);
e_previous = HIGH; // Reset it
}
else { // just in case
e_previous = HIGH; // Reset it
}
}