Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define MY_DEBUG
- #define MY_GATEWAY_SERIAL
- #define MY_SERIALDEVICE Serial
- #define MY_BAUD_RATE 115200
- #include <MySensors.h>
- #define CHILD_ID_CONTACT_1 101
- #define CONTACT_PIN_1 2
- #define CHILD_ID_CONTACT_2 102
- #define CONTACT_PIN_2 3
- MyMessage msg1(CHILD_ID_CONTACT_1, V_ARMED);
- MyMessage msg2(CHILD_ID_CONTACT_2, V_TRIPPED);
- bool lastState1, lastState2;
- void presentation() {
- sendSketchInfo("Diagral alarm status", "1.0");
- present(CHILD_ID_CONTACT_1, S_DOOR);
- present(CHILD_ID_CONTACT_2, S_DOOR);
- }
- void setup() {
- pinMode(CONTACT_PIN_1, INPUT_PULLUP);
- pinMode(CONTACT_PIN_2, INPUT_PULLUP);
- // Read initial state
- lastState1 = digitalRead(CONTACT_PIN_1);
- send(msg1.set(lastState1 == LOW ? 1 : 0));
- lastState2 = digitalRead(CONTACT_PIN_2);
- send(msg2.set(lastState2 == LOW ? 1 : 0));
- }
- void loop() {
- bool currentState;
- currentState = digitalRead(CONTACT_PIN_1);
- if (currentState != lastState1) {
- // LOW = contact closed → TRIPPED
- send(msg1.set(currentState == LOW ? 1 : 0));
- lastState1 = currentState;
- }
- currentState = digitalRead(CONTACT_PIN_2);
- if (currentState != lastState2) {
- // LOW = contact closed → TRIPPED
- send(msg2.set(currentState == LOW ? 1 : 0));
- lastState2 = currentState;
- }
- // Small debounce delay
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment