我正在尝试做一个简单的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);
}
发布于 2013-07-08 06:15:33
你的问题出在你的if条件中:
if(photocellReading << 10){
CurrentNumber = CurrentNumber + 1;
}
您实际上要做的是:将photocellReading的位左移10 (相当于乘以2^10,也就是1024)。这很可能意味着,只有当photocellReading的值一开始就是0时,才会出现false。(我说最有可能是因为它取决于bits是否循环,但这并不完全相关)。
tl;dr您的代码在概念上等同于:
if((photocellReading * 1024) != 0){
CurrentNumber = CurrentNumber + 1;
}
我猜你想要做的是(考虑到你减去1023,恰巧是1024 - 1):
if(photocellReading < 1024){ // again 1024 == 2^10
CurrentNumber = CurrentNumber + 1;
}
https://stackoverflow.com/questions/17516857
复制相似问题