Dimmer and switches



  • Hello, I've got Raspberry PI with Domoticz, and Arduino Mega 2560 as Gateway USB.
    I want to two dimmer controling on Domoticz, and two switches (on/off) controlling on Domoticz and manual switch. My sketch don't work coretly. Switches (unit 1, 2) controlling also dimmer (unit 0). Please help why this is happening?

    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level. 
    //#define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    #if F_CPU == 8000000L
    #define MY_BAUD_RATE 38400
    #endif
    
    // Flash leds on rx/tx/err
    // #define MY_LEDS_BLINKING_FEATURE
    // Set blinking period
    // #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Inverses the behavior of leds
    // #define MY_WITH_LEDS_BLINKING_INVERSE
    
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    #define MY_INCLUSION_BUTTON_FEATURE
    
    // Inverses behavior of inclusion button (if using external pullup)
    //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
    
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60 
    // Digital pin used for inclusion mode button
    #define MY_INCLUSION_MODE_BUTTON_PIN  3 
    
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <Bounce2.h>
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  5
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    #define BUTTON_PIN A1
    #define BUTTON2_PIN A2
    
    #define SN "DimmableLED"
    #define SV "1.1"
    
    #define LED_PIN 9      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    
    
    void before() { 
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    Bounce debouncer = Bounce();
    Bounce debouncer2 = Bounce();
    
    void setup() { 
      // Setup locally attached sensors
      delay(10000);
       // Setup the button.
      pinMode(BUTTON_PIN, INPUT_PULLUP);
      pinMode(BUTTON2_PIN, INPUT_PULLUP);
      // After setting up the button, setup debouncer.
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      debouncer2.attach(BUTTON2_PIN);
      debouncer2.interval(5);
    
       // Pull the gateway's current dim level - restore light level upon node power-up
        request( 0, V_DIMMER );
    }
    void presentation()  
    {   
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay", "1.0");
    
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_LIGHT);
      }
    
      // Register the LED Dimmable Light with the gateway
        present( 0, S_DIMMER );
    
        sendSketchInfo(SN, SV);
    }
    
    static int16_t currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(0, V_DIMMER);
    MyMessage lightMsg(0, V_LIGHT);
    MyMessage msg(1, V_LIGHT);
    MyMessage msg2(2, V_LIGHT);
    
    void loop() { 
      // Send locally attached sensor data here 
      if (debouncer.update()) {
        // Get the update value.
        int value = debouncer.read();
        // Send in the new value.
        if(value == LOW){
             saveState(1, !loadState(1));
             digitalWrite(RELAY_1, loadState(1)?RELAY_ON:RELAY_OFF);
             send(msg.set(loadState(1)));
             }
      }
      if (debouncer2.update()) {
          int value2 = debouncer2.read();
        if(value2 == LOW){
             saveState(2, !loadState(2));
             digitalWrite(RELAY_2, loadState(2)?RELAY_ON:RELAY_OFF);
             send(msg2.set(loadState(2)));
             }
      }
    }
    
    
    void receive(const MyMessage &message) {
    
    
    
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       
    
            //  Retrieve the power or dim level from the incoming request message
            int requestedLevel = atoi( message.data );
    
            // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
            requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    
            // Clip incoming level to valid range of 0 to 100
            requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
            requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
            Serial.print( "Changing level to " );
            Serial.print( requestedLevel );
            Serial.print( ", from " );
            Serial.println( currentLevel );
    
            fadeToLevel( requestedLevel );
    
            // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
            send(lightMsg.set(currentLevel > 0));
    
            // hek comment: Is this really nessesary?
            send( dimmerMsg.set(currentLevel) );
    
    
        
    
    }
    
    void fadeToLevel( int toLevel )
    {
    
        int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
        while ( currentLevel != toLevel ) {
            currentLevel += delta;
            analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
            //delay( FADE_DELAY );
        }
    }```

  • Mod

    Welcome to the forum @greg_2502 !

    The debug log will probablybe the easiest way to understand what is happening. Your sketch has several nice debug prints that show what the node receives.



  • Thank you for your help. It was enough to add the if condition depending on the message.sensor.


Log in to reply
 

Suggested Topics

  • 5
  • 5
  • 1
  • 4
  • 2
  • 8

63
Online

11.4k
Users

11.1k
Topics

112.7k
Posts