Problem with interrupts

hi i’m new with arduino and i have a problem with interrupts, i was testing this code:

int pin = 10;
volatile int state = LOW;

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

void loop()
{
  digitalWrite(pin, state);
}

void blink()
{
  state = !state;
}

but im not sure about why the led change when i connect to ground, if i conect to 5 volts nothing happens, i have an arduinomega

thanks

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

  1. code tags please!

  2. connect what to 5V or ground? where is the LED?

sorry for the code tags, and i connected the interrupt pin (pin2) to 5v, this is like a sensor signal i think ,so why does not activate the interrupt ??? and the led is on pin10

thanks

Do you know if the LED actually works? It starts off LOW… if it’s faulty then it might be trying to go on when you apply the interrupt but you can’t see it.

Maybe change the line volatile int state = [glow=yellow,2,300]LOW[/glow]; to volatile int state = [glow=yellow,2,300]HIGH[/glow]; so it starts in the “on” state and you’ll be confident it actually works.

thank you jimboza, I tried your idea, but the LED works well, and something strange happens when I connect pin2 to ground, changes state :S

If you move pin 2 from Gnd to +5V (and back) does the LED toggle?

pt_t: ...and something strange happens ...

And what is that strange thing?

if I connect the ground to pin2 the state of LED changes or just connect a wire to pin2( i dont know why) , but if a connect 5 volts and back nothings append .....

and now I'm testing on RISING mode

is it clear? maybe my English is another problem xD

The pin needs a defined state. Just connecting and disconnecting a wire is not that. Enable the internal pull-ups, or use an external resistor.