2017-03-16 39 views
0

我在設計一個使用Arduino的發生器的自動轉換。當EB電源關閉時,我需要接通發電機啓動電機五秒鐘,然後將其關閉,然後打開接觸器C2。當發電機處於ON狀態,現在如果EB電源進入,我必須關閉接觸器C2,接通發電機停止電機5秒鐘,然後接通接觸器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

我沒有試圖理解所有的代碼,但是這表明你已經問到這個問題:你需要保持e以前的狀態

,如果它不是LOW當低if語句叫那麼你需要關閉代碼運行一次:

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 

Correcti你的代碼:

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