interupt causes hang

Im using a Yun.
If I run the following code it doesnt post to my php file. The serial console doesnt open “error opening serial port”
I can upload a new/changed sketch though so It doesnt seem to be hanging the device completely.

If I remove the sei() and cli() the sketch runs and i can view serial console. Obviously my data isnt correct as Im not ever polling the hall effect sensor.

Any idea why this causes a hang?

#include <Bridge.h>
#include <HttpClient.h>
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() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  Serial.begin(9600);
  pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}

void loop() {
  NbTopsFan = 0;	//Set NbTops to 0 ready for calculations
  sei();		//Enables interrupts
  delay (10);	//Wait (a really small amount of time)
  cli();		//Disable interrupts
  Calc = (NbTopsFan * 6000 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour 
  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
  HttpClient client;
  String url = "http://10.10.0.50/catchyun.php?waterflow=value";
  url += String(Calc);
  client.get(url);

  Serial.flush();

  delay(5000);
}
  delay (10);  //Wait 1 second

Code and comment do not match. This delays for 10mS not one second.

was testing if the interrupt being shorter would help. I did adjust my math in the Calc variable. ;)

in any case a bad comment isn't causing the hang. I did edit the comment though.

You only need to disable interrupts to read NbTopsFan and then they should be enabled again. Your keeping interrupts off during serial print and maybe other calls may be hanging your sketch.

in any case a bad comment isn't causing the hang.

No but is shows a disconnect in your thinking which might be causing the problem.

@mike lol I was going to say that but you out grumpied me ;)

@Riva maybe; but this sketch works without the yun specific stuff on a Duo. Ill test the old non IoT version of the sketch on the Yun and see if it hangs as well.

Just tested the old version and it works on the Yun.

// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com

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
} 
// The setup() method runs once, when the sketch starts
void setup() //
{ 
  pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
  Serial.begin(9600); //This is the setup function where the serial port is initialised,
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
} 
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()    
{
  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 in L/hour 
  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
}

Does the sketch in your first post work if delay is 1000 instead of 10? Maybe (I'm not near hardware to test) your not leaving enough time for an interrupt to occur with delay 10 and NbTopsFan is zero when you do (NbTopsFan * 60 / 7.5 and this is causing a divide by zero error. Not sure what happens when divide by zero happens on an Arduino but it may lock/crash. I still think interrupts should be enabled during serial prints but maybe someone else can confirm/prove this wrong.

it doesnt work at 1000 either. I changed it to 10 to see if the interrupt was to long...