|
1 | | -// Interrupt driven binary switch example |
| 1 | +// Interrupt driven binary switch example with dual interrupts |
2 | 2 | // Author: Patrick 'Anticimex' Fallberg |
3 | | -// Connect button or door/window reed switch between |
4 | | -// digitial I/O pin 3 (BUTTON_PIN below) and GND. |
| 3 | +// Connect one button or door/window reed switch between |
| 4 | +// digitial I/O pin 3 (BUTTON_PIN below) and GND and the other |
| 5 | +// one in similar fashion on digital I/O pin 2. |
5 | 6 | // This example is designed to fit Arduino Nano/Pro Mini |
6 | 7 |
|
7 | 8 | #include <MySensor.h> |
8 | 9 | #include <SPI.h> |
9 | 10 |
|
10 | | -#define CHILD_ID 3 |
11 | | -#define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch |
| 11 | +#define SKETCH_NAME "Binary Sensor" |
| 12 | +#define SKETCH_MAJOR_VER "1" |
| 13 | +#define SKETCH_MINOR_VER "0" |
12 | 14 |
|
13 | | -#if (BUTTON_PIN < 2 || BUTTON_PIN > 3) |
14 | | -#error BUTTON_PIN must be either 2 or 3 for interrupts to work |
15 | | -#endif |
| 15 | +#define PRIMARY_CHILD_ID 3 |
| 16 | +#define SECONDARY_CHILD_ID 4 |
| 17 | + |
| 18 | +#define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch |
| 19 | +#define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch |
16 | 20 |
|
17 | | -MySensor gw; |
| 21 | +#if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3) |
| 22 | +#error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work |
| 23 | +#endif |
| 24 | +#if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3) |
| 25 | +#error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work |
| 26 | +#endif |
| 27 | +#if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN) |
| 28 | +#error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same |
| 29 | +#endif |
| 30 | +#if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID) |
| 31 | +#error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same |
| 32 | +#endif |
| 33 | + |
| 34 | +MySensor sensor_node; |
18 | 35 |
|
19 | 36 | // Change to V_LIGHT if you use S_LIGHT in presentation below |
20 | | -MyMessage msg(CHILD_ID,V_TRIPPED); |
| 37 | +MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED); |
| 38 | +MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED); |
21 | 39 |
|
22 | 40 | void setup() |
23 | 41 | { |
24 | | - gw.begin(); |
| 42 | + sensor_node.begin(); |
25 | 43 |
|
26 | | - // Setup the button |
27 | | - pinMode(BUTTON_PIN,INPUT); |
28 | | - // Activate internal pull-up |
29 | | - digitalWrite(BUTTON_PIN,HIGH); |
| 44 | + // Setup the buttons |
| 45 | + pinMode(PRIMARY_BUTTON_PIN, INPUT); |
| 46 | + pinMode(SECONDARY_BUTTON_PIN, INPUT); |
| 47 | + |
| 48 | + // Activate internal pull-ups |
| 49 | + digitalWrite(PRIMARY_BUTTON_PIN, HIGH); |
| 50 | + digitalWrite(SECONDARY_BUTTON_PIN, HIGH); |
30 | 51 |
|
31 | 52 | // Send the sketch version information to the gateway and Controller |
32 | | - gw.sendSketchInfo("Binary Sensor", "1.0"); |
| 53 | + sensor_node.sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER"."SKETCH_MINOR_VER); |
33 | 54 |
|
34 | | - // Register binary input sensor to gw (they will be created as child devices) |
| 55 | + // Register binary input sensor to sensor_node (they will be created as child devices) |
35 | 56 | // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. |
36 | 57 | // If S_LIGHT is used, remember to update variable type you send in. See "msg" above. |
37 | | - gw.present(CHILD_ID, S_DOOR); |
| 58 | + sensor_node.present(PRIMARY_CHILD_ID, S_DOOR); |
| 59 | + sensor_node.present(SECONDARY_CHILD_ID, S_DOOR); |
38 | 60 | } |
39 | 61 |
|
40 | | -// Loop will iterate on changes on the BUTTON_PIN |
| 62 | +// Loop will iterate on changes on the BUTTON_PINs |
41 | 63 | void loop() |
42 | 64 | { |
43 | 65 | uint8_t value; |
44 | 66 | static uint8_t sentValue=2; |
| 67 | + static uint8_t sentValue2=2; |
45 | 68 |
|
46 | | - // Short delay to allow button to properly settle |
47 | | - gw.sleep(5); |
| 69 | + // Short delay to allow buttons to properly settle |
| 70 | + sensor_node.sleep(5); |
48 | 71 |
|
49 | | - value = digitalRead(BUTTON_PIN); |
| 72 | + value = digitalRead(PRIMARY_BUTTON_PIN); |
50 | 73 |
|
51 | 74 | if (value != sentValue) { |
52 | 75 | // Value has changed from last transmission, send the updated value |
53 | | - gw.send(msg.set(value==HIGH ? 1 : 0)); |
| 76 | + sensor_node.send(msg.set(value==HIGH ? 1 : 0)); |
54 | 77 | sentValue = value; |
55 | 78 | } |
56 | 79 |
|
| 80 | + value = digitalRead(SECONDARY_BUTTON_PIN); |
| 81 | + |
| 82 | + if (value != sentValue2) { |
| 83 | + // Value has changed from last transmission, send the updated value |
| 84 | + sensor_node.send(msg2.set(value==HIGH ? 1 : 0)); |
| 85 | + sentValue2 = value; |
| 86 | + } |
| 87 | + |
57 | 88 | // Sleep until something happens with the sensor |
58 | | - gw.sleep(BUTTON_PIN-2, CHANGE, 0); |
| 89 | + sensor_node.sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0); |
59 | 90 | } |
60 | | - |
|
0 commit comments