简体   繁体   English

在C ++中对布尔进行NOT运算符

[英]NOT operator on Boolean in c++

I'm trying to use this Arduino code in my program, the LED will stay on for 5 sec then turns itself off 我正在尝试在程序中使用此Arduino代码,LED指示灯将保持点亮5秒钟,然后自行关闭

#include <elapsedMillis.h>

int led = 13 ;
elapsedMillis timer0 ;
#define interval 5000
boolean timer0Fired ;

// the setup routine runs once when you press reset:
void setup() 
{
    pinMode(led, OUTPUT);
    digitalWrite(led, HIGH);
    timer0Fired = false;
    timer0 = 0; // clear the timer at the end of startup
}

void loop()
{
    if ((!timer0Fired) && (timer0 > interval))
    {
        timer0Fired = true;      // don't execute this again
        digitalWrite(led, LOW);  // turn led off after 5 sec
    }
}

I don't understand how the if statement works inside the loop, !timer0Fired should evaluates to 1 but when I print it out it's 0, so when timer0 exceeds the interval the if statement should evaluates to be false, I tried it on a UNO and it works. 我不明白if语句如何在循环内工作,!timer0Fired应该计算为1,但是当我将其打印出来为0时,因此,当timer0超出间隔时,if语句应该计算为false,我在UNO上尝试过而且有效。

!timer0Fired should evaluates to 1 but when I print it out it's 0 !timer0Fired应该计算为1,但当我将其打印出来时为0

When you print it, do you print "!timer0Fired" or "timer0Fired"? 打印时,是否打印“!timer0Fired”或“ timer0Fired”? It would explain the output. 它将解释输出。

so when timer0 exceeds the interval the if statement should evaluates to be false 因此,当timer0超过时间间隔时,if语句的计算结果应为false

Currently, "(timer0 > interval)" evaluates to true when timer0 exceeds the interval, not the contrary. 当前,当timer0超过该间隔时,“(timer0> interval)”的计算结果为true,相反。 Isn't it correct? 是不是这样

This is just a basic programming logic. 这只是基本的编程逻辑。

Imagine that you want to count to 5 and call some function, eg DoSomething , in and endless loop. 假设您要计数到5并在无限循环中调用某些函数,例如DoSomething

int count = 0;
bool called = false; // DoSomething not called yet

while (true) // main loop
{
    ++count;

    if (count == 5)
    {
        // the count is 5
        if (called == false)
        {
            // function not called yet, call it!
            called = true;
            DoSomething();
        }
    }
}

This might seem as a non-sense in a way. 在某种程度上,这似乎是胡说八道。 Just wait for it... 等一下...

However, the problem you're facing is that you don't have a simple counter like mine count . 但是,您面临的问题是您没有像mine count这样的简单计数器。 Instead, you're using a timer that could be a few milliseconds off and you still want the code to execute even if it's late. 取而代之的是,您使用的计时器可能会延迟几毫秒,即使延迟了,您仍然希望代码能够执行。

For example: 例如:

int count = 0;
bool called = false; // DoSomething not called yet

while (true) // main loop
{
    count += 2; // emulate counter being late

    if (count >= 5) // note that count == 5 will be always false here...
    {
        // the count is 5 or more
        if (called == false)
        {
            // function not called yet, call it!
            called = true;
            DoSomething();
        }
    }
}

Now that completely inverses the value of the if s. 现在,这完全颠倒了if s的值。 This way, the application almost always passes the first if but only once the second. 这样,应用程序几乎总是通过第一个if ,但只有一次第二。 In order to optimize this, you can just swap the if s: 为了对此进行优化,您可以仅交换if s:

if (called == false)
{
    if (count >= 5)
    {
        called = true;
        DoSomething();
    }
}

And now, as you probably know, these nested if statements can be easily grouped into one using && operator. 现在,您可能已经知道,可以使用&&运算符将这些嵌套的if语句轻松分组为一个。 Also, called == false can be made less verbose by using !called . 同样,可以通过使用!called named来使named called == false变得更简洁。 This in the end results in 最终导致

while (true) // main loop
{
    count += 2; // emulate counter being late

    if (!called && count >= 5)
    {
        // function not called yet and count is already 5 or more
        called = true;
        DoSomething();
    }
}

So what is going on is: 所以发生了什么事:
a) you want to execute a piece of code only once 一)要执行的代码只有一次
- represented by bool called = false; if (!called) called = true; -以bool called = false; if (!called) called = true; bool called = false; if (!called) called = true; logic or timer0fired in your code for the matter 在您的代码中timer0fired逻辑或timer0fired

b) you want to delay the call until some timer reaches a certain amount b)您想将通话延迟到某个计时器达到一定数量
- represented by count >= 5 or timer >= interval in your example -在您的示例中以count >= 5timer >= interval

tl;dr tl; dr
Your code is working correctly. 您的代码正常工作。

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

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