使用瞬时按钮的双稳态单按切换,保持时无连续切换 [英] bistable single push toggle using momentary pushbutton, NO contiguous toggle upon hold

查看:92
本文介绍了使用瞬时按钮的双稳态单按切换,保持时无连续切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在按住按钮时停止连续状态更改?

How to stop contiguous state change upon button hold?

    const int btn = 5;
    const int ledPin = 3;
    int ledValue = LOW;

    void setup(){
        Serial.begin(9600);
        pinMode(btn, INPUT_PULLUP);
        pinMode(ledPin, OUTPUT);
    }

    void loop ()
    {
        if (digitalRead(btn) == LOW)
        delay(100);
    {
        ledValue = !ledValue;
        delay(100);
        digitalWrite(ledPin, ledValue);
        delay(100);    
        Serial.println(digitalRead(ledPin));  
      }
     }

当我按住按钮时,我会收到连续的状态更改.我想按下按钮并接收单个状态更改,而在保留-或意外保留-我不想更改状态.

When I hold the button I receive contiguous state change. I want to press button and receive single state change, upon hold -- or accidental hold -- I would not like to change state.

更多地通过触发器的结果来寻找边缘检测的效果.

More looking for effect of edge detection with the result of flip flop.

此代码上还有更多开发要做,但这是第一阶段. 最终,我将把FOR语句集成到循环中,也许还会将SWITCH(case)语句集成到循环中.

There is more development to be done on this code but this is the first stage. Eventually I will integrate a FOR statement into the loop and perhaps a SWITCH(case) statement.

基本上,我需要一次按一下按钮即可切换输出引脚,我还希望-在将来-能够通过使用FOR来根据特定的输入条件在可能的输出状态之间循环和SWITCH(case)一起.那是一个不同的职位.除非您也可以为该问题找到解决方案.

Basically I need to be able to toggle output pins with a single momentary push, I also would like -- in the FUTURE -- to be able to cycle through possible output states based on specific input conditions, by way of using FOR and SWITCH(case) together. That is a different post. Unless you can surmise a solution for that problem as well.

推荐答案

最简单的方法是添加一个保存按钮状态的变量.

The easiest way is to add a variable that holds the state of the button.

当您按下按钮时,该变量将设置为true,并且您要运行的代码.当该变量为true时,您编写的代码将不会再次执行.释放按钮时,变量将设置为false,因此,下次按下按钮将再次执行您的代码.

When you press the button, that variable is set to true and the code you want runs. While that variable is true, the code you wrote will not be executed a second time. When you release the button, the variable gets set to false, so a next button press will have your code executed again.

代码:

bool isPressed = false; // the button is currently not pressed

void loop ()
{
    if (digitalRead(btn) == LOW) //button is pressed
    {
       if (!isPressed) //the button was not pressed on the previous loop (!isPressed means isPressed == FALSE)
       {
         isPressed = true; //set to true, so this code will not run while button remains pressed
         ledValue = !ledValue;
         digitalWrite(ledPin, ledValue); 
         Serial.println(digitalRead(ledPin));  
       }
    }
    else
    {
       isPressed = false; 
       // the button is not pressed right now, 
       // so set isPressed to false, so next button press will be handled correctly
    }
 } 

添加了第二个示例

const int btn = 5;
const int ledPin = 3;
int ledValue = LOW;
boolean isPressed = false; 

void setup(){
    Serial.begin(9600);
    pinMode(btn, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
}

void loop ()
{
  if (digitalRead(btn) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
  {
    isPressed = true;  //set to true, so this code will not run again until button released
    doMyCode(); // a call to a separate function that holds your code
  } else if (digitalRead(btn) == HIGH)
  {
    isPressed = false; //button is released, variable reset
  }
}

void doMyCode() {
  ledValue = !ledValue;
  digitalWrite(ledPin, ledValue); 
  Serial.println(digitalRead(ledPin));
}

这篇关于使用瞬时按钮的双稳态单按切换,保持时无连续切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆