Ref:
http://www.dave-auld.net/index.php?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107
int pbIn = 0; // Interrupt 0 is on DIGITAL PIN 2!
02.int ledOut = 4; // The output LED pin
03.volatile int state = LOW; // The input state toggle
04.
05.void setup()
06.{
07.// Set up the digital pin 2 to an Interrupt and Pin 4 to an Output
08.pinMode(ledOut, OUTPUT);
09.
10.//Attach the interrupt to the input pin and monitor for ANY Change
11.attachInterrupt(pbIn, stateChange, CHANGE);
12.}
13.
14.void loop()
15.{
16.//Simulate a long running process or complex task
17.for (int i = 0; i < 100; i++)
18.{
19.// do nothing but waste some time
20.delay(10);
21.}
22.}
23.
24.void stateChange()
25.{
26.state = !state;
27.digitalWrite(ledOut, state);
28.}
Very nice informative post,,
ReplyDelete