Interrupt not interrupting, what am I doing wrong?

I’m trying to get a interrupt to count pulses from a IC, but it’s not interrupting.

I know it’s not the chip because I have hooked up a switch right to the interrupt pin, and despite switching 5v to it, still cannot get it to interrupt.

I know it’s not interrupting, or at least the ISR function is not running because the variable in the ISR is not incrementing and the serial debug message in the ISR is not transmitted, even though other serial debug messages are working.

I am using digital pin 2 but I attach the interrupt using 0, which is what the docs say to do.

I have tried setting the digital pin 2 to an input, even though the docs say nothing about having to do so

I tried explicitly declaring the variable in the ISR as volatile, but found no difference

There must be some detail about using interrupts that I don’t know about. This is the first time I have tried to set one up, so it must be me. I cannot move forward with the project without interrupts so if anyone can help me I would really appreciate it.

in setup():

attachInterrupt(0,pulsecounter,RISING);

in loop():

//for debugging
Serial.println(pulses);
delay (500)

At the end of my code, outside both setup() and loop():

/*********************************************
    pulsecounter
**********************************************
this function increments a value on every pulse on the interrupt pin, thus counting pulses from the transducer*/

void pulsecounter(){
  pulses++;
  Serial.println("interrupted");
  return;
}

Generally speaking, putting stuff like "print" in interrupt routines is a no-no. Try blinking a LED.

void pulsecounter(){
  pulses++;
  Serial.println("interrupted");
  return;
}

"return" is redundant.

I know. However, I've been trying everything to get this fixed.

So, you've taken the print out of the ISR? Are you sure the source of interrupts is running?

The print wasn't in the ISR originally, and it didn't work then. I added it later. To eliminate the chance that the source of the interrupt wasn't working, I tested it by switching 5v directly to the pin with a switch. I measured it with a voltmeter to confirm that the pin was going high and then low.

I think tonight I will try using digitalWrite on the interrupt pin, see if I can get an interrupt that way.

I removed the Serial.print from the ISR. It's worth noting that before I did so, I tried to run the program a few times while switching voltage to the pin and actually got the debug message to print, and the variable in the ISR to increment to 12, which is not the number of times I pressed the switch, on one occasion. So it worked, once, and I don't know why.

I also changed my mode from RISING to CHANGE, just for giggles.

I also modified my code to set pin 2 as an output, and hooked up an LED. I used digitalWrite and delay to blink it every 50ms, while it was still supposed to have an interrupt attached to it. It blinks as expected but the variable in the ISR does not increment. The Atmega datasheet says that you can trigger interrupts by using the internal pullup resistor, so I believe it should be working.

which is not the number of times I pressed the switch, on one occasion

That could be switch bounce.

Yes it could, however I wish I could reproduce even switch bounce.

I am using an interrupt on Pin 2, in a very similar way. I cannot see anything wrong from what you have posted. Can you post your all of your sketch, and I will try to reproduce the interrupt working, or not. (bank holiday here, so long weekend)

What board, and what compilation environment are you using? 0012 had a buggy compiler that was not saving all registers in some circumstances.

The board is a Duemilanove. I do not know about the compilation environment, but avr-gcc --version returns

avr-gcc (GCC) 4.3.2

I could post all of my sketch, but I'm afraid I would be embarrassed. It has a lot of fluff having to do with human-interfacing and such, so it might be confusing.

could post all of my sketch, but I'm afraid I would be embarrassed

So, cut out the stuff you really don't need (anything with serial comms involved), put in some simple LED blinking code. Your sketch will end up probably no more than 20 lines long.

You don't go to three sessions of night-school French and then set out to read "A la recherche du temps perdu".

Don't try the same here.

I found it. It was a bug in my code. Possibly had I posted the code someone would have seen it. I plan to eventually have two interrupts in my code. I had moved this interrupt in question to use the digital pin 2 (interrupt 0) but I did not comment out the other interrupt, which had been set to use the same pin (it's the only open interrupt pin on my board at the moment). Since it came after the first one, it attached itself to the pin and its own ISR was being run when the interrupt occurred on the pin, and not the one I was expecting.

better sense, i [myself] am also just now reading and learning about interrupts,

i don't know if you've found out what was wrong yet, or not; and, i am not sure if i'm right or now, but:

i've read that when using attachInterrupt, you cannot call parameters, [u]nor return anything[/u]... to me, it implies that your function is adding, but then the information is lose as you exit the interrupt function...

i believe i've thought to have read that other people write their own ISRs (interrupt service routines) -- instead of using attachInterrupt -- for this reason...

again, i know nothing about interrupts yet, but let me know if this helps, and if i am right, as this will help me understand more about interrupts.