Integer overflow error help

I am making a Hall effect tachometer. I have five magnets on a rotating disc in front of a US5881

Code I'm using:

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int encoder_pin = 2;
unsigned int rpm;
volatile byte pulses;
unsigned long timeold;
unsigned int pulsesperturn = 5;

void counter(){
pulses++;
}
void setup() {
lcd.begin(16,2);
lcd.print("RPM:");
pinMode(encoder_pin, INPUT);
attachInterrupt(0, counter, FALLING);
pulses = 0;
rpm = 0;
timeold = 0;

}

void loop() {
if (millis() - timeold >= 1000){
detachInterrupt(0);
rpm = (60 * 1000 / pulsesperturn) / (millis() - timeold) * pulses;
timeold = millis();
pulses = 0;
lcd.setCursor(0,1);
lcd.print(rpm);
attachInterrupt(0, counter, FALLING);
}
}

The error I'm getting is, seems to be an issue with my equation:

C:\Users\Devin\Documents\Arduino\Hall_Tachometer\Hall_Tachometer.ino: In function 'void loop()':

C:\Users\Devin\Documents\Arduino\Hall_Tachometer\Hall_Tachometer.ino:27:13: warning: integer overflow in expression [-Woverflow]

rpm = (60 * 1000 / pulsesperturn) / (millis() - timeold) * pulses;

^

Any help would be great! :slight_smile:

This calculation will be handled as an int if you do nothing else. The largest int is 32767. 60 * 1000 will exceed this value.

One solution, and there are several, is to replace 60 with 60UL. This will force this part of the calculation to be handled as unsigned long. An unsigned long can be much greater than 60,000.

This assumes, of course, that the formula is correct in the first place.

Well the error is gone with I us 60UL but the output on the LCD is:
RPM:
0

No matter how fast the rpm actually is. I have an LED in my circuit that will only turn on when the magnets are detected by the hall sensor. I can see the LED cycle on and off so the circuit itself is working.

Guess it's a problem with the equation but I dont know why.

rpm = (60UL * 1000 / pulsesperturn) / (millis() - timeold) * pulses;

Pulses/pulsesperturn = number of rotations
60*1000 is number of miliseconds in a minute
(millis()-timeold) = elapse time in miliseconds
Not sure how that could be wrong.

Nvm my input pin wasnt connect all the way :roll_eyes:

Few questions whose answers, from your side, might lead youself solving the problem?

1. Are you using UNO?
2. Which digital pins do accept interrupt request signal (IRQ) from external devices?
3. What are the puposes of the three arguments of the attachInterrupt(arg1, arg2, arg3); function?
4. What is recommended synatx of the use of arg1 of the attachInterrupt();?
5. Is it enough to initialise the interrupt process only for once using the attachInterrupt();?
6. Are you counting your encoder pulses through interrupt process or polling the PD0 (0) line?
7. Do you know the reason for discouraging the use of the 0, 1 pins of the UNO as general purpose IO lines?
8. In some cases, we might be using the 0, 1 pins of the UNO as GPIO lines; then, what do we do with the jumper wires connected with these pins during the uploading of a sketch?
9. Does the function millis(); work on interrupt protocol?