I have written a code but my RPM reading from my IR tachometer keeps on fluctuating.
An example is shown below and the motor is constantly spinning around 990RPM
00:57:01.278 -> =====================
00:57:01.278 -> RPM : 1194
00:57:01.583 -> =====================
00:57:01.583 -> RPM : 995
00:57:01.854 -> =====================
00:57:01.888 -> RPM : 995
00:57:02.157 -> =====================
00:57:02.190 -> RPM : 796
00:57:02.457 -> =====================
00:57:02.491 -> RPM : 995
00:57:02.794 -> =====================
00:57:02.794 -> RPM : 1593
00:57:03.064 -> =====================
00:57:03.099 -> RPM : 1194
00:57:03.368 -> =====================
00:57:03.401 -> RPM : 1593
00:57:03.669 -> =====================
00:57:03.703 -> RPM : 1194
00:57:03.971 -> =====================
00:57:04.004 -> RPM : 995
Attached is the code i used
// Pin Allocation
#define trigPin 10 // Ultrasonic Sensor Trigger Pin
#define echoPin 11 // Ultrasonic Sensor Echo Pin
float rev=0;
int rpm;
long time=0,duration, distance ,oldtime=0;
void isr() // Interrupt Service Routine to count the pulse od the IR Sensor
{
if(digitalRead(2)==LOW)
rev++;
}
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Ultrasonic Trig Pin as Output
pinMode(echoPin, INPUT); // Ultrasonic echo Pin as Input
attachInterrupt(0,isr,FALLING); // Attaching the interrupt to interrupt pin0(Digital Pin 2) with Falling Edge
}
void loop()
{
// --------------------------------- Tachometer Reading using IR ------------------------------------------------
detachInterrupt(0); // Detaches the interrupt
time=micros()-oldtime; // Finds the time
Serial.println("=====================");
//Serial.println(rev); // Print the Revolutions on Serial monitor
rpm=(rev/time)*60000000; // Equation to calculates RPM
//Serial.println(oldtime); // Print the time on Serial monitor
//Serial.println(time); // Print the time on Serial monitor
Serial.print("RPM : ");
Serial.println(rpm); // Print the RPM on Serial monitor
oldtime=micros(); // saves the current time
rev=0;
attachInterrupt(0,isr,FALLING); // Attaching the interrupt to interrupt pin0(Digital Pin 2) with rising Edge
delay (300);
}