AttachInterrupt() help..


new arduino..not sure im doing wrong..maybe guys can me out? =)

this first time trying use external interrupt..

as understand it.. these 'listeners' check state of 'pin', without having code dependent?  ie: not having code keep 'polling/checking' pin status.

question 1:
i know arduino due says has 2 external interrupt pins..
is there way more these two? (i thought read pin change?..but bit on head then..since didnt have arduino yet)


what im trying (i thought) simple..

i have 'sensor'.. wich nothing more spring/nail type sensor





any time sensor completes circuit/connects.. should trigger event. (whatver function noted in parameter of attachinterrupt() function.)

however.. can not seem consistent results..

it should make noice (play tone) or light led when connection happens..then pause 500 ms.. turn off again..waiting interrupt fire again..


code: [select]

int pin = 13;
volatile int state = low;

void setup()
{
 // initialize serial communications:
 serial.begin(9600);
 //set pim mode
 pinmode(pin, output);
 //attach interrupt 'listener' pin2 on arduino
 attachinterrupt(0, blink, high);
}

void loop()
{
 if(state == high){
   digitalwrite(pin, state);
   delay(500);
   state = low;
 }
}

void blink()
{
 serial.println("--event triggered--");
 //tone(8, 350, 250);
 state = high;  
}




question 2:
after interrupt triggered.. should remove it? until after code/routine has executed..then re-enable/re-attach interrupt?

think of 'sensor' impact sensor.. everytime 'flick' it.. or project hit against something.. want sound/light go off.. (and of course off again).. everytime stirke te project.. shoudl repeat process..  even if repeatedly 'hit it'..

should keep executing routine on , over..


thanks

hi, first off - don't use 'serial.print' inside interupt service routine - comment out , try again.

also, may have better luck if use 'change' in attach... line - 'catch' rising , falling edges of signal.

after interrupt triggered - leave isr there catch next event.

charles


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > AttachInterrupt() help..


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums