Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. I'm not good at programming

I'm not good at programming

Scheduled Pinned Locked Moved Troubleshooting
6 Posts 2 Posters 856 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • zrom69Z Offline
    zrom69Z Offline
    zrom69
    wrote on last edited by zrom69
    #1

    Hello I'm not good at programming I mix two motion light my times his block is what a he can look at thank

    #define MY_DEBUG

    #define MY_RF24_CE_PIN A0
    #define MY_RADIO_NRF24
    #define MY_REPEATER_FEATURE

    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>

    #define MOTION_PIN 4
    #define RELAY_ON 1
    #define RELAY_OFF 0

    #define SSR_A_ID 1 // Id of the sensor child
    #define MOTION_ID 2

    // Sleep time between reads (in milliseconds)
    unsigned long SLEEP_TIME = 100000;

    unsigned long T = 1;

    const int buttonPinA = 3;
    const int relayPinA = 5;
    int oldValueA = 0;
    bool stateA = false;
    bool lastMotion;

    Bounce debouncerA = Bounce();

    MyMessage msgA(SSR_A_ID, V_STATUS);
    MyMessage motion_msg(MOTION_ID, V_TRIPPED);

    void setup()
    {

    pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
    // Then set relay pins in output mode
    pinMode(relayPinA, OUTPUT);

    // After setting up the buttons, setup debouncer
    debouncerA.attach(buttonPinA);
    debouncerA.interval(5);

    // Make sure relays are off when starting up
    digitalWrite(relayPinA, RELAY_OFF);

    // Define the motion sensor pin for digital input
    pinMode(MOTION_PIN, INPUT);

    /*--------------------- Added these lines for toggle switch-------------------------*/
    

    oldValueA = digitalRead(buttonPinA); // set oldValueA to the current status of the toggle switch
    send(msgA.set(false)); // Send off state for relayA to ensure controller knows the switch is off

    }

    void presentation() {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Mains Controller", "1.0");

    // Register all sensors to gw (they will be created as child devices)
    present(SSR_A_ID, S_LIGHT);

    // Register the motion sensor to the gateway
    present(MOTION_ID, S_MOTION);
    }

    /*
    Example on how to asynchronously check for new messages from gw
    */
    void loop()
    {
    debouncerA.update();
    // Get the update value
    int valueA = debouncerA.read();
    if (valueA != oldValueA) {
    send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
    oldValueA = valueA;
    }
    // Read digital motion value
    bool motion = digitalRead(MOTION_PIN) == HIGH;

    if (motion != lastMotion) {
    // Send tripped value to gw
    send(motion_msg.set(motion ? "1" : "0"));
    lastMotion = motion;
    }
    T++;
    if (T > SLEEP_TIME) {
    T = 1;
    Serial.print("Motion: ");
    Serial.println(motion);

    }
    

    }

    void receive(const MyMessage &message) {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type == V_STATUS) {

    switch (message.sensor) {
      case 1:
        stateA = message.getBool();
        digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
        
        break;
      
    }
    
      // Write some debug info
    Serial.print("Incoming change for sensor:");
    Serial.println(message.sensor);
    Serial.print("from node:");
    Serial.println(message.sender);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
    

    }
    }

    zrom69Z 1 Reply Last reply
    0
    • zrom69Z zrom69

      Hello I'm not good at programming I mix two motion light my times his block is what a he can look at thank

      #define MY_DEBUG

      #define MY_RF24_CE_PIN A0
      #define MY_RADIO_NRF24
      #define MY_REPEATER_FEATURE

      #include <SPI.h>
      #include <MySensors.h>
      #include <Bounce2.h>

      #define MOTION_PIN 4
      #define RELAY_ON 1
      #define RELAY_OFF 0

      #define SSR_A_ID 1 // Id of the sensor child
      #define MOTION_ID 2

      // Sleep time between reads (in milliseconds)
      unsigned long SLEEP_TIME = 100000;

      unsigned long T = 1;

      const int buttonPinA = 3;
      const int relayPinA = 5;
      int oldValueA = 0;
      bool stateA = false;
      bool lastMotion;

      Bounce debouncerA = Bounce();

      MyMessage msgA(SSR_A_ID, V_STATUS);
      MyMessage motion_msg(MOTION_ID, V_TRIPPED);

      void setup()
      {

      pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
      // Then set relay pins in output mode
      pinMode(relayPinA, OUTPUT);

      // After setting up the buttons, setup debouncer
      debouncerA.attach(buttonPinA);
      debouncerA.interval(5);

      // Make sure relays are off when starting up
      digitalWrite(relayPinA, RELAY_OFF);

      // Define the motion sensor pin for digital input
      pinMode(MOTION_PIN, INPUT);

      /*--------------------- Added these lines for toggle switch-------------------------*/
      

      oldValueA = digitalRead(buttonPinA); // set oldValueA to the current status of the toggle switch
      send(msgA.set(false)); // Send off state for relayA to ensure controller knows the switch is off

      }

      void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Mains Controller", "1.0");

      // Register all sensors to gw (they will be created as child devices)
      present(SSR_A_ID, S_LIGHT);

      // Register the motion sensor to the gateway
      present(MOTION_ID, S_MOTION);
      }

      /*
      Example on how to asynchronously check for new messages from gw
      */
      void loop()
      {
      debouncerA.update();
      // Get the update value
      int valueA = debouncerA.read();
      if (valueA != oldValueA) {
      send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
      oldValueA = valueA;
      }
      // Read digital motion value
      bool motion = digitalRead(MOTION_PIN) == HIGH;

      if (motion != lastMotion) {
      // Send tripped value to gw
      send(motion_msg.set(motion ? "1" : "0"));
      lastMotion = motion;
      }
      T++;
      if (T > SLEEP_TIME) {
      T = 1;
      Serial.print("Motion: ");
      Serial.println(motion);

      }
      

      }

      void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {

      switch (message.sensor) {
        case 1:
          stateA = message.getBool();
          digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
          
          break;
        
      }
      
        // Write some debug info
      Serial.print("Incoming change for sensor:");
      Serial.println(message.sensor);
      Serial.print("from node:");
      Serial.println(message.sender);
      Serial.print(", New status: ");
      Serial.println(message.getBool());
      

      }
      }

      zrom69Z Offline
      zrom69Z Offline
      zrom69
      wrote on last edited by zrom69
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • alowhumA Offline
        alowhumA Offline
        alowhum
        Plugin Developer
        wrote on last edited by
        #3

        So, if I get this right..

        You want a motion detection sensor? And you tried turning a button node into a motion sensing node by having the motion sensor acting as the button?

        And you want two motion sensors?
        Or do you want to be able to turn on the light and/or sense motion at the same time? You code seems to imply the second.

        So.. what is the problem? Is it not working?

        This is odd:
        #define SSR_A_ID 1 // Id of the sensor child

        A solid sate relay (SSR) is an actuator, not a sensor. Did you try to turn that into a second sensor?

        If you want to have two motion sensors connected, you should probably have two of these:
        MyMessage motion_msg(MOTION_ID, V_TRIPPED);
        (or set the child ID and use one message holder)

        Here you are writing to a relay though:
        // Make sure relays are off when starting up
        digitalWrite(relayPinA, RELAY_OFF);

        Hmmm, you're counting the loops? Up to 100.000? Hmm, I wonder how long that takes in milliseconds. In any case, your node will have trouble receiving any command form the gateway if it's sleeping.

        I'm not sure how that debouncer works.

        What's this all about?
        digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
        Why the +4? If you only have one relay, why not just point to that pin directly?

        1 Reply Last reply
        0
        • zrom69Z Offline
          zrom69Z Offline
          zrom69
          wrote on last edited by
          #4

          thank you for your response
          light with classic toggle switch
          and i have add pir sensor
          for what I ask to correct
            I have to thank you

          1 Reply Last reply
          0
          • alowhumA Offline
            alowhumA Offline
            alowhum
            Plugin Developer
            wrote on last edited by
            #5

            Can you explain the problem a bit more?

            1 Reply Last reply
            0
            • zrom69Z Offline
              zrom69Z Offline
              zrom69
              wrote on last edited by
              #6

              in domoticz he sends his state of light
              it is he and turn on or off

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              22

              Online

              11.7k

              Users

              11.2k

              Topics

              113.1k

              Posts


              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • MySensors
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular