No, there is several functionality that relies on interrupts - not only Serial. Als e.g. delay() and millis(). And many libs rely on interrupts too.
Post your final code please, not in a picture.
int flowPin = 2;
double flowRate=52.05;
volatile int count;
void setup() {
pinMode(flowPin, INPUT);
Serial.begin(9600);
Serial.println("In the Interrupt");
attachInterrupt(digitalPinToInterrupt(flowPin),flow, RISING);
delay(1000);
detachInterrupt(digitalPinToInterrupt(flowPin));
Serial.println(flowRate);
}
void loop() {
}
void flow()
{
count++;
}
Output is:
In fact, the attachInterrupt() function could be written in the following two styles:
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); //(recommended); arg1 = DPin-2 or 3
attachInterrupt(interrupt, ISR, mode); //(not recommended); arg1 = interrupt type: 0 or 1
They're the same style.
Yes. I am sure that by default interrupt 0 is provided at the GPIO 2 and interrupt 1 is provided at GPIO 3 That is why the below two declarations are equivalent:
- attachInterrupt(digitalPinToInterrupt(flowPin),flow, RISING);
- attachInterrupt(0,flow, RISING); In case the pin is GPIO 2
Yup, that what the docs say. As it should only happen once or once in a great while there is no advantage to not using the helper function except to save a couple bytes of program space, right?
Which Learning Board/Kit are you using?
Note:
attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)
@versatile_vats in the code in reply #43, why do you bother to detach the interrupt ?
I am also somewhat bemused as to why you print "In the Interrupt" when the code is not in the interrupt
@GolamMostafa I am not using any dev kit. I am just working on an Arduino UNO R3. Here is an image for the reference.
digitalPinToInterrupt(2) will evalute to 0 which is the Interrupt no 0 available on GPIO 2
That's only true for UNO and Nano. Other Arduino boards have different mappings. Therefor it is always a good idea to use the helper function.
digitalPinToInterrupt is a macro, and usually the expansion only leads to a comparison of constants. This can be done by the compiler at compile time and does not lead to any additional code.
As my old Drill Sergeant used to say, "A darn sight more better"
I swear they goal was to make us laugh by saying dumb stuff.
You are working with Arduino UNO but are using terminology like GPIO (a term used in ESP8266 Board/NodeMCU, Fig-1) which confused me and led me to ask you the question. In Arduino UNO context, we usually use DPin (Digital Pin) term.
Figure-1:
Yup I just checked that delay functionality can't be used in the Interrupt Service functions. Otherwise we can use them perfectly in the code
We do?
Do you why?
When an interrupt occurs, the MCU disables the "interrupt logic" before landing at the Interrupt Service Routine (ISR); but, the delay() function needs "enabled interrupt logic" for its functioning. If you still want to insert delay() function in the ISR (not recommended at all!), then you can enable the interrupt logic by executing one of the following codes just after arriving at the ISR.
interrupts();
or
sei();
or
bitSet(SREG, 7);
or
bitWrite(SREG, 7, HIGH);
I usually use DPin term. I use this term to stay away from the physical pin (PPin) of the MCU.
You are not disabling anything, rather you are detaching an explicit interrupt which will have no effect on other interrupts. However, in the code that you have posted I don't understand why you are doing it.
I don't think it can be made sense of from a practical perspective - from the first post I gathered that it's an arbitrary exercise intended to help the OP gain experience using interrupts.
But the code in post #43 doesn't actually use an interrupt at all so he will not gain much experience from it


