[Resolved] AnalogWrite impacting Interrupt?

I've borrowed some code to help me write a frequency counter. The code reads a 0-5V square wave output from the engine CPU, and writes the result via a serial print. But, I want to output a PWM voltage to the movement of an analog gauge. So, I've added an analogWrite statement to the code. But when I implement the write, it seems to impact the function of the original code. It's very repeatable: //out the Analog write, and everything works correctly; put it back in, and the data goes haywire. Specifically, I get about 10x as many 'rpmcount's - meaning (I think) that the code is getting Interrupted way more often when the AnalogWrite is there. This makes no sense to me, but it's the best I can figure now.
(I should note for anyone looking at the code that the 'rpm' variable isn't really actual RPM, it's supposed to represent the pwm value that is required to drive the analog gauge needle to the correct position).
I'm an Arduino noob - sure hope someone can help me learn something here!
Thanks!

//Define Variables 
  int rpm = 0;  
  volatile int rpmcount =0;  // volitile because the variable is manipulated during the interrupt
  unsigned long timeold = 0; // used to calculate d_t= millis()-timeold;
  int d_t; // delta_time

void setup()
{
   Serial.begin(9600);
   //Interrupt 0 is digital pin 2, so that is where the Input is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, rpm_fun, FALLING);
}

void loop()
{
   //Don't process interrupts during main loop
   detachInterrupt(0);
   d_t=millis()-timeold;
   if (d_t >= 150)  // fixed read interval of 150ms
    {
      rpm = (rpmcount)*0.82;  // 0.82 is a correction factor to get to the analog voltage I need for the gauge
      timeold = millis();
      d_t=0; //reset d_t
       //Serial Port Output
      Serial.print("Time(ms) ");
      Serial.print(timeold); //time at end of interval - hence timeold=millis()
      Serial.print(" TicsPerInterval ");
      Serial.print(rpmcount);
      Serial.print(" RPM ");
      Serial.println(rpm);
//      analogWrite(11, int(rpm));  //THIS GUY IS THE BUGGER - when //out, the code works fine, else NOT!
      rpmcount = 0; //reset rpmcount
}
  //Restart the interrupt processing
  attachInterrupt(0, rpm_fun, FALLING);
}


void rpm_fun()
{
   //This interrupt is run each time the processor senses a falling slope in the tach input
   detachInterrupt(0); //im not sure if this is necessary here
  
   rpmcount++; //update rpmcount

   attachInterrupt(0, rpm_fun, FALLING);
}

Delta_G:
I don't see how rpmcount can ever be greater than 1 in that code. You're disabling interrupts for the entire loop. So what's the point of an interrupt if you don't want it to interrupt anything? Why not just read the pin once on every pass of the loop if that's what you're going to do. Then rpmcount gets set back to zero on every turn of the loop right before the interrupt gets turned on and then off again on the very next command (back at the top of loop). If you get a falling edge anywhere during loop then the interrupt will be queued and will run once between the end of loop at the beginning. Then the interrupt can't fire again because it is turned off. So how could rpmcount ever be any greater than 1?

At first glance, I agree - it seems like the code would spend most of the time ignoring interrupts. But - it works well, so long as the analogWrite function is //out. My guess is that while d_t < 150, the code is so simple that it runs thru a basically useless loop manymanymany times, occasionally collecting an interrupt & rpmcount increment, until finally 150ms pass. The program then spends a bit of time writing to the serial port (and hopefully to the analogWrite), before resetting to zero & starting over.
I'm open to other ways to do this, although that's not my real question here. The program works well until I try to include analogWrite, then things get goofy. Perhaps analogWrite consumes a lot of time?

Delta_G:

rpm = (rpmcount)*0.82;  // 0.82 is a correction factor to get to the analog voltage I need for the gauge

I'd also look at that bit again and maybe think about the map() function. I'm not sure those floats and ints are going to play well together like you think they will.

Interesting idea - I like the map() function - esp if I had to deal with any negative voltages. Anyway, I replaced the rpm calculation with the map function, and - get this - it works the same! The code returns the correct value if the analogWrite function is //disabled, but I get about 10x too many rpmcounts if the analogWrite is enabled. Any other ideas???

OK, I've simplified the code, and done a better job of commenting. I'm still having trouble with analogWrite impacting the function of the code. When the analogWrite line is //commented out, the code works well. When it is not commented out, I get about 10x the correct number of rpmcounts. I've played with a number of things, but can't find anything that works. Can anyone see a flaw in my code, or know of a reason analogWrite could impact the function of the code?
Thanks!

//Define Variables 
  int rpm = 0;  
  volatile int rpmcount = 0;  // volitile because the variable is manipulated during the interrupt
  unsigned long timeold = 0; // used to calculate d_t= millis()-timeold;
  int d_t;  // delta_time
  int outputpin = 11;  // Pin 11 is the PWM output to the analog Tach
  
void setup()
{
   pinMode(outputpin,OUTPUT);  // define outputpin as an output (not needed?)
   Serial.begin(9600);  // open serial communications
   //Interrupt 0 is digital pin 2, so that is where the Input is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, rpm_fun, FALLING);
}  

void loop()
{
   d_t=millis()-timeold;  //sets delta_time to (current clock time - the clock time of the last fixed interval)
  
   if (d_t >= 150)  // after 150ms, stop endless loop & collection of interrupts & rpmcounts, and move to reporting
    {
      rpm = map(rpmcount, 0, 110, 0, 90);  //map instead of compute - was some concern about variable types being a problem...
      //Serial Port Output
      Serial.print("Time(ms) ");
      Serial.print(timeold);  //clock time as of this interval
      Serial.print(" TicsPerInterval ");
      Serial.print(rpmcount);  //number of interrupts & rpmcounts collected during the interval
      Serial.print(" RPM PWM Out ");
      Serial.println(rpm);  //mapped value from rpmcount
      rpmcount = 0;  //reset rpmcount in preparation for the next interval
      analogWrite(outputpin, rpm);  //THIS GUY IS THE BUGGER - when //out, the code works fine, else NOT!
      timeold = millis();  //records the clock time of the current interval
      d_t=0;  //reset delta_time   
    }
}

void rpm_fun()  //This interrupt is run each time the processor senses a falling slope in the tach input
{
   rpmcount++; //update rpmcount
}

OK, this is resolved.
I was monitoring the PWM'd pin with my o-scope, and I finally discovered that the code & pin function fine without the 'scope hooked up, but go crazy with it attached. I looked more closely & realized that I did not have my scope probe properly grounded!
There's 5 hours of my life I'd sure like to have back!
Thanks to those who replied - at least I learned some things!