Pages: [1]   Go Down
Author Topic: Problem with interrupts  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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: [code] [/code] tags added.
« Last Edit: January 03, 2013, 10:23:10 pm by Coding Badly » Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10445
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1) code tags please!

2) connect _what_ to 5V or ground?  where is the LED?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 = LOW; to volatile int state = HIGH; so it starts in the "on" state and you'll be confident it actually works.
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...and something strange happens ...

And what is that strange thing?
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: