Rtma
1
I have some problem with rpm detection project.
Program is referenced from this site:
http://playground.arduino.cc/Main/ReadingRPM
When i change the number from 30 to 60 in rpm calculation line.
rpm = 30*1000/(millis() - timeold)*half_revolutions;
to
rpm = 60*1000/(millis() - timeold)*half_revolutions;
Serial monitor gives me a strange answer.
For example,the real rpm is 12 but it displays thousand rpm.
What wrong make this happen?
system
2
What wrong make this happen?
What is 60 * 1000? Does that fit in an int?
What is 60 * 1000UL? What type register will the compiler use to hold the result?
Rtma
3
I change the rpm calculation line from
rpm = 60*1000/(millis() - timeold)*half_revolutions;
to
rpm = 30*1000/(millis() - timeold)half_revolutions;
rpm = rpm2;
the strange answer disappears
I can't distinguish the different between them.
system
4
I am not understand the different way between them.
30 is an int.
1000 is an int.
30 * 1000 is an int, and will fit in an int.
60 is an int.
1000 is an int.
60 * 1000 is an int, but the value will NOT fit in an int.
60 * 1000UL will force the use of an unsigned long register to hold the result. So will 60 UL * 1000 or 60UL * 1000UL.
void setup() {
Serial.begin(115200);
Serial.print("60*1000 = ");
Serial.println(60 * 1000);
Serial.print("30*1000 = ");
Serial.println(30 * 1000);
}
void loop() {}
60*1000 = -5536
30*1000 = 30000