I'm not good at programming



  • 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());
    

    }
    }



  • This post is deleted!


  • 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?



  • 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



  • Can you explain the problem a bit more?



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


 

247
Online

7.8k
Users

8.7k
Topics

93.0k
Posts