Hi All,
I have this behaviour in my code which I can not explain.
Please have a look and let me know if this is normal.
int sensePin = 2;
volatile unsigned int counter = 0;
void detect() // just a varialble to keep track of the number of hits
{
counter = counter + 1;
}
void setup()
{
pinMode(sensePin, INPUT);
attachInterrupt(0,detect,FALLING);
Serial.begin(9600);
Serial.flush();
}
void loop()
{
if (Serial.available() > 0)
{
Serial.println(counter);
Serial.flush();
}
}
I have a switch connected via a pull-up resistor to pin 2.
When I push the switch, the interrupt called 'detect' is triggered and counter is increased with 1.
That seems to work. To check this, I open Serial Monitor which sends me the value of counter every time I enter a character.
At first the value = 0, but that changes every time I push the button.
So far so good.
When I close the serial monitor and reopen it, then counter seems to be reset to 0 !! Why?
Does closing Arduino serial monitor reset the board ??
Any suggestions? Can it be avoided?