Arduino calculation error... Or operator error?
Below is a tachometer program that was patched together... Using a hall effect for the sensor. For some odd reason when the calculation is done for interrupts per second something goes wrong... When set at 30*1000 I get what seems to be a good RPM number... But when changed to 60*1000 the RPM jumps to 20-50k... Not sure what is going on... If someone would be so kind and have a look at the program and see if I am missing something... It would be appreciated..
int ledPin = 11;
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
void rpm_fun()
{
rpmcount++;
}
void setup()
{
lcd.begin(16, 2);
attachInterrupt(0, rpm_fun, FALLING);
pinMode(ledPin, OUTPUT);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
delay(1000);
detachInterrupt(0);
rpm = 60 * 1000 / (millis() - timeold) * rpmcount;
timeold = millis();
rpmcount = 0;
lcd.clear();
lcd.print("Rotary Head RPM:");
lcd.setCursor(6, 1);
lcd.print(rpm);
if (rpm > 100)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite (ledPin, LOW);
}
attachInterrupt(0, rpm_fun, FALLING);
Comments
5 years ago
Try
rpm = 60000 / (millis() - timeold) / rpmcount;
And redefine RPM to a bigger type like unsigned long
Answer 5 years ago
This make the static RPM start at 65535 and go down to 3 when doing the interrupt counting...
Answer 5 years ago
Which kind of proves you are exceeding the limit of the type. Did you change it like I said ? It should be obvious, that after 65 seconds, funny things are going to happen.
5 years ago
I'm by far no expert with Arduino so I might have missed something but I can't see where the input from your sensor is?
You have a nice calculation but where goes the signal in this calculation?
Answer 5 years ago
Its the value generated by incrementing RPMcount in the interrupt routine.
Answer 5 years ago
Yes, I got that, but where is the input from the actual sensor?
Answer 5 years ago
Pin 2.
Answer 5 years ago
At the AttachInterrupt argument (0, rpm_fun, FALLING) ?
Not good form, but works.
5 years ago
If you're using an Uno, and not a Due, you may well be running out of room in "rpm" for the calculatio, since it can only hold a 16bit value.