Guest User

Untitled

a guest
Dec 19th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | Software | 0 0
  1. //#define MY_DEBUG
  2. #define MY_GATEWAY_SERIAL
  3. #define MY_SERIALDEVICE Serial
  4. #define MY_BAUD_RATE 115200
  5.  
  6. #include <MySensors.h>
  7.  
  8. #define CHILD_ID_CONTACT_1 101
  9. #define CONTACT_PIN_1 2
  10. #define CHILD_ID_CONTACT_2 102
  11. #define CONTACT_PIN_2 3
  12.  
  13. MyMessage msg1(CHILD_ID_CONTACT_1, V_ARMED);
  14. MyMessage msg2(CHILD_ID_CONTACT_2, V_TRIPPED);
  15.  
  16. bool lastState1, lastState2;
  17.  
  18. void presentation() {
  19.   sendSketchInfo("Diagral alarm status", "1.0");
  20.   present(CHILD_ID_CONTACT_1, S_DOOR);
  21.   present(CHILD_ID_CONTACT_2, S_DOOR);
  22. }
  23.  
  24. void setup() {
  25.   pinMode(CONTACT_PIN_1, INPUT_PULLUP);
  26.   pinMode(CONTACT_PIN_2, INPUT_PULLUP);
  27.  
  28.   // Read initial state
  29.   lastState1 = digitalRead(CONTACT_PIN_1);
  30.   send(msg1.set(lastState1 == LOW ? 1 : 0));
  31.  
  32.   lastState2 = digitalRead(CONTACT_PIN_2);
  33.   send(msg2.set(lastState2 == LOW ? 1 : 0));  
  34. }
  35.  
  36. void loop() {
  37.   bool currentState;
  38.  
  39.   currentState = digitalRead(CONTACT_PIN_1);
  40.  
  41.   if (currentState != lastState1) {
  42.     // LOW = contact closed → TRIPPED
  43.     send(msg1.set(currentState == LOW ? 1 : 0));
  44.     lastState1 = currentState;
  45.   }
  46.  
  47.   currentState = digitalRead(CONTACT_PIN_2);
  48.  
  49.   if (currentState != lastState2) {
  50.     // LOW = contact closed → TRIPPED
  51.     send(msg2.set(currentState == LOW ? 1 : 0));
  52.     lastState2 = currentState;
  53.   }
  54.  
  55.   // Small debounce delay
  56.   delay(50);
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment