Skip to content

Commit dd50aa9

Browse files
committed
Merge pull request arduino#30 from fallberg/development
Implementation of interrupt driven binary switch
2 parents 76cd614 + 4ffb53b commit dd50aa9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Interrupt driven binary switch example
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.
5+
// This example is designed to fit Arduino Nano/Pro Mini
6+
7+
#include <MySensor.h>
8+
#include <SPI.h>
9+
10+
#define CHILD_ID 3
11+
#define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
12+
13+
#if (BUTTON_PIN < 2 || BUTTON_PIN > 3)
14+
#error BUTTON_PIN must be either 2 or 3 for interrupts to work
15+
#endif
16+
17+
MySensor gw;
18+
19+
// Change to V_LIGHT if you use S_LIGHT in presentation below
20+
MyMessage msg(CHILD_ID,V_TRIPPED);
21+
22+
void setup()
23+
{
24+
gw.begin();
25+
26+
// Setup the button
27+
pinMode(BUTTON_PIN,INPUT);
28+
// Activate internal pull-up
29+
digitalWrite(BUTTON_PIN,HIGH);
30+
31+
// Send the sketch version information to the gateway and Controller
32+
gw.sendSketchInfo("Binary Sensor", "1.0");
33+
34+
// Register binary input sensor to gw (they will be created as child devices)
35+
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
36+
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
37+
gw.present(CHILD_ID, S_DOOR);
38+
}
39+
40+
// Loop will iterate on changes on the BUTTON_PIN
41+
void loop()
42+
{
43+
uint8_t value;
44+
static uint8_t sentValue=2;
45+
46+
// Short delay to allow button to properly settle
47+
gw.sleep(5);
48+
49+
value = digitalRead(BUTTON_PIN);
50+
51+
if (value != sentValue) {
52+
// Value has changed from last transmission, send the updated value
53+
gw.send(msg.set(value==HIGH ? 1 : 0));
54+
sentValue = value;
55+
}
56+
57+
// Sleep until something happens with the sensor
58+
gw.sleep(BUTTON_PIN-2, CHANGE, 0);
59+
}
60+

0 commit comments

Comments
 (0)