Hi everyone, I am learning on interrupts and I wrote a simple sketch just for practice. The interrupt it doesn't look to work fine... I am using an Arduino Nano for the experiment. I have set pin 8 to HIGH and pin 2 as the interrupt pin. With a jumper cable, I attach/de-attach pin 8 to pin 2 in order to enable the interrupt and I measure the attachments with a counter.
The problem is that when I attach pin 8 to pin 2 and the interrupt is activated, pin2 reads high for more time than I actually have the jumper cable attached. For example, I attach the jumper cable for 1s and then de-attach but pin 2 is still high for around 4-5 seconds with consequence the counter does not count during this period... I cannot figure out what is going on... It is like something keeps tie pin2 to high...
Any help?
# include <Arduino.h>
# define pinOut 8
# define pinToInterrupt 2 //For Arduino Nano the interrupts pins are 2 and 3
volatile int counter;
void counterFunc(){
counter++;
}
void setup(){
Serial.begin(9600);
pinMode(pinOut,OUTPUT);
pinMode(pinToInterrupt,INPUT);
PORTB = B00000001; // Set digital pin 8 to high through PORTB register
attachInterrupt(digitalPinToInterrupt(pinToInterrupt),counterFunc, RISING); //Interrupt occurs when as soon as pin 2 go from LOW to HIGH
}
void loop(){
Serial.print("The counter is: "); Serial.println(counter);
}