That does work for me but not quite what I am looking for...
So it's true what some (but not all) examples say: you have to declare the ISR before the attachinterrupt()?
If I modify the code slightly as below - why doesn't this work?
I would expect the LED to be on when the board boots, then toggle on/off when I press my switch. Instead the LED is off when the board boots and does not turn on when I press the switch.
What is triggering the interrupt on pin 2? In Whandall's code, the loop() function is toggling pin 2. In your code, nothing is toggling pin 2. Unless you have an external interrupt source applied to pin 2, the behavior you see is what I would expect.
Do I need to set internal pull-down resistor on pin 2?
You could try. The Arduino doesn't have internal pulldown resistors. It only has internal pullup resistors. If you don't have an external resistor, you have a floating pin condition - hardly useful for triggering an interrupt.
I have copied your code and it is working - kinda. I am getting a flickering LED which turns fully on/off on button push, then goes back to flickering after a 1/2 second or so.
I have got to this point before, with the flickering. I appreciate that my button is not debounced but the behavior does not change even when I remove my two lead wires from the switch.
Seems like such a basic function, not sure why I am struggling so much with it.
If you activate the internal pullup - as my suggested code did - you should not need an external one.
But you will have to debounce the button probably.
The first code I posted switched the port out output to use the software interrupt.
This works with pinchange interrupts also, but you have change events only.