简体   繁体   English

使用中断来处理arduino中的布尔值

[英]Using an interrupt to handle a boolean in arduino

I've researched a bit, and I've ran into a couple of examples of how buttons are used as interrupts. 我进行了一些研究,并且遇到了一些有关如何将按钮用作中断的示例。 However, the design I'm trying to implement uses analog sensors. 但是,我要实现的设计使用了模拟传感器。 Right now, what I want to do is to have my analog sensors flag a boolean to tell the interrupt to execute, not a button. 现在,我想做的是让我的模拟传感器标记一个布尔值来告知中断要执行,而不是一个按钮。 How would I do so? 我该怎么办?

This is what I have thought up based on what I researched: 这是我根据研究发现所想到的:

    boolean isWall;

    attachInterrupt(isWall, interruptFunction, RISING);

    void loop() {
        if(analogSensor.response > 450) {
                isWall = true;
        }
        normalExecution();  // what it normally does if isWall is false 
    }

    void interruptFunction() {
          // code implementation
          isWall = false;  // set isWall back to false after executing interruptFunction
    }

    void normalExecution() {
          // foo
    }

Can someone verify? 有人可以验证吗?

Perhaps I am completely wrong but it has always been my understanding that interrupts in Arduino are based on the voltage seen by a pin - ie they are hardware interrupts. 也许我是完全错误的,但是我一直以来的理解是,Arduino中的中断基于引脚所看到的电压 -即它们是硬件中断。 Consequently, while your code is correct for a hardware interrupt, it will not work for a change in a variable. 因此,尽管您的代码对于硬件中断来说是正确的,但对于变量的更改而言,它是行不通的。

That said, this link and this link discuss Analog Comparator methods for accomplishing what you are trying to do. 就是说,此链接和此链接讨论了用于完成您要执行的操作的Analog Comparator方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM