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. My Project
  3. Need help with Code. Simple Nod with 4 button's and 2 Led output. Cant get he Led to work as I whant. They respond but both lights

Need help with Code. Simple Nod with 4 button's and 2 Led output. Cant get he Led to work as I whant. They respond but both lights

Scheduled Pinned Locked Moved My Project
mysensors
3 Posts 3 Posters 1.5k Views 3 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.
  • K Offline
    K Offline
    Knightan
    wrote on last edited by
    #1

    I want change state for the LEDS but they both lights when i Press on in Domoticz.

    How to solve the last stage with if (message.type == V_STATUS) // Change relay state

    If command A = Led 1 lights or command B = Led 2 Lights.

    Best regards!
    Bosses leksak.jpg

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_1 1
    #define CHILD_ID_2 2
    #define CHILD_ID_3 3
    #define CHILD_ID_4 4
    #define CHILD_ID_5 5  //LED KNAPP OUTPUT
    #define CHILD_ID_6 6  //LED KNAPP OUTPUT
    
    #define LED_PIN_Channel_1 3 // Relay 
    #define LED_PIN_Channel_2 4 // Relay
    #define BUTTON_PIN_Channel_1 5
    #define BUTTON_PIN_Channel_1 5  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_Channel_2 6 
    #define BUTTON_PIN_Channel_3 7
    #define BUTTON_PIN_Channel_4 8
    
    
    
    #define MY_REPEATER_FEATURE
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    #define MY_DEFAULT_TX_LED_PIN A0
    #define MY_DEFAULT_RX_LED_PIN A1
    
    
    // RELÄ LED
    
    #define RELAY_1_ON 1
    #define RELAY_2_ON 1
    #define RELAY_1_OFF 0
    #define RELAY_2_OFF 0
    bool state_1; // RELÄ 1 
    bool state_2; // RELÄ 2
    
    Bounce debouncer_1 = Bounce(); 
    Bounce debouncer_2 = Bounce(); 
    Bounce debouncer_3 = Bounce(); 
    Bounce debouncer_4 = Bounce(); 
    
    int oldValue_1=-1;
    int oldValue_2=-1;
    int oldValue_3=-1;
    int oldValue_4=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg1(CHILD_ID_1, V_TRIPPED);
    MyMessage msg2(CHILD_ID_2, V_TRIPPED);
    MyMessage msg3(CHILD_ID_3, V_TRIPPED);
    MyMessage msg4(CHILD_ID_4, V_TRIPPED);
    MyMessage msg5(CHILD_ID_5, V_STATUS); //LED
    MyMessage msg6(CHILD_ID_6, V_STATUS); //LED
    
    void setup()  
    {  
      // Setup the button
      // Activate internal pull-up
       // After setting up the button, setup debouncer
      pinMode(BUTTON_PIN_Channel_1,INPUT);
      pinMode(BUTTON_PIN_Channel_2,INPUT);
      pinMode(BUTTON_PIN_Channel_3,INPUT);
      pinMode(BUTTON_PIN_Channel_4,INPUT);
    
    
    //  RELÄ
    
      pinMode(LED_PIN_Channel_1, OUTPUT);
      pinMode(LED_PIN_Channel_2, OUTPUT);  
      pinMode(A0, OUTPUT); // RX TX Använda Analog pin som output 
      pinMode(A1, OUTPUT); // RX TX
    
      digitalWrite(LED_PIN_Channel_1, RELAY_1_OFF);
      digitalWrite(LED_PIN_Channel_2, RELAY_2_OFF);
    
    // Minne från EEPROM
    
       state_1 = loadState(CHILD_ID_5);
      digitalWrite(LED_PIN_Channel_1, state_1?RELAY_1_ON:RELAY_1_OFF);
    
         state_2 = loadState(CHILD_ID_6);
      digitalWrite(LED_PIN_Channel_2, state_2?RELAY_2_ON:RELAY_2_OFF);
      
      digitalWrite(BUTTON_PIN_Channel_1,LOW);
      digitalWrite(BUTTON_PIN_Channel_2,LOW);
      digitalWrite(BUTTON_PIN_Channel_3,LOW);
      digitalWrite(BUTTON_PIN_Channel_4,LOW);
      
      debouncer_1.attach(BUTTON_PIN_Channel_1);
      debouncer_2.attach(BUTTON_PIN_Channel_2);
      debouncer_3.attach(BUTTON_PIN_Channel_3);
      debouncer_4.attach(BUTTON_PIN_Channel_4);
      debouncer_1.interval(5);
      debouncer_2.interval(5);
      debouncer_3.interval(5);
      debouncer_4.interval(5);
      
    }
    
    void presentation() {
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
    
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo(F("Bosses LEKSAK"), F("1.1"));
      present(CHILD_ID_1, S_DOOR); 
      present(CHILD_ID_2, S_DOOR); 
      present(CHILD_ID_3, S_DOOR); 
      present(CHILD_ID_4, S_DOOR);  
      present(CHILD_ID_5, S_BINARY); 
      present(CHILD_ID_6, S_BINARY);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer_1.update();
      debouncer_2.update();
      debouncer_3.update();
      debouncer_4.update();
      
      
      
      // Get the update value
      int value_1 = debouncer_1.read();
      int value_2 = debouncer_2.read();
      int value_3 = debouncer_3.read();
      int value_4 = debouncer_4.read();
     
      if (value_1 != oldValue_1) {
         // Send in the new value
         send(msg1.set(value_1==HIGH ? 1 : 0));
         oldValue_1 = value_1; }
      
    
      if (value_2 != oldValue_2) {
         // Send in the new value
         send(msg2.set(value_2==HIGH ? 1 : 0));
         oldValue_2 = value_2; }
    
      if (value_3 != oldValue_3) {
         // Send in the new value
         send(msg3.set(value_3==HIGH ? 1 : 0));
         oldValue_3 = value_3; }
    
      if (value_4 != oldValue_4) {
         // Send in the new value
         send(msg4.set(value_4==HIGH ? 1 : 0));
         oldValue_4 = value_4; }
    }
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
       if (message.isAck()) {
         Serial.println("This is an ack from gateway");
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    
    
    
         
    }
    
       if (message.type == V_STATUS) {
       // Change relay state
        state_1 = message.getBool();
        digitalWrite(LED_PIN_Channel_1, state_1?RELAY_1_ON:RELAY_1_OFF);
        // Store state in eeprom
        saveState(CHILD_ID_5, state_1);
        
         // Write some debug info
         Serial.print("Incoming change for sensor: STATE 1");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    
    if (message.type == V_STATUS) {
       // Change relay state
        state_2 = message.getBool();
        digitalWrite(LED_PIN_Channel_2, state_2?RELAY_2_ON:RELAY_2_OFF);
        // Store state in eeprom
        saveState(CHILD_ID_6, state_2);
        
         // Write some debug info
         Serial.print("Incoming change for sensor: STATE 2");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    
    
    
       
    }
    
    
    
     
    
    1 Reply Last reply
    0
    • skywatchS Offline
      skywatchS Offline
      skywatch
      wrote on last edited by skywatch
      #2

      @Knightan

      I believe that your problem may be related to this block of code. See my thoughts as comments....

       if (message.type == V_STATUS) {  //This is true for all messages received
         // Change relay state
          state_1 = message.getBool(); //This sets state for state_1 to the received value. 
          digitalWrite(LED_PIN_Channel_1, state_1?RELAY_1_ON:RELAY_1_OFF);
          // Store state in eeprom
          saveState(CHILD_ID_5, state_1);
          
           // Write some debug info
           Serial.print("Incoming change for sensor: STATE 1");
           Serial.print(message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
         } 
      
      if (message.type == V_STATUS) {
         // Change relay state
          state_2 = message.getBool();  //This sets state for state_2 to the received value. 
          digitalWrite(LED_PIN_Channel_2, state_2?RELAY_2_ON:RELAY_2_OFF);
          // Store state in eeprom
          saveState(CHILD_ID_6, state_2);
          
           // Write some debug info
           Serial.print("Incoming change for sensor: STATE 2");
           Serial.print(message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
         } 
      
      

      It seems to me that state_1 = message.getBool(); AND state_2 = message.getBool(). So they both get set to the incoming value.

      I think you need to not set both to the same value and instead differentiate them to unique values. I could be wrong as I have not had chance to do anything related to mysensors for a long time now - I also don't use Domoticz.

      Have a look at the code on this page, it might help you a lot.... https://www.mysensors.org/build/relay

      1 Reply Last reply
      2
      • T Offline
        T Offline
        TheoL
        Contest Winner
        wrote on last edited by
        #3

        You don't check for which sensor the message is send - message.getSensor() - and in both cases you set the relays both on Like Skywatch said.

        So you could do something like:

        if ( message.type == V_STATUS ) { // check if GW send a status change
             if ( message.getSensor() == sensor 1 ) {
                  // handles sensor 1
             }
             else if ( message.getSensor() == sensor 2 ) {
                  // handles sensor 1
             }
             ...
            etc
        }
        
        1 Reply Last reply
        0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        19

        Online

        12.0k

        Users

        11.2k

        Topics

        113.4k

        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