Hi,
I'm putting an rpm sensor on the end of a motor using a photo interrupter, and I'm having issues getting accurate feedback when I adjust the motor speed that I'm reading the rpm on. At full speed, I get a stable rpm value, about 3400 rpm -which seems accurate, but when I slow the motor speed down to anything below a 255 analogWrite value, the rpm values jump inconsistently from about 16000 to 172000 rpm, regardless of the speed. Any thoughts?
volatile byte counts;
unsigned long rpm;
unsigned long timeold;
long prevTime;
int motorPin = 9;
int analogPin = A0;
int analogVal;
int N = 2; //number of blades on fan
int pwm;
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), rpm_fun, RISING);
// attachInterrupt(0, rpm_fun, RISING);
counts = 0;
rpm = 0;
timeold = 0;
}
void loop(){
pwm = 90;
analogWrite(motorPin, pwm);
if (counts >= 100) {
long interval = millis() - prevTime;
prevTime = millis();
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = (60 / N) * 1000 / (millis() - timeold) * counts;
timeold = millis();
counts = 0;
Serial.print("OutputVal= ");
Serial.print(pwm);
Serial.print(" RPM= ");
Serial.print(rpm, DEC);
Serial.print(" millis = ");
Serial.println(interval);
// adjRPM(2600);
}
}
void rpm_fun() {
counts++;
}