Dear All,
it seems a problem with my interrupt pin 2. I want to calculate the RPM of a motor. The simple code is quite well through int pin 3, but when I change it to int pin 2 the result is not correct. What should be the problem?
I check it on two arduino uno boards, same problem.
thanks
int ir_sensor = 2;
unsigned int ir_rpm = 0;
unsigned int numOfRotations = 0;
int pulsesPerTurn = 1;
unsigned long lastTime = 0;
void counter(){
numOfRotations++;
}
void setup() {
Serial.begin(115200);
pinMode(ir_sensor,INPUT);
attachInterrupt(digitalPinToInterrupt(ir_sensor), counter, FALLING);
}
void loop() {
stop_ir_Interrupt();
Serial.print(" "); Serial.println(ir_rpm);
}
void stop_ir_Interrupt(){
if(millis() - lastTime >= 1000){
detachInterrupt(digitalPinToInterrupt(ir_sensor));
ir_rpm = (numOfRotations * 60) / pulsesPerTurn;
lastTime = millis();
numOfRotations = 0;
attachInterrupt(digitalPinToInterrupt(ir_sensor), counter, FALLING);
}
}