首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止ButtonState从高到低

防止ButtonState从高到低
EN

Stack Overflow用户
提问于 2016-08-25 15:53:26
回答 1查看 526关注 0票数 0

Functionality:

在按下红色圆顶按钮(不是2状态按钮)之前,串行监视器将打印列表“0”,当按下红色圆顶按钮时,按钮状态将从低到高切换,因此串行监视器将打印“1”列表。

然而,当按钮处于高状态时,串行监视器打印的“1”和用户将无法切换按钮状态从高到低。因此,按钮只能在延时(25s)后从高到低自动切换。

因此,正确的行为

初始状态打印=> 00000000000000(当用户按下红色圆顶按钮=>按钮低到高)111111111111111111(当用户按下按钮时,什么都不会发生)11111111111111(延迟25s后,buttonstate将切换到较高的值)00000000000

发行:

此时,用户可以在低到高和高到低之间切换。意思是,流程=> 00.000(用户按下按钮,低到高)111.111(用户按下按钮,切换到低)0000.

我不太确定如何使按钮只能从低到高切换,但禁用按钮切换从高到低。

这意味着当用户按下按钮时,它可以将按钮状态从"0“更改为"1”,但当按钮状态处于"1“时,则无法更改按钮状态。

因此,我请求提供一些帮助,以使以下行为正确。

谢谢

代码:

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-25 18:11:14

您需要设置outputState,并让它设置,直到它将在25秒后被重置。如果按钮仍然按下,它将在251上循环。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39149751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档