c++ - 即使初始要求为真,也不会进入 for 循环

标签 c++ for-loop arduino-c++

我有以下函数,其中有一个 for 循环。该代码在 Arduino 上运行,Serial.print 函数显示已使用正确的输入值正确输入该函数。但是没有进入 for 循环。有谁知道为什么?

void openvalveCold(int steps){
    Serial.println(steps);

    // Steps is confimed to be 200. 
    digitalWrite(sleep1,HIGH);

    for (antalsteg = 0; antalsteg == steps; antalsteg++)
    {  
        Serial.println("2");  

        //digitalWrite(dir1,HIGH);
        digitalWrite(stepp1,HIGH);

        delay(25);
        digitalWrite(stepp1,LOW);
        delay(25);
        Serial.println(antalsteg);

        nr_of_steps_cold++;
    }
}

void loop{
    // calling on function
    openvalveCold(200);
}

最佳答案

for 循环通常是这样构造的:

for(init counter; condition; increase counter)

您做出了(错误的)假设,即循环直到条件为真。那是错误的。它循环它是真的。更改为:

for (antalsteg = 0; antalsteg < steps; antalsteg++)

关于c++ - 即使初始要求为真,也不会进入 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030337/

相关文章:

java - 如何将其变成帕斯卡三角形而不是直角三角形?

c++ - 访问多维 std::array 元素时出现问题

c++ - 没有括号时,输入结尾处的预期 '}'

java - 当我从Java代码执行C++时未显示任何输出

c++ - std::exp 给出与复数的 MATLAB exp 不同的结果

c++ - 在不释放内存的情况下销毁 std::vector

c++ - 程序错误 : for replacing spaces with "%20"

c - 替代 for 循环语法

python - 预测一个循环的剩余时间

arduino - 制作 10 个 LED 灯 block