rpm tacho using interrupts
Posted: Thu Nov 24, 2016 3:54 pm
I am trying to get a tacho working for feedback into a PID loop but the code I used for a Uno doesn't seem to work for ESP8266 Wemos D1 mini.
I get erratic results and I also get an output even when the motor is powered down.
I am guessing there is a difference in the way interrupts are handled????
I cannot seem to find any examples of tacho code for ESP8266 so here I am.
The code
The output.
Any help would be appreciated.
I get erratic results and I also get an output even when the motor is powered down.
I am guessing there is a difference in the way interrupts are handled????
I cannot seem to find any examples of tacho code for ESP8266 so here I am.
The code
Code: Select all
volatile float time = 0;
volatile float time_last = 0;
volatile int long rpm_array[5] = {0, 0, 0, 0, 0};
int pot;
void setup()
{
pinMode(14, INPUT_PULLUP);
//IO14 (D5) Set As An Interrupt
attachInterrupt(14, fan_interrupt, FALLING);
Serial.begin(115200);
digitalWrite(13, HIGH);
analogWrite(12, 500);
}
//Main Loop To Calculate RPM and Update LCD Display
void loop()
{
while (1) {
int long rpm = 0;
//Slow Down The Display Updates
delay(400);
//Update The RPM
noInterrupts();
unsigned long copyTime = time;
interrupts();
if (time > 0)
{
//5 Sample Moving Average To Smooth Out The Data
rpm_array[0] = rpm_array[1];
rpm_array[1] = rpm_array[2];
rpm_array[2] = rpm_array[3];
rpm_array[3] = rpm_array[4];
rpm_array[4] = 60 * (1000000 / (time * 36));
//Last 5 Average RPM Counts Eqauls....
rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
// rpm = 60*(1000000/(time * 36));
Serial.print(rpm);
Serial.println(" rpm");
//Serial.println(pot);
}
}
}
//Capture The IR Break-Beam Interrupt
void fan_interrupt()
{
time = (micros() - time_last);
time_last = micros();
}
The output.
Code: Select all
54 rpm
83387 rpm
83391 rpm
83347 rpm
83349 rpm
83349 rpm
19 rpm
20 rpm
20 rpm
19 rpm
19 rpm
20 rpm
19 rpm
19 rpm
19 rpm
19 rpm
19 rpm
19 rpm
53 rpm
53 rpm
52 rpm
52 rpm
52 rpm
20 rpm
20 rpm
Any help would be appreciated.