i am using optical speed sensor on my arduino uno to sense the rpm of a dc motor . the results is being displayed on i2c lcd , while the motor is functioning and the sensor is supposed to measure the desired rpm , the lcd shows 0 rpm but when the sensor is removed , the lcd displays the correct result.
here is my code:
int encoder_pin = 2;
unsigned int rpm;
volatile byte pulses;
unsigned long timeold;
unsigned int pulsesperturn = 2;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void counter()
{
pulses++;
}
void setup()
{
lcd.begin(16,2);
lcd.backlight();
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,0);
lcd.print("RPM = ");
lcd.println(rpm,DEC);
attachInterrupt(0, counter, FALLING);
}
}