0

我有以下函数,里面有一个 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);
}
4

2 回答 2

4

A for loop is usually constructed like this:

for(init counter; condition; increase counter)

You have made the (false) assumption that it loops until the condition is true. That's wrong. It loops while it is true. Change to:

for (antalsteg = 0; antalsteg < steps; antalsteg++)
于 2019-05-07T21:01:40.887 回答
3

未进入循环,因为循环开始时条件为假:

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

当第一次评估循环的条件时,antalsteg为 0 和steps200。因此antalsteg == steps评估0 == 200为 false。所以永远不会进入循环。

于 2019-05-07T20:55:29.227 回答