Problem in using Interrupts in Arduino

I was going through the concept of Interrupts in Arduino and found myself stuck at a point. I just wrote a simple Arduino code to count the number of pulses generated in one second by the Water Flow Sensor YF-S401 sensor.

I am having couple of problems with the output of the code. The problems are as follows:

int flowPin = 2;
double flowRate=5.0;
volatile int count;

void setup() {
  pinMode(flowPin, INPUT);
  Serial.begin(9600);

  Serial.println("In the Interrupt");
  attachInterrupt(0,flow, RISING);
  delay(1000);
  noInterrupts();
  
  Serial.println(count);
  Serial.print(flowRate);
}


void loop() {
}

void flow()
{
  count++; 
}
  • In the above code, I have just written the code to count the number of pulses generated / no of revolutions incurred by the rotor of the flow sensor. In this case it was 12. I have used Serial.println() to print out two variables ie flowRate and count. But only the first print statement is being executed. Why the second one is not there? Output for the above code is:

Why the second variable is not printing?

  • Also I saw that whenever I am trying to print a variable (be it string, int or double), in the output only two places are visible. For eaxmple see this screenshot:

Why only 52 is showing? It should have given a value of 52.05 Same is the problem for any variable. Have a look:


Whty is this happening ?

Try turning interrupts back on.

Why ?

@wildbill but I need that Interrupt to run only once!

Should digitalPinToInterrupt(0) be the first parameter, instead of just 0?
Also, do you really need to use pin 0? I try to avoid using 0 & 1 unless the world is ending.
Also, you have flowPin = 2, what is this pin for?

In oredr to close it. Is it wrong?

Then detach it. I have a feeling that Serial needs them.

The flow sensor is attached to GPIO pin 2 of arduino which has the Interrupt available. I guess, GPIO 0 is equivalent to Interrupt 0!

Oops

What is wrong in the code @TheMemberFormerlyKnownAsAWOL

Ok, so you have a sensor hooked to pin 2 but you're trying to read it on pin 0?
I would recommend using

attachInterrupt(digitalPinToInterrupt(2), flow, RISING);

Plus all the other recommendations by everyone else too

Serial I/O depends on interrupts

Why guess, when you can know?

@er_name_not_found I have corrected it but still the same is being shown.

Something is wrong in the interrupts because when I remove the Interruot section then it is all good

Still oops.

Post code, not pictures of code.

Oh my bad. Here is the code:

int flowPin = 2;
double flowRate=52.05;
String quantity = "HIGH";
volatile int count;

void setup() {
  pinMode(flowPin, INPUT);
  Serial.begin(9600);

  Serial.println("In the Interrupt");
  attachInterrupt(digitalPinToInterrupt(flowPin),flow, RISING);
  delay(1000);
  noInterrupts();
  
  Serial.print(quantity);
}


void loop() {
}


void flow()
{
  count++; 
}

No. He has a sensor on pin 2 and is reading it with interrupt 0. Guess which pin is used by interrupt 0 ?
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

You must not switch off all interrupts forever. There is not only your interrupt on an arduino. This was already adviced to you. Believe it!.

Yes. Corrected it also but still the same

Nope.