[SOLVED] Combine sketches for Relay + status (RGB)LED



  • Hello,

    First of all I would like to thank you for the good work you all are doing.
    Since I have been attended to this site, I am thinking of making a relay board with a status signal.
    What do I mean:
    I have a magnet which is working on 12V. I have this magnet installed to my door, so it is possible for me to close the door by magnet.
    From the site I have downloaded the sketch for operate a relay board, so it is possible for me to operate the magnet (with a relay) from Domoticz. It is already up and running.
    Unfortunately the magnet doesn’t have a status led, so it isn’t possible to see if this magnet is working or not.
    When I’m searching on the internet, I see several sketches to operate a led via some pins on the Arduino.
    My question is:
    Is it possible to integrate some code in the relay sketch to operate the LED.
    For your info, I have a RGB LED which only have to work with the colors green and red.

    I have done some fiddling with the code, but it doesn’t work I think:

    • modify it under the terms of the GNU General Public License
    • version 2 as published by the Free Software Foundation.

    • REVISION HISTORY
    • Version 1.0 - Henrik Ekblad
    • DESCRIPTION
    • Example sketch showing how to control physical relays.
    • This example will remember relay state after power failure.
    • http://www.mysensors.org/build/relay
      */

    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>

    #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // 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 redPin = 5
    #define greenPin = 6

    // NRFRF24L01 radio driver (set low transmit power by default)
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);

    void setup()
    {
    // Initialize library and add callback for incoming messages
    gw.begin(incomingMessage, AUTO, true);
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("RelayLED", "1.0");

    // Fetch relay status
    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)
    gw.present(sensor, S_LIGHT);
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);

    // Set relay to last known state (using eeprom storage) 
    digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
    

    }
    }

    void loop()
    {
    // Alway process incoming messages whenever possible
    gw.process();
    setColor(255, 0, 0); // red
    setColor(0, 255, 0); // green
    }

    void incomingMessage(const MyMessage &message) {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_LIGHT) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    gw.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());
    }

    void setColor(int red, int green, int blue){
    #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    #endif
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    }
    }

    I do know I have to combine the analogWrite command with the state of the relay, but I don know if this is possible….

    Can you help me please?

    Kind regards and thanks in advance….
    italicised textitalicised textitalicised text


  • Hero Member

    @Naitsirhc
    If you just want to turn the leds fully on to red or green try the code below. You may need to swap the colours around depending which one you want on when the relay is active.

    I have not tested the code but i think it will work

    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // 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 redPin  5
    #define greenPin  6
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
    
      // Fetch relay status
      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)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);  
    	pinMode(redPin, OUTPUT);
        pinMode(greenPin, OUTPUT);
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
    	 
    	 if (message.getBool()== true){
          digitalWrite(greenPin, LOW);
          digitalWrite(redPin, HIGH);
         }
         else{
          digitalWrite(redPin, LOW);
          digitalWrite(greenPin, HIGH);
         }
    	 
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    


  • You're brilliant.

    It works like you said.... Red if the door is closed and green when it is opened

    therefore I have adapted the code a littlebit:

    if (message.getBool()== true)
    is now:
    if (message.getBool()== false)

    Thank you very much


  • Hero Member

    Great! Glad it worked for you


 

302
Online

7.8k
Users

8.7k
Topics

92.9k
Posts