Ardweeny simple interrupt test - fires constantly

Prior to using interrupts for the first time in project, I did a simple test using the code below.

The interrupt fires constantly and the LCD shows variable "i" incrementing rapidly.

This happens with pin D0 connected to my switch debounce circuit (shown in the upper right in the photo below). It also happens with pin D0 connected directly to ground or to +Vcc via 10k resistor.

My little commented-out test works - I can read pin D0 and make the LED change state that way.

The same problem occurs if I use pin D1.

Google searching shows constant firing when pin is unconnected (due to noise), but that's not my issue since I'm grounding (or holding high) via resistor.

Any suggestions would be appreciated!

// simple interrupt test
//
// prior to upload and when FTDI is attached
// disconnect wires to interrupt pins
// (used by FTDI for serial comm)

// includes
#include <LiquidCrystal.h>

// defines
int pin = 13;
volatile int i = 0;
volatile int state = LOW;

// the following puts all LCD wires on the same side of Ardweeny
LiquidCrystal lcd(2, 3, 5, 4, 6, 7);

void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);

lcd.begin(8, 2);
lcd.setCursor(0, 0);
lcd.print("hello!");

}

void loop()
{
// TEST
// can we turn off interrupts
// and just read the pin and change the state
// based on pin state? yes...this works
// state = digitalRead(0);

digitalWrite(pin, state);

lcd.setCursor(0, 1);
lcd.print("i=");
lcd.print(i);

}

void blink()
{
//ISR..toggle state on interrupt and increment counter
state = !state;
i += 1;
}

The attachInterrupt(0,...) actually uses digital pin 2 - and attachInterrupt(1,...) uses pin 3.
You'll have to rewire your LCD so that it doesn't use pin 2.

Pete

P.S. IIRC you also have to declare pin 2 as an input before attaching the interrupt.

P.P.S put your code inside code tags - not quote tags.

Pete

Thank you!

Oh, how embarrassing! :blush: I thought pin 2 meant physical pin 2, not digital pin 2. But in retrospect, it wouldn't make much sense to have the serial pins be the interrupt pins.

Well, that's a relief, sort of: I thought maybe my chip was bad; in fact, it is "just" my brain that is bad!

Dave

PS: Thanks for the hint about code tags. I'd read that in the "how to use the forum," but then when I right-clicked on the code, it offered "copy for forum," so that's what I tried...

The "copy for forum" is a nuisance because, as you've found, it doesn't do what it implies.

Pete