Skip to content

Commit c13425d

Browse files
committed
Updated BinarySwitchSensorInt to handle two sensors
The new sleep() constructor allows for two interrupt sources to wake device from sleep. This example is now updated to illustrate this.
1 parent 35788d6 commit c13425d

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

libraries/MySensors/examples/BinarySwitchSensorInt/BinarySwitchSensorInt.ino

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,90 @@
1-
// Interrupt driven binary switch example
1+
// Interrupt driven binary switch example with dual interrupts
22
// 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.
56
// This example is designed to fit Arduino Nano/Pro Mini
67

78
#include <MySensor.h>
89
#include <SPI.h>
910

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"
1214

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
1620

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;
1835

1936
// 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);
2139

2240
void setup()
2341
{
24-
gw.begin();
42+
sensor_node.begin();
2543

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);
3051

3152
// 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);
3354

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)
3556
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
3657
// 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);
3860
}
3961

40-
// Loop will iterate on changes on the BUTTON_PIN
62+
// Loop will iterate on changes on the BUTTON_PINs
4163
void loop()
4264
{
4365
uint8_t value;
4466
static uint8_t sentValue=2;
67+
static uint8_t sentValue2=2;
4568

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);
4871

49-
value = digitalRead(BUTTON_PIN);
72+
value = digitalRead(PRIMARY_BUTTON_PIN);
5073

5174
if (value != sentValue) {
5275
// 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));
5477
sentValue = value;
5578
}
5679

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+
5788
// 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);
5990
}
60-

0 commit comments

Comments
 (0)