RPM Tahometer giving much bigger values

Hey guys, I'm trying to build a tahometer for my school projects. However, I encountered a really big problem and i don't know how to solve it. Instead of showing rpms of 300, 400,500,1000 and so on it shows rpms of 50000 60000, 49000 and so on. Here's my programm that I run.

int rpmcount;
unsigned int rpm;
unsigned long timeold;

void rpm_fun(){
rpmcount++;
}

void setup (){
pinMode(13, OUTPUT);
digitalWrite(13,HIGH);
Serial.begin(9600);
attachInterrupt(0, rpm_fun, FALLING);
rpmcount=0;
rpm=0;
timeold=0;
}

void loop(){
delay(1000);
detachInterrupt(0);
rpm=60*1000/(millis()-timeold)*rpmcount;
timeold=millis();
rpmcount=0;
Serial.print("RPM:");
Serial.println(rpm);
attachInterrupt(0,rpm_fun, FALLING);
}

On the pin 2 we receive High and Low but i don't know why when the function in attachInterrupt is called, instead of adding plus 1 it gives values like 98, 96 , 134, 128.

It is really weird. I run an Arduino uno with an IR sensor (Shop by Category | eBay). The problem begins at the counting function. It really drives me crazy

I would suspect that your math is overflowing. 60*1000 wont fit into a signed integer so that alone may be causing an overflow error. I would use 60000UL. When you divide that by the approximately 1000 milliseconds since the last pass through loop() so you end up with a value around 60. Since you are using integers there will be some roundoff errors there. Multiplying by interrupts in that second should get interrupts per minute. Your sketch assume that your sensor gives one pulse per revolution. Is that a correct assumption?

johnwasser:
I would suspect that your math is overflowing. 60*1000 wont fit into a signed integer so that alone may be causing an overflow error. I would use 60000UL. When you divide that by the approximately 1000 milliseconds since the last pass through loop() so you end up with a value around 60. Since you are using integers there will be some roundoff errors there. Multiplying by interrupts in that second should get interrupts per minute. Your sketch assume that your sensor gives one pulse per revolution. Is that a correct assumption?

Yep, it's one pulse per revolution. However, I still suspect the attachInterrupt function counting the time the sensor detects the white strip and i can't figure out why

taranbis:
I still suspect the attachInterrupt function counting the time the sensor detects the white strip and i can't figure out why.

You should test your suspicion to see if your suspicion is correct or not. Try just displaying the interrupt count each second to see if you get reasonable values.

void loop() {
  delay(1000);
  detachInterrupt(0);

  Serial.print("RPM count: ");
  Serial.println(rpmcount);

  rpmcount=0;
  attachInterrupt(0,rpm_fun, FALLING);
}

Yep, it's confirmed. Instead of counting like 1,2,3 it counts like 163, 251, 100 and so on ... apparently it counts whenever it's changing although there is clearly stated FALLING and it also counts like 4 times the same stripe ... that's why i get the really ugly results

If you get more interrupts than you have revolutions I would guess that your signal is bouncing. Exactly what sensor do you have and how are you conditioning the signal?

taranbis:
Yep, it's confirmed. Instead of counting like 1,2,3 it counts like 163, 251, 100 and so on ... apparently it counts whenever it's changing although there is clearly stated FALLING and it also counts like 4 times the same stripe ... that's why i get the really ugly results

You mention "stripe". What stripe? what it the stripe? Is it paint, reflective strip? Did you paint it with a brush? How wide is the stripe? What do the edges of the stripe look like? Are they ragged or is there an abrupt transition from the wheel surface to the stripe? Is there adhesive showing on the edge of the stripe?

I guess your "tahometer" is a "tachometer". Something to measure rotation per minute or second.

Can you run your program and move the stripe by hand past the sensor? You should be able to see the program count 1 every time the sensor sees the stripe.

Paul