Use of detachInterrupt()

Hi there.

I have the following code(only showing part of the void loop) The attachinterrupt is in void setup (attachinterrupt(0,rpm_fun,FALLING)):

void loop()
{
  // read 13 sensors and append to the string:
  analogRead(analogPin0);
  delay(10);
  tempPin0 = ((2*analogRead(analogPin0)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin1);
  delay(10);
  tempPin1 = ((2*analogRead(analogPin1)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin2);
  delay(10);
  tempPin2 = ((2*analogRead(analogPin2)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin3);
  delay(10);
  tempPin3 = ((2*analogRead(analogPin3)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin4);
  delay(10);
  tempPin4 = ((2*analogRead(analogPin4)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin5);
  delay(10);
  tempPin5 = ((2*analogRead(analogPin5)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin6);
  delay(10);
  tempPin6 = ((2*analogRead(analogPin6)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  analogRead(analogPin7);
  delay(10);
  tempPin7 = ((2*analogRead(analogPin7)*(5.0 / 1023))-2.05)/0.005;
  delay(10);
  pressurePin0 = (analogRead(analogPin8)*(5.0 / 1023)*3)-1.5-0.11;
  delay(10);
  pressurePin1 = (analogRead(analogPin9)*(5.0 / 1023)*3)-1.5-0.11;
  delay(10);
  pressurePin2 = (analogRead(analogPin10)*(5.0 / 1023)*3)-1.5-0.11;
  delay(10);
  pressurePin3 = (analogRead(analogPin11)*(5.0 / 1023)*3)-1.5-0.11;
  delay(10);
  rpmPin0 = rpm;
  delay(10);
  rpmPin1 = rpm1;
  pressurePin01 = pressurePin0;
  pressurePin11 = pressurePin1;
  pressurePin21 = pressurePin2;
  pressurePin31 = pressurePin3;
  
  
  if (digitalRead(controlPin0)==LOW)
  {
    controlPin = false;
  }
  ////////CREATING A NEW LOGFILE
if (digitalRead(controlPin0)==HIGH && controlPin==false)
  {
   
  // create a new file
  //char filename[] = "LOGGER00.csv";
  for (uint8_t i = 0; i < 100; i++) {
  filename[6] = i/10 + '0';
  filename[7] = i%10 + '0';
  if (! SD.exists(filename)) {
  // only open a new file if it doesn't exist
  logfile = SD.open(filename, FILE_WRITE); 
  break;  // leave the loop!
  }
  }
 
  
  if (! logfile) {
    //Serial.println("couldnt create file");
    return;
  }
  
  //Serial.println("card initialized.");
  
  //Serial.print("Logging to: ");
  //Serial.println(filename);
  
   logfile = SD.open(filename, FILE_WRITE);
  logfile.println("Temperature0,temperature1,temperature2,Temperature3,temperature4,temperature5,temperature6,temperature7,pressure0,pressure1,pressure2,pressure3,RPM0,RPM1");    
logfile.close();

  //Serial.println("Temperature0,temperature1,temperature2,Temperature3,temperature4,temperature5,temperature6,temperature7,pressure0,pressure1,pressure2,pressure3,RPM0,RPM1");
 
 controlPin = true;
 
 delay (100);

  }
  
  
   //Update RPM every second
   //delay(1000);
   //Update RPM every 20 counts, increase this for better RPM resolution,
   //decrease for faster update
   //Don't process interrupts during calculations
   //detachInterrupt(0);
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm = (rpmcount*60000)/(millis() - timeold);
   timeold = millis();
   rpmcount = 0;
   
    //detachInterrupt(1);
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm1 = (rpmcount1*60000)/(millis() - timeold1);
   timeold1 = millis();
   rpmcount1 = 0;

   //lcd.clear();
   //lcd.print("RPM=");
   //lcd.print(rpm);

   
 //////////START LOGGING ON THE SD CARD 
  if (digitalRead(controlPin0)==HIGH && controlPin==true)
  {

  // make a string for assembling the data to log:
logfile = SD.open(filename, FILE_WRITE);
void rpm_fun()
 {
   //Each rotation, this interrupt function is run twice, so take that into consideration for 
   //calculating RPM
   //Update count
      rpmcount++;
 }

It seems to work okay with all the analogread and so on. But the question is, should i use the detachinterrupt() when during calculations?

best regards Bastian

No you should not use it.

There is unlikely to be a need to call detachInterrupt() ever, you just set
things up in setup(). There is no corresponding "de_setup()", the micro
controller just runs till the power is removed typically.

You do not want to detach the interrupt.

You may want to disable the interrupt very briefly during some calculations, so that if the interrupt even occurs during the middle of the calculation, the result is not bogus.

Temporarily disabling interrupts is different to detaching the interrupt function.

Try google arduino cli and arduino sei for examples.

I'd suggest avoiding cli/sei, use the macros interrupts() and noInterrupts() - these are
portable across different microcontrollers in the various Arduino boards, and are
a lot more self-eplanatory!

Thanks guys :slight_smile: just gonna leave it as it is. It seems to work acceptable