1 c++ loops if-statement arduino
我试图制作一个简单的Arduino代码,当光电池读数小于900时,它会将1加到CurrentNumber并显示在4位7段显示器上.问题是它不会停止添加一个,即使它读取超过1000.
void loop() {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
photocellReading = 1023 - photocellReading;
if(photocellReading << 10){
CurrentNumber = CurrentNumber + 1;
}
displayNumber(CurrentNumber);
}
Run Code Online (Sandbox Code Playgroud)
您的问题出在if条件中:
if(photocellReading << 10){
CurrentNumber = CurrentNumber + 1;
}
Run Code Online (Sandbox Code Playgroud)
你基本上做的是:将photocellReading的位向左移10(相当于乘以2 ^ 10又称1024).这很可能意味着,如果photocellReading的值为0,那么唯一一次这将是假的.(我说很可能是因为它取决于这些位是否循环回来,但这并不完全相关).
tl; dr你的代码在概念上等同于:
if((photocellReading * 1024) != 0){
CurrentNumber = CurrentNumber + 1;
}
Run Code Online (Sandbox Code Playgroud)
我猜你想做什么(考虑你减去1023,巧合的是1024 - 1)是:
if(photocellReading < 1024){ // again 1024 == 2^10
CurrentNumber = CurrentNumber + 1;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
113 次 |
最近记录: |