loop wont loop with a delay

On line 27 if I uncomment it the program will loop once or sometimes twice and thats it. As soon as I comment it out the programm loops with just the 1 second delay while the hall effect sensor is timing pulses. I've moved it up into the main void loop, moved it to the top, moved it to the bottom. I cant figure out why it kills the program? No matter where I put in the additional delay it dies. But I have many other sketches that have multiple delays with no problem. Any Processing gurus have an idea?

//Dry Remote Arduino Dingle Dongle for GroPI5000  (CO2, Temp, Humidity, Flood Alert)

int AirtemperaturePin = 3;     //Air temp connected to analog 3
int airtempsensorvalue = 0;    //Air Temp Sensor Value 
int airtemperature = 0;        //Air Temp
int CO2Pin = 4;                //CO2 Sensor Pin
int CO2SensorValue = 0;        //CO2 Sensor Value
int CO2LevelValue = 0;         //CO2 Level Value
int FloodValue = 0;            //Flood Sensor value
int FloodPin = 5;              //Flood Sensor Pin

volatile int NbTopsFan;        //measuring the rising edges of the signal
int Calc;                               
int hallsensor = 2;            //The pin location of the sensor
void rpm ()     //This is the function that the interupt calls 
{ 
  NbTopsFan++;  //This function measures the rising and falling edge of the hall effect sensors signal
} 
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600); //This is the setup function where the serial port is initialised,
  pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
void loop()        {             // run over and over again
doDryDingleDongle();
//delay (5000); //Wait 5 seconds  --- THIS DOESNT WORK

}

void doDryDingleDongle()
{
 ///////// Water Flow Meter

   NbTopsFan = 0;  //Set NbTops to 0 ready for calculations
  sei();       //Enables interrupts
  delay (1000);    //Wait 1 second
  cli();       //Disable interrupts
  Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate 
  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line

  // Air Temp
   
airtempsensorvalue=analogRead(AirtemperaturePin);//Connect LM35 on Analog 3     
airtemperature=(500*airtempsensorvalue)>>10; 
airtemperature = (airtemperature * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
Serial.print("Air Temp:");
Serial.print(airtemperature);                     //printing the result
Serial.print(" / ");
Serial.print("Pin Reading:");
Serial.println(airtempsensorvalue); 


  /////////////////////////////////// //  CO2
   CO2SensorValue = analogRead(CO2Pin);
CO2LevelValue = map(CO2SensorValue, 0, 1023, 0, 2000); ;            
  Serial.print("CO2:");
  Serial.print(CO2LevelValue);                     //printing the result
Serial.print(" / ");
 Serial.print("Pin Reading:");
 Serial.println(CO2SensorValue);
 Serial.println("---------------------------------");
}

void doDryDingleDongle()
{
///////// Water Flow Meter

NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts

Please explain what you want those two lines of code to accomplish.

[

jointtech:
…As soon as I comment it out the programm loops with just the 1 second delay …

...

void loop()        {
   doDryDingleDongle();
   delay (5000);    //Wait 5 seconds  — THIS DOESNT WORK

}
void doDryDingleDongle()
{
///////// Water Flow Meter

NbTopsFan = 0; //Set NbTops to 0 ready for calculations
 sei(); //Enables interrupts
 delay (1000); //Wait 1 second
 cli(); //Disable interrupts <==== From davekw7x: Huh?
.
.
.
Serial.println("---------------------------------");
}

For one thing, you are leaving this function with interrupts disabled. The delay() function (and several other things) won’t work properly.

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    int time1, time2, time3, time4;
    
    cli();
    time1 = millis();
    delay(5000);
    time2 = millis();
    
    sei();
    time3 = millis();
    delay(5000);
    time4 = millis();
    
    Serial.print("time2 - time1 = ");Serial.println(time2 - time1);
    Serial.print("time4 - time3 = ");Serial.println(time4 - time3);
    
    Serial.println();
}

Output:


[color=blue]
time2 - time1 = 0
time4 - time3 = 4999
[/color]

Maybe you can try something like the following:

1. Don’t attach the rpm interrupt in setup().

Then, inside the doDryDingleDongle() function:

2. Set NbTopsFan = 0.

3. Attach the rpm interrupt so that NbTopsFan can change.

4. Delay a second.

5. Detach the rpm interrupt so that NbTopsFan can not change any more.

6. Do the calculations using the value of NbTopsFan.

Bottom line: For this application, you don’t need to use cli() to disable global interrupts. (But if you really, really (really) want to, make sure that you re-enable them before expecting something useful for subsequent delay() function calls or anything else involving Timer 0 interrupts.)

Regards,

Dave