robobuggy / arduino / InterruptRCRecieverReference / PinChangeInt / Examples / PinChangeIntExample / PinChangeIntExample.pde @ c5d6b0e8
History | View | Annotate | Download (3.28 KB)
1 |
// PinChangeIntExample, version 1.1 Sun Jan 15 06:24:19 CST 2012 |
---|---|
2 |
// See the Wiki at http://code.google.com/p/arduino-pinchangeint/wiki for more information. |
3 |
//-------- define these in your sketch, if applicable ---------------------------------------------------------- |
4 |
// You can reduce the memory footprint of this handler by declaring that there will be no pin change interrupts |
5 |
// on any one or two of the three ports. If only a single port remains, the handler will be declared inline |
6 |
// reducing the size and latency of the handler. |
7 |
//#define NO_PORTB_PINCHANGES // to indicate that port b will not be used for pin change interrupts |
8 |
//#define NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts |
9 |
// #define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts |
10 |
// if there is only one PCInt vector in use the code can be inlined |
11 |
// reducing latency and code size |
12 |
// define DISABLE_PCINT_MULTI_SERVICE below to limit the handler to servicing a single interrupt per invocation. |
13 |
// #define DISABLE_PCINT_MULTI_SERVICE |
14 |
//-------- define the above in your sketch, if applicable ------------------------------------------------------ |
15 |
#include <PinChangeInt.h> |
16 |
|
17 |
// This example demonstrates a configuration of 3 interrupting pins and 2 interrupt functions. |
18 |
// All interrupts are serviced immediately, but one of the pins (pin 4) will show you immediately |
19 |
// on the Terminal. The other function connected to 2 pins sets an array member that is queried in loop(). |
20 |
// You can then query the array at your leisure. |
21 |
// This makes loop timing non-critical. |
22 |
|
23 |
// Add more Pins at your leisure. |
24 |
// For the Analog Input pins used as digital input pins, and you can use 14, 15, 16, etc. |
25 |
// or you can use A0, A1, A2, etc. (the Arduino code comes with #define's |
26 |
// for the Analog Input pins and will properly recognize e.g., pinMode(A0, INPUT); |
27 |
#define PIN1 2 |
28 |
#define PIN2 3 |
29 |
#define PIN3 4 |
30 |
|
31 |
uint8_t latest_interrupted_pin; |
32 |
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins |
33 |
void quicfunc() { |
34 |
latest_interrupted_pin=PCintPort::arduinoPin; |
35 |
interrupt_count[latest_interrupted_pin]++; |
36 |
}; |
37 |
|
38 |
// You can assign any number of functions to any number of pins. |
39 |
// How cool is that? |
40 |
void pin3func() { |
41 |
Serial.print("Pin "); Serial.print(PIN3, DEC); Serial.println("!"); |
42 |
} |
43 |
|
44 |
void setup() { |
45 |
pinMode(PIN1, INPUT); digitalWrite(PIN1, HIGH); |
46 |
PCintPort::attachInterrupt(PIN1, &quicfunc, FALLING); // add more attachInterrupt code as required |
47 |
pinMode(PIN2, INPUT); digitalWrite(PIN2, HIGH); |
48 |
PCintPort::attachInterrupt(PIN2, &quicfunc, FALLING); |
49 |
pinMode(PIN3, INPUT); digitalWrite(PIN3, HIGH); |
50 |
PCintPort::attachInterrupt(PIN3, &pin3func, CHANGE); |
51 |
Serial.begin(115200); |
52 |
Serial.println("---------------------------------------"); |
53 |
} |
54 |
|
55 |
uint8_t i; |
56 |
void loop() { |
57 |
uint8_t count; |
58 |
Serial.print("."); |
59 |
delay(1000); |
60 |
for (i=0; i < 20; i++) { |
61 |
if (interrupt_count[i] != 0) { |
62 |
count=interrupt_count[i]; |
63 |
interrupt_count[i]=0; |
64 |
Serial.print("Count for pin "); |
65 |
if (i < 14) { |
66 |
Serial.print("D"); |
67 |
Serial.print(i, DEC); |
68 |
} else { |
69 |
Serial.print("A"); |
70 |
Serial.print(i-14, DEC); |
71 |
} |
72 |
Serial.print(" is "); |
73 |
Serial.println(count, DEC); |
74 |
} |
75 |
} |
76 |
} |
77 |
|