Arduino Interrupt pins problem

Hi everybody,

I want to get pulses to calculate the RPM of rotor speed through Arduino Uno interrupt pins 2 and 3. There is a problem, when there is nothing connected to those pins I have many interrupts and I see rpm more than 3000. Wha is the problem?

P.S.: When IR sensor is conneted everything is good. With Inductive sensor I have this problem when there is no pulse! But it gives me the rpm as well when the rotor is working.

Is there any bugs with interrupt pins or my code hase problem?

#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C LCD(0x27,20,4);

int ir = 2;
unsigned int rpm=0;
unsigned int numOfRotations=0 ;
unsigned long lastTime=0;
int pulsesPerTurn = 1;
int p = 3;
unsigned int rpmp=0;
unsigned int numOfRotationsp=0 ;
unsigned long lastTimep=0;

void counter(){
numOfRotations++;
}
void counterp(){
numOfRotationsp++;
}

void setup() {
Serial.begin(115200);
LCD.begin();
pinMode(ir, INPUT);
attachInterrupt(digitalPinToInterrupt(ir), counter, FALLING);
pinMode(p, INPUT);
attachInterrupt(digitalPinToInterrupt(p), counterp, FALLING);
}

void loop(){

if(millis() - lastTime >= 1000){
detachInterrupt(digitalPinToInterrupt(ir));
rpm = (numOfRotations * 60) / pulsesPerTurn;
lastTime = millis();
numOfRotations = 0;

attachInterrupt(digitalPinToInterrupt(ir), counter, FALLING);
}

if(millis() - lastTimep >= 1000){
detachInterrupt(digitalPinToInterrupt(p));
rpmp = (numOfRotationsp * 60) / pulsesPerTurn;
lastTimep = millis();
numOfRotationsp = 0;

attachInterrupt(digitalPinToInterrupt(p), counterp, FALLING);
}

Serial.print("RPM= ");
Serial.print(rpm);
Serial.print(" ");
LCD.setCursor(0,1);
LCD.print("RPM= ");
LCD.print(rpm);
Serial.print("RPMp= ");
Serial.println(rpmp);
LCD.setCursor(0,2);
LCD.print("RPMp= ");
LCD.print(rpmp);
}

If you have nothing connected to an input pin, it will pick up electrical interference and randomly switch on and off. This is why a pull-up or pull-down resistor is used; to maintain the pin in a known state. The Uno's microcontroller has built-in pull-up resistors you can turn on by setting the pin mode to INPUT_PULLUP.

Please include links to datasheets/product pages for each piece of hardware you refer to - we need
to know what you have connected up!

Also why post in the audio forum?

Also why post in the audio forum?

My guess is the OP read the forum name as Auto - thinking it was about automobiles.