2016-08-25 29 views
0

功能:防止ButtonState切換從高至低

按下前紅色圓頂按鍵(而不是2狀態的按鈕),串行監控將打印清單「0」,當紅色按下圓頂按鈕時,按鈕狀態將從低切換到高,因此串行監視器將打印一個「1」列表。

但是,當按鈕處於HIGH狀態時,串行監視器打印「1」,用戶將無法將按鈕狀態從HIGH切換到LOW。因此,按鈕只能在一段時間(25秒)後自動從高電平切換到低電平。

因此,正確的行爲

初始狀態打印=> 00000000000000(當用戶按下紅色圓頂按鍵=> buttonstate變化低到高)111111111111111111(當用戶按下按鈕,沒有任何反應) 111111111111111111(後25S延遲,buttonState將切換高到低)0000000000000

ISSUE:

此時,用戶可以在低電平到高電平之間切換,從高電平切換到低電平。意思是,流=> 00..000(用戶按下按鈕,切換低到高)111 ... 111(用戶按下按鈕,切換高到低)0000 ...

而且我不太確定如何使按鈕只能從低電平切換到高電平,但禁止按鈕從高電平切換到低電平。

含義當用戶按下按鈕時,它可以將按鈕狀態從「0」改變爲「1」,但當它處於「1」時不能改變按鈕狀態。

因此,我想要求一些幫助,這將允許以下正確的行爲。

感謝

代碼:

const int buttonPin = 2; //the number of the pushbutton pin 
const int Relay  = 4; //the number of the LED relay pin 

uint8_t stateLED = LOW; 
uint8_t  btnCnt = 1; 

int buttonState = 0; //variable for reading the pushbutton status 
int buttonLastState = 0; 
int outputState = 0; 

void setup() { 
    Serial.begin(9600); 
    pinMode(buttonPin, INPUT); 
    pinMode(Relay, OUTPUT); 
    digitalWrite(Relay, LOW); 
} 

void loop() { 

    // read the state of the pushbutton value: 
    buttonState = digitalRead(buttonPin); 
    // Check if there is a change from LOW to HIGH 
    if (buttonLastState == LOW && buttonState == HIGH) 
    { 
    outputState = !outputState; // Change outputState 
    } 
    buttonLastState = buttonState; //Set the button's last state 

    // Print the output 
    if (outputState) 
    { 
    switch (btnCnt++) { 
     case 100: 
     stateLED = LOW; 
     digitalWrite(Relay, HIGH); // after 10s turn on 
     break; 

     case 250: 
     digitalWrite(Relay, LOW); // after 20s turn off 
     //Toggle ButtonState to LOW from HIGH without user pressing the button 
     digitalWrite(buttonPin, LOW); 
     break; 

     case 252: // small loop at the end, to do not repeat the LED cycle 
     btnCnt--; 
     break;  
    } 

    Serial.println("1"); 
    }else{ 
    Serial.println("0"); 
    if (btnCnt > 0) { 
     // disable all: 
     stateLED = LOW; 
     digitalWrite(Relay, LOW); 
    } 
    btnCnt = 0; 
    } 

    delay(100); 
} 

回答

0

您需要設置outputState並讓它設置,直到將其25秒鐘後復位。如果按鈕仍然按下,然後就會對251

const int buttonPin = 2; //the number of the pushbutton pin 
const int Relay  = 4; //the number of the LED relay pin 

uint8_t stateLED = LOW; 
uint8_t  btnCnt = 1; 

bool outputState = false; 

void setup() { 
    Serial.begin(9600); 
    pinMode(buttonPin, INPUT); 
    pinMode(Relay, OUTPUT); 
    digitalWrite(Relay, LOW); 
} 

void loop() { 

    outputState |= digitalRead(buttonPin); // if pushButton is high, set outputState (low does nothing) 

    // Print the output 
    if (outputState) 
    { 
    switch (btnCnt++) { 
     case 100: 
     stateLED = LOW; 
     digitalWrite(Relay, HIGH); // after 10s turn on 
     break; 

     case 250: 
     digitalWrite(Relay, LOW); // after 20s turn off 
     //Toggle ButtonState to LOW from HIGH without user pressing the button 
     outputState = false; // reset state to low 
     break; 
     case 251: // loop (it might happen, if previous step sets outputState=false but button is still pressed -> no action) 
     --btnCnt; 
     outputState = false; 
     break; 
    } 

    Serial.println("1"); 
    }else{ 
    Serial.println("0"); 
    if (btnCnt > 0) { 
     // disable all: 
     stateLED = LOW; 
     digitalWrite(Relay, LOW); 
    } 
    btnCnt = 0; 
    } 

    delay(100); 
} 
+0

重申循環,這是爲了讓buttonState在高保持設置:「1」的時間在規定的期限,直到時間後(20秒)它會自動切換到低電平。其次,當buttonState在(20s)期間處於HIGH時,不會發生HIGH→LOW狀態的切換。我對嗎? – Luke