Q: neopixel color change based on relays (lights) in vera



  • Hi there, i created a very crude sensor that connected to a strip of neopixels. i have it simply fade to white when the light is pressed on and fade to black (off) when pressed off in vera.

    However, i've tried to now create some more "relays" as switches to allow me to trigger different light effects/modes, but i don't know how to create a switch, or case to check "when relay 1 on, color mode 1, relay2 on color 2, etc" It always reads a general 1 or 0, regardless of which light send the command from vera.

    Any ideas on how i can change the code in the receive function to listen to each switch(relay) and then trigger a different condition from there?

    below is my code

    /**
     * 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.
     *
     *******************************
     *
     * 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
     */ 
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 12
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Adafruit_NeoPixel.h>
    
    #define LED_PIN 5
    #define RELAY_1  1  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // 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 NUM_LEDS 60 //60 for full cabinet set
    #define ledFadeTime 5
    #define BRIGHTNESS 100
    
    Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
    
    int gamma[] = {
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
        2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
        5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
       10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
       17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
       25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
       37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
       51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
       69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
       90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
      115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
      144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
      177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
      215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
    
    
    void before() { 
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    void setup() {
      strip.setBrightness(BRIGHTNESS);
      strip.begin();
      strip.show(); // Initialize all pixels to 'off'
    }
    
    void presentation()  
    {   
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Kitchen Island Lights", "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);
      }
    }
    
    
    void loop() 
    {
      
    }
    
    void receive(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
         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());
         int showState = message.getBool();
         Serial.print("Newstate variable data:");
         Serial.println(showState);
         if (showState==1) {
            //rgbFadeIn(255, 255, 255, ledFadeTime); // White
            rgbFadeIn(255, 255, 255, ledFadeTime); // orange 
               
          //colorWipe(strip.Color(255, 255, 255), 50);    // White
         }else{
          rgbFadeOut(255,255,255,ledFadeTime);
          //colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
         }
         
       } 
    }
    
    
    // Fill the dots one after the other with a color
    void colorWipe(uint32_t c, uint8_t wait) {
      for(uint16_t i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
        strip.show();
        delay(wait);
      }
    }
    
    void rgbFadeIn(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
      for(uint8_t b = 0; b <255; b++) {
         for(uint8_t i=0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
         }
         strip.show();
         //delay(wait);
      }
    }
    void rgbFadeOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
      for(uint8_t b=255; b > 0; b--) {
         for(uint8_t i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
         }
         strip.show();
         //delay(wait);
      }
    }
    
    

  • Hero Member

    I have a RGBW node that also have mode selection and I send the mode from Vera to the node using V_VAR1.

    This is maybe not what you are looking for but maybe it can give you some idea or be helpful someway.

    /**
     * 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * 
     * Version 1.5 - Modes added
     * 
     * Example sketch showing how to control RGBW LED Strip.
    
     * IMPORTANTE NOTE!!! one of the "radio" pins has been moved from pin 9 to pin 4 because the White is connected to pin 9 because only 3,5,6 and 9 are PWM outputs on Arduino Pro Mini!!!!
    
     * This code should generate 4 Dimmer devices in Vera so you can control the RED, GREEN, BLUE and WHITE individualy
    **/
    
     /*
     arduino Pro mini 5V 16MHz
    */
    
    #define MY_DEBUG    // Enables debug messages in the serial log
    #define MY_RADIO_NRF24
    
    #define MY_NODE_ID 4
    #define SN "RGBW"
    #define SV "2.0"
    
    #define MY_RF24_CE_PIN 4    //<-- NOTE!!! changed, the default is 9
    #define MY_RF24_CS_PIN 10    // default is 10
    
    
    #include <MyConfig.h>
    #include <MySensors.h>
    #include <SPI.h>
    
    #include <LEDFader.h> //library for fading effect https://github.com/jgillick/arduino-LEDFader
    
    
    
    #define REDPIN       3 // pin for red LED
    #define GREENPIN     5 // pin for green LED
    #define BLUEPIN      6 // pin for blue LED
    #define WHITEPIN     9 // pin for white LED
    
    
    //#define RF24_PA_LEVEL RF24_PA_MAX
    
    #define LED_NUM 3 //number of LEDs to fade
    #define DIR_UP 1  //
    #define DIR_DOWN -1
    #define FADE_TIME 2000
    
    int mode;
    int direction = DIR_UP;   //for fading effect
    unsigned long previousMillis = 0;
    unsigned long currentMillis;
    const long interval = 1000;
    int REDLedState = 0;
    int BLUELedState = 0;
    
    // 4 LED Faders
    LEDFader leds[LED_NUM] =
    {
      LEDFader(REDPIN),
      LEDFader(GREENPIN),
      LEDFader(BLUEPIN)
    };
    
    LEDFader REDled;
    
    MyMessage msgVAR1(REDPIN, V_VAR1);
    
    void setup()
    {
      
      pinMode(REDPIN, OUTPUT);
      pinMode(GREENPIN, OUTPUT);
      pinMode(BLUEPIN, OUTPUT);
      pinMode(WHITEPIN, OUTPUT);
    
      mode = 0;
      
      REDled = LEDFader(REDPIN);
      
      Serial.print(" Mode is: ");
      Serial.print(mode);
    
      //Get Dim values from RAM and write to outputs
      analogWrite(REDPIN, 255 * loadState(REDPIN) / 100);
      analogWrite(GREENPIN, 255 * loadState(GREENPIN) / 100);
      analogWrite(BLUEPIN, 255 * loadState(BLUEPIN) / 100);
      analogWrite(WHITEPIN, 255 * loadState(WHITEPIN) / 100);
    
      // Request/wait for dimmer status
      /*
       gw.request(RED, V_DIMMER);
      gw.process();
       gw.request(GREEN, V_DIMMER);
      gw.process();
       gw.request(BLUE, V_DIMMER);
      gw.process();
       gw.request(WHITE, V_DIMMER);
      gw.process();
      */
    
    }
    
    void presentation()  
    { 
      // Send the Sketch Version Information to the Gateway
      sendSketchInfo(SN, SV);
      // Register all sensors to gw (they will be created as child devices)
      present(REDPIN, S_DIMMER);
      present(GREENPIN, S_DIMMER);
      present(BLUEPIN, S_DIMMER);
      present(WHITEPIN, S_DIMMER);
    }
    
    void loop()
    {
      currentMillis = millis();
    
    
      if (mode == 1)
      {
        modeone();
    
      }
    
      if (mode == 2)
      {
        modetwo();
    
      }
    
      if (mode == 3)
      {
        modethree();
    
      }
    }
    
    void receive(const MyMessage &message)
    {
      Serial.print(message.type);
    
      if (message.type == V_DIMMER)
      {
        uint8_t incomingDimmerStatus = message.getByte();
        // Change Dimmer level
    
        analogWrite(message.sensor, 255 * incomingDimmerStatus / 100);
        saveState(message.sensor, message.getByte()); //Save Dim value to RAM
        // Write some debug info
        Serial.print("Incoming change for dimmer on pin: ");
        Serial.println(message.sensor);
        Serial.print(" New status: ");
        Serial.println(incomingDimmerStatus);
      }
    
      if (message.type == V_VAR1)
      {
        uint8_t IncomeingModeStatus = message.getByte();
        mode = IncomeingModeStatus;
        Serial.print(" New Mode recieved: ");
        Serial.println(mode);
    
        if (mode == 0)
        {
          //Get saved dim values from RAM and write to output
          analogWrite(REDPIN, 255 * loadState(REDPIN) / 100); 
          analogWrite(GREENPIN, 255 * loadState(GREENPIN) / 100);
          analogWrite(BLUEPIN, 255 * loadState(BLUEPIN) / 100);
          analogWrite(WHITEPIN, 255 * loadState(WHITEPIN) / 100);
        }
    
      }
    
    }
    
    
    void modeone()
    {
      //Alarm mode RED BLUE blinking
      Serial.println("Runing Mode 1 sequence");
    
    
    
      if (currentMillis - previousMillis >= interval)
      {
        // save the last time you changed to LEDstrip state
        previousMillis = currentMillis;
    
        analogWrite(GREENPIN, 0);
        analogWrite(WHITEPIN, 0);
    
        // if the LEDstrip is RED turn it BLUE and vice-versa:
        if (REDLedState == 0)
        {
          REDLedState = 255;
          BLUELedState = 0;
        }
        else
        {
          REDLedState = 0;
          BLUELedState = 255;
        }
    
        // set the LEDstrip with the ledState of the variable:
        analogWrite(REDPIN, REDLedState);
        analogWrite(BLUEPIN, BLUELedState);
      }
    
    }
    
    void modetwo()
    {
      //Random fading of all except white
      Serial.println("Runing Mode 2 sequence");
      
    
      // Update all LEDs and start new fades if any are done
      for (byte i = 0; i < LED_NUM; i++)
      {
        LEDFader *led = &leds[i];
        led->update();
        analogWrite(WHITEPIN, 0);
        
        // This LED is not fading, start a new fade
        if (led->is_fading() == false)
        {
          int duration = random(1000, 3000); // between 1 - 3 seconds
    
          // Fade Up
          if (led->get_value() == 0)
          {
            byte intensity = random(50, 255);
            led->fade(intensity, duration);
          }
          // Fade Down
          else
          {
            led->fade(0, duration);
          }
        }
      }
    
    }
    
    void modethree()
    
    {
      //Fade up fade down of RED
      Serial.println("Runing Mode 3 sequence");
      REDled.update();
    
      
      
      // LED no longer fading, switch direction
      if (!REDled.is_fading())
      {
        //turn off all other LEDs
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        analogWrite(WHITEPIN, 0);
        
        // Fade down
        if (direction == DIR_UP)
        {
          REDled.fade(0, FADE_TIME);
          direction = DIR_DOWN;
        }
        // Fade up
        else
        {
          REDled.fade(255, FADE_TIME);
          direction = DIR_UP;
        }
      }
    }
    


  • @korttoma

    thsnk you for the input. how does the interface look in vera? I'm not too clear on using the V_var options. I do like doing this through a single sensor vs trying to use different.

    I think my biggest problem right now is understanding how to write the code to understand the differences of the messages coming in from vera/the myS gateway


  • Hero Member

    I actually do not have anything in the interface for setting the mode. I just use a simple lua command:

    luup.call_action("urn:upnp-arduino-cc:serviceId:arduino1", "SendCommand", {radioId="11;3", variableId="VAR_1", value=1}, 24)
    

Log in to reply
 

517
Online

6.7k
Users

7.6k
Topics

80.5k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.