[MySensors] Ruban led RGB (RVB)

Vous avez créé un script LUA dont vous êtes fier, un .sh génial, un programme Python hors du commun, un Tuto, c'est ici que vous pouvez les partager.
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: [tuto] Ruban led RGB (RVB)

Message par vil1driver »

oui
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
jule1986
Messages : 68
Inscription : 02 nov. 2015, 18:30
Localisation : Loiret (45)

Re: [tuto] Ruban led RGB (RVB)

Message par jule1986 »

Merci Vil1driver pour la correction.

J'ai enfin fait mon montage et tout fonctionne parfaitement pour un ruban RGBW.

J'éditerais ce poste pour mettre le sketch modifié.
jackslayter
Messages : 1040
Inscription : 30 sept. 2014, 14:40
Localisation : Isère (38)

Re: [tuto] Ruban led RGB (RVB)

Message par jackslayter »

hello, vous savez si c'est possible de mettre un ruban avec des led blanches sur un contrôleur RGB sans entrée White ?
parce que sur mon ruban RGB si je veux mettre du blanc , il faut que les 3 couleurs soient active et donc je suis à 35W pour du blanc contre 12W pour une seule couleur (R, G, ou B)

Merci
Raspberry Pi + RFLink + Domoticz Beta
Oregon - 3x Thgr122Nx / Chacon - micromodule 200w, télécommande 3b et 16b, 2x module 1000w, détecteur de fumée, contact de porte, 2x prise 3500w / OWL - CM180 / TRC02 v2 RGB 3M / Cheap - PIR, contact de porte
youn
Messages : 10
Inscription : 30 août 2017, 18:52

Re: [tuto] Ruban led RGB (RVB)

Message par youn »

bonjour j'ai modifier le code pour le faire marcher directement avec domoticz mais il est pas reconnu pourquoi ?

le code

Code : Tout sélectionner

// Enable gateway ethernet module type
#define MY_GATEWAY_W5100

// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
//#define MY_W5100_SPI_EN 4

// Enable Soft SPI for NRF radio (note different radio wiring is required)
// The W5100 ethernet module seems to have a hard time co-operate with
// radio on the same spi bus.
#if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
#define MY_SOFTSPI
#define MY_SOFT_SPI_SCK_PIN 14
#define MY_SOFT_SPI_MISO_PIN 16
#define MY_SOFT_SPI_MOSI_PIN 15
#endif
/*
// When W5100 is connected we have to move CE/CSN pins for NRF radio
#ifndef MY_RF24_CE_PIN
#define MY_RF24_CE_PIN 5
#endif
#ifndef MY_RF24_CS_PIN
#define MY_RF24_CS_PIN 6
#endif*/

// Enable UDP communication
//#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
#define MY_IP_ADDRESS 192,168,1,55

// If using static ip you can define Gateway and Subnet address as well
//#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
//#define MY_IP_SUBNET_ADDRESS 255,255,255,0

// Renewal period if using DHCP
//#define MY_IP_RENEWAL_INTERVAL 60000

// The port to keep open on node server mode / or port to contact in client mode
#define MY_PORT 5003

// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254

// The MAC address can be anything you want but should be unique on your network.
// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
// Note that most of the Ardunio examples use  "DEAD BEEF FEED" for the MAC address.
#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
//#define MY_INCLUSION_BUTTON_FEATURE
// 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

// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300

// Flash leds on rx/tx/err
// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 7  // Error led pin
//#define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN  9  // Transmit led pin

#if defined(MY_USE_UDP)
#include <EthernetUdp.h>
#endif
#include <Ethernet.h>

//#define MY_NODE_ID 5 // Sets a static id for a node

#include <SPI.h>
#include <MySensors.h>  

// Arduino pin attached to MOSFET Gate pin
#define RED_PIN 3   
#define GREEN_PIN 5
#define BLUE_PIN 6

// Choose your color for your blink : 3 RED, 5 GREEN, 6 BLUE
#define BLINK_PIN 6

// Define message name and type to send sensor info
MyMessage RedStatus(RED_PIN, V_DIMMER);   
MyMessage GreenStatus(GREEN_PIN, V_DIMMER);
MyMessage BlueStatus(BLUE_PIN, V_DIMMER);
MyMessage Status(1, V_DIMMER);
MyMessage rgbShowState(0, V_LIGHT);
MyMessage blinkState(2, V_LIGHT);
    
// Serial.print translate sensor id to sensor name
char color[][6] = {"","","","RED","","GREEN","BLUE"}; 
   
// Vars for rgbShow function
int redval = 0;
int greenval = 0;
int blueval = 0;
long time=0;
int isShow;

// Vars for blink function
unsigned long mtime=1000;  // memory time
unsigned int timeblinkOn=1000; // time blink On in ms
unsigned int timeblinkOff=1000; // time blink Off in ms
int blinkval=255; // value On led for blink
int isBlink=0; // for activate the blink, first is Off
   
void setup() 
{
  // Define pin mode (pin number, type)
  pinMode(RED_PIN, OUTPUT);   
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);

  // Correct saved RGB value for first start
  saveState(RED_PIN, constrain((int8_t)loadState(RED_PIN), 0, 100)); 
  saveState(GREEN_PIN, constrain((int8_t)loadState(GREEN_PIN), 0, 100)); 
  saveState(BLUE_PIN, constrain((int8_t)loadState(BLUE_PIN), 0, 100)); 
             
  // Get value from eeprom and write to output
  analogWrite(RED_PIN, 255 * loadState(RED_PIN) / 100);     
  analogWrite(GREEN_PIN, 255 * loadState(GREEN_PIN) / 100);
  analogWrite(BLUE_PIN, 255 * loadState(BLUE_PIN) / 100);
         
  // Write some debug info
  Serial.print("Load from eeprom RED: "); 
  Serial.print(loadState(RED_PIN)); 
  Serial.println("%"); 
  Serial.print("Load from eeprom GREEN: "); 
  Serial.print(loadState(GREEN_PIN)); 
  Serial.println("%"); 
  Serial.print("Load from eeprom BLUE: "); 
  Serial.print(loadState(BLUE_PIN)); 
  Serial.println("%");  
  
  // Send RGB value to controler (request ack back: true/false)
  Serial.println("Send eeprom value to controler"); 
  send( RedStatus.set(loadState(RED_PIN)), false );    
  send( GreenStatus.set(loadState(GREEN_PIN)), false );
  send( BlueStatus.set(loadState(BLUE_PIN)), false );
  
  // Correct RGB show state for first start and load it (set to 'On' at first start)
  saveState(0, constrain((int8_t)loadState(0), 0, 1));
  isShow=loadState(0);
       
  // Send RGB show state to controler (request ack back: true/false)
  //send( rgbShowState.set(isShow), false);
  send( rgbShowState.set(1), false);

  if (isShow==1){Serial.println("RGB show running..."); }
  Serial.println("Ready to receive messages...");  
}

void presentation()  {
  // Present sketch (name, version)
  sendSketchInfo(SN, SV);        
       
  // Register sensors (id, type, description, ack back)
  present(RED_PIN, S_DIMMER, "RED", false);
  present(GREEN_PIN, S_DIMMER, "GREEN", false);
  present(BLUE_PIN, S_DIMMER, "BLUE", false);
  present(0, S_LIGHT, "Show button", false);
  present(2, S_LIGHT, "Blink", false);
}

void loop()
{
  
   // Run RGB show if is set
  if (isShow==1)
  {
      rgbShow();
    analogWrite(RED_PIN, redval);
    analogWrite(GREEN_PIN, greenval);
    analogWrite(BLUE_PIN, blueval);
   } 

  // Run blink
  if (isBlink==1 && isShow==0)
  {  
    analogWrite(BLINK_PIN , blinkval); // LED blink Pin On/Off
      
      if ( millis() >= mtime )  // If time is up
     { 
              
       if (blinkval==255)
       { 
       Serial.println("Blink On");
       mtime = millis() + timeblinkOn ;
       blinkval=0;
       }
       else if (blinkval==0)
       {
       Serial.println("Blink Off"); 
       mtime = millis() + timeblinkOff ; 
       blinkval=255;
      
       }     
     } 
   } 
}


void receive(const MyMessage &message)
{

  BlinkShowOff();   // all commands we stop the blink.
  
  if (message.isAck())
  {
    Serial.println("Got ack from gateway");
  }
  if (message.type == V_LIGHT)
  {
           
    // Incoming on/off command sent from controller ("1" or "0")
    int lightState = message.getString()[0] == '1';
  
    // if receive RGB Show On commands, start the show
    if (message.sensor==0 && lightState==1)
    {
    rgbShowOn(); 
    }
        // if receive RGB Show Off commands, stop the show
    else if (message.sensor==0 && lightState==0)
    {
    rgbShowOff();
    }

    // if receive Blink Show On commands, start the Blink
    else if (message.sensor==2 && lightState==1)
    { 
    rgbShowOff();
    BlinkShowOn();
    }
    
    // if receive Blink Show Off commands, stop the show
    else if (message.sensor==2 && lightState==0)
    { 
    BlinkShowOff();
    rgbShowOn();
    send( rgbShowState.set(1), false);
    }
       
    // if receive RGB switch On command
    else if (lightState==1)
    {
      // Write some debug info
            Serial.print("Incoming change for ");
            Serial.print(color[message.sensor]);
            Serial.println(": On");
            Serial.print("Load from eeprom: ");
          
      if ( loadState(message.sensor) == 0)
      {
        // Pick up last saved dimmer level from the eeprom
                analogWrite(message.sensor, 255 * loadState(10*message.sensor) / 100);
                // Save loaded value to current
                saveState(message.sensor, loadState(10*message.sensor));
                Serial.print(loadState(10*message.sensor)); 
                Serial.println("%");
                // Send value to controler
                Serial.println("Send value to controler");
                send(Status.setSensor(message.sensor).set(loadState(10*message.sensor)),false);
            }
            else
            {
                // Pick up last saved dimmer level from the eeprom
                analogWrite(message.sensor, 255 * loadState(message.sensor) / 100);
                Serial.print(loadState(message.sensor));
                Serial.println("%"); 
                // Send value to controler
                Serial.println("Send value to controler");
                send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false);
            } 
            // Stop the show if it's running
            if (isShow==1){ rgbShowStop(message.sensor); }
        }
    // if recieve switch Off command
    else if (lightState==0)
    {
      // Write output to 0 (Off)
            analogWrite(message.sensor, 0);
            // Save old value to eeprom if it'was not zero
            if ( loadState(message.sensor) != 0 )
            {
                saveState(10*message.sensor, constrain((int8_t)loadState(message.sensor), 0, 100)); 
            }
            // Save new value to eeprom
            saveState(message.sensor, 0); 
            // Write some debug info
      Serial.print("Incoming change for ");
      Serial.print(color[message.sensor]);
      Serial.print(": ");
      Serial.println("Off");  
            Serial.print("Store old value: ");
            Serial.print(loadState(10*message.sensor));  
            Serial.println("%");
            // Send value to controler
            Serial.println("Send value to controler");
            send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false);
      // Stop the show if it's running
      if (isShow==1){ rgbShowStop(message.sensor); }
    }
  }
  else if (message.type == V_DIMMER)
  {    
      uint8_t incomingDimmerStatus = message.getByte();
      // limits range of sensor values to between 0 and 100 
      incomingDimmerStatus = constrain((int8_t)incomingDimmerStatus, 0, 100);
      // Change Dimmer level
      analogWrite(message.sensor, 255 * incomingDimmerStatus / 100);
      //Save value to eeprom
      saveState(message.sensor, incomingDimmerStatus); 
      // Write some debug info
      Serial.print("Incoming change for ");
      Serial.print(color[message.sensor]);
      Serial.print(": ");
      Serial.print(incomingDimmerStatus);
      Serial.println("%");
        // Send value to controler
        Serial.println("Send value to controler");
        send(Status.setSensor(message.sensor).set(loadState(message.sensor)),false);
      // Stop the show if it's running
      if (isShow==1){ rgbShowStop(message.sensor); }
    }
}
   
void rgbShow()
{
  time = millis();
  redval = 128+250*cos(2*PI/300000*time);
  greenval = 128+250*cos(2*PI/300000*time-222);
  blueval = 128+250*cos(2*PI/300000*time-111);
  // limits range of sensor values to between 0 and 255 
  redval = constrain(redval, 0, 255);
  greenval = constrain(greenval, 0, 255);
  blueval = constrain(blueval, 0, 255);
}

void rgbShowOn()
{
  // define show On
  isShow=1;
  // Save state
  saveState(0, 1); 
  // Write some debug info
  Serial.println("Show must go on");
}

 
void rgbShowOff()
{
  // define show Off
  isShow=0;
  // Save state
  saveState(0, 0);
  // Save RGB value to eeprom
  saveState(RED_PIN, 100 * redval / 255); 
  saveState(GREEN_PIN, 100 * greenval / 255);
  saveState(BLUE_PIN, 100 * blueval / 255);
  // Write some debug info
  Serial.println("Stop the show");
  // Send actual RGB value and state to controler and request ack back (true/false)
  Serial.println("Send eeprom value to controler"); 
  send( RedStatus.set(loadState(RED_PIN)), false );    
  send( GreenStatus.set(loadState(GREEN_PIN)), false );
  send( BlueStatus.set(loadState(BLUE_PIN)), false );
  send( rgbShowState.set(0), false);
}

void rgbShowStop(int sensor)
{
   // define show Off
   isShow=0;
   // Save state
   saveState(0, 0);
   // Write some debug info
   Serial.println("Stop the show");
   // Send actual RGB value and state to controler and request ack back (true/false)
   Serial.println("Send eeprom value to controler"); 
   if (sensor != RED_PIN)
   {
        saveState(RED_PIN, 100 * redval / 255); 
        send( RedStatus.set(loadState(RED_PIN)), false );  
    }
    if (sensor != GREEN_PIN)
    {
        saveState(GREEN_PIN, 100 * greenval / 255); 
        send( GreenStatus.set(loadState(GREEN_PIN)), false );
    }
    if (sensor != BLUE_PIN)
    {
        saveState(BLUE_PIN, 100 * blueval / 255);
        send( BlueStatus.set(loadState(BLUE_PIN)), false );
    }
    send( rgbShowState.set(0), false);
}

void BlinkShowOn()
{
    //  On the Blink led, Off other, before begin the blink show
   if (BLINK_PIN == RED_PIN)
   {
   analogWrite(RED_PIN, 255);
   analogWrite(GREEN_PIN, 0);
   analogWrite(BLUE_PIN, 0);
   }
   else if (BLINK_PIN == GREEN_PIN)
   {
   analogWrite(RED_PIN, 0);
   analogWrite(GREEN_PIN, 255);
   analogWrite(BLUE_PIN, 0);
   }
   else
   {
   analogWrite(RED_PIN, 0);
   analogWrite(GREEN_PIN, 0);
   analogWrite(BLUE_PIN, 255);
   }
  
  // define show On
  isBlink=1;
  // Save state
  saveState(2, 1); 
  // Write some debug info
  Serial.println("Blink show on");
  send( blinkState.set(1), false);
}

void BlinkShowOff()
{
  // define show On
  isBlink=0;
  // Save state
  saveState(2, 0); 
  // Write some debug info
  Serial.println("Blink show off");
  send( blinkState.set(0), false);
 }
jule1986
Messages : 68
Inscription : 02 nov. 2015, 18:30
Localisation : Loiret (45)

Re: [tuto] Ruban led RGB (RVB)

Message par jule1986 »

Bonjour a tous,

Exactement 1 ans après mon dernier post sur ce sujet..... :? Voici le code avec le blanc :

Pour info il faut utiliser le pin 9 pour le blanc et du coup le RF24_CE sur le pin 4

Code : Tout sélectionner

/**
 * 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 - Created by vil1driver
 * 
 * DESCRIPTION
 * RGB led strip controled with three dimmers + one On/Off for run/stop rgb color cycle :p
 * 
 */
 
#define SN   "RGBW Led strip 3D"
#define SV   "v1 RGBW"

// Load mysensors library	
#include <MySensor.h>	
// Load Serial Peripheral Interface library  
#include <SPI.h>

// Arduino pin attached to MOSFET Gate pin
#define RED_PIN 3 	
#define GREEN_PIN 5
#define BLUE_PIN 6
#define WHITE_PIN 9

// MySensor gw;	

// change the pins to free up the pwm pin for led control
#define RF24_CE_PIN   4 //<-- NOTE!!! changed, the default is 9
#define RF24_CS_PIN   10  // default is 10
#define RF24_PA_LEVEL RF24_PA_MAX

MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL);
MySensor gw(transport);  

// Define message name and type to send sensor info
MyMessage RedStatus(RED_PIN, V_DIMMER);		
MyMessage GreenStatus(GREEN_PIN, V_DIMMER);
MyMessage BlueStatus(BLUE_PIN, V_DIMMER);
MyMessage WhiteStatus(WHITE_PIN, V_DIMMER);
MyMessage Status(1, V_DIMMER);
MyMessage rgbShowState(0, V_LIGHT);
    
// Serial.print translate sensor id to sensor name
char color[][9] = {"","","","RED","","GREEN","BLUE","","","WHITE"};	
   
// Vars for rgbShow function
int redval = 0;
int greenval = 0;
int blueval = 0;
long time=0;
int isShow;
     
void setup() 
{
  // Initializes the sensor node (Callback function for incoming messages, node id, is repeater)
  gw.begin(incomingMessage, 31, true);		
       
  // Present sketch (name, version)
  gw.sendSketchInfo(SN, SV);				
       
  // Register sensors (id, type, description, ack back)
  gw.present(RED_PIN, S_DIMMER, "present RED light", false);
  gw.present(GREEN_PIN, S_DIMMER, "present GREEN light", false);
  gw.present(BLUE_PIN, S_DIMMER, "present BLUE light", false);
  gw.present(WHITE_PIN, S_DIMMER, "present WHITE light", false);
  gw.present(0, S_LIGHT, "present Show button", false);

  // Define pin mode (pin number, type)
  pinMode(RED_PIN, OUTPUT);	
  TCCR2A = 0b00100011;
  TCCR2B = 0b00000011;
  OCR2B = 128;	
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(WHITE_PIN, OUTPUT);

  // Correct saved RGB value for first start
  gw.saveState(RED_PIN, constrain((int8_t)gw.loadState(RED_PIN), 0, 100)); 
  gw.saveState(GREEN_PIN, constrain((int8_t)gw.loadState(GREEN_PIN), 0, 100)); 
  gw.saveState(BLUE_PIN, constrain((int8_t)gw.loadState(BLUE_PIN), 0, 100));
  gw.saveState(WHITE_PIN, constrain((int8_t)gw.loadState(WHITE_PIN), 0, 100)); 
             
  // Get value from eeprom and write to output
  analogWrite(RED_PIN, 255 * gw.loadState(RED_PIN) / 100); 		
  analogWrite(GREEN_PIN, 255 * gw.loadState(GREEN_PIN) / 100);
  analogWrite(BLUE_PIN, 255 * gw.loadState(BLUE_PIN) / 100);
  analogWrite(WHITE_PIN, 255 * gw.loadState(WHITE_PIN) / 100);
         
  // Write some debug info
  Serial.print("Load from eeprom RED: "); 
  Serial.print(gw.loadState(RED_PIN)); 
  Serial.println("%"); 
  Serial.print("Load from eeprom GREEN: "); 
  Serial.print(gw.loadState(GREEN_PIN)); 
  Serial.println("%"); 
  Serial.print("Load from eeprom BLUE: "); 
  Serial.print(gw.loadState(BLUE_PIN)); 
  Serial.println("%");  
  Serial.print("Load from eeprom WHITE: "); 
  Serial.print(gw.loadState(WHITE_PIN)); 
  Serial.println("%");  
  
  
  // Send RGB value to controler (request ack back: true/false)
  Serial.println("Send eeprom value to controler"); 
  gw.send( RedStatus.set(gw.loadState(RED_PIN)), false );    
  gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false );
  gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false );
  gw.send( WhiteStatus.set(gw.loadState(WHITE_PIN)), false );
    
  // Correct RGB show state for first start and load it (set to 'On' at first start)
  gw.saveState(0, constrain((int8_t)gw.loadState(0), 0, 1));
  isShow=gw.loadState(0);
       
  // Send RGB show state to controler (request ack back: true/false)
  gw.send( rgbShowState.set(isShow), false);
  
  if (isShow==1){Serial.println("RGB show running..."); }
  Serial.println("Ready to receive messages...");  
}

void loop()
{
  // Process incoming messages (like config and light state from controller)
  gw.process();		
      
  // Run RGB show if is set
  if (isShow==1)
  {
    	rgbShow();
  	analogWrite(RED_PIN, redval);
  	analogWrite(GREEN_PIN, greenval);
  	analogWrite(BLUE_PIN, blueval);
   } 
}


void incomingMessage(const MyMessage &message)
{
  if (message.isAck())
  {
   	Serial.println("Got ack from gateway");
  }
  if (message.type == V_LIGHT)
  {
  	// Incoming on/off command sent from controller ("1" or "0")
  	int lightState = message.getString()[0] == '1';
  
  	// if receive RGB Show On commands, start the show
  	if (message.sensor==0 && lightState==1){ rgbShowOn();	}
        // if receive RGB Show Off commands, stop the show
  	else if (message.sensor==0 && lightState==0){	rgbShowOff();	}
       
  	// if receive RGB switch On command
  	else if (lightState==1)
  	{
  		// Write some debug info
          	Serial.print("Incoming change for ");
          	Serial.print(color[message.sensor]);
          	Serial.println(": On");
          	Serial.print("Load from eeprom: ");
          
  		if ( gw.loadState(message.sensor) == 0)
  		{
  			// Pick up last saved dimmer level from the eeprom
            		analogWrite(message.sensor, 255 * gw.loadState(10*message.sensor) / 100);
            		// Save loaded value to current
            		gw.saveState(message.sensor, gw.loadState(10*message.sensor));
            		Serial.print(gw.loadState(10*message.sensor)); 
            		Serial.println("%");
            		// Send value to controler
            		Serial.println("Send value to controler");
            		gw.send(Status.setSensor(message.sensor).set(gw.loadState(10*message.sensor)),false);
          	}
          	else
          	{
            		// Pick up last saved dimmer level from the eeprom
            		analogWrite(message.sensor, 255 * gw.loadState(message.sensor) / 100);
            		Serial.print(gw.loadState(message.sensor));
            		Serial.println("%"); 
            		// Send value to controler
            		Serial.println("Send value to controler");
            		gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false);
          	} 
          	// Stop the show if it's running
          	if (isShow==1){ rgbShowStop(message.sensor); }
        }
  	// if recieve switch Off command
  	else if (lightState==0)
  	{
  		// Write output to 0 (Off)
          	analogWrite(message.sensor, 0);
          	// Save old value to eeprom if it'was not zero
          	if ( gw.loadState(message.sensor) != 0 )
          	{
            		gw.saveState(10*message.sensor, constrain((int8_t)gw.loadState(message.sensor), 0, 100)); 
          	}
          	// Save new value to eeprom
          	gw.saveState(message.sensor, 0); 
          	// Write some debug info
  		Serial.print("Incoming change for ");
  		Serial.print(color[message.sensor]);
  		Serial.print(": ");
  		Serial.println("Off");	
          	Serial.print("Store old value: ");
          	Serial.print(gw.loadState(10*message.sensor));  
          	Serial.println("%");
          	// Send value to controler
          	Serial.println("Send value to controler");
          	gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false);
  		// Stop the show if it's running
  		if (isShow==1){ rgbShowStop(message.sensor); }
  	}
  }
  else if (message.type == V_DIMMER)
  {    
    	uint8_t incomingDimmerStatus = message.getByte();
    	// limits range of sensor values to between 0 and 100 
    	incomingDimmerStatus = constrain((int8_t)incomingDimmerStatus, 0, 100);
    	// Change Dimmer level
    	analogWrite(message.sensor, 255 * incomingDimmerStatus / 100);
    	//Save value to eeprom
    	gw.saveState(message.sensor, incomingDimmerStatus); 
    	// Write some debug info
    	Serial.print("Incoming change for ");
    	Serial.print(color[message.sensor]);
    	Serial.print(": ");
    	Serial.print(incomingDimmerStatus);
    	Serial.println("%");
        // Send value to controler
        Serial.println("Send value to controler");
        gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false);
    	// Stop the show if it's running
    	if (isShow==1){ rgbShowStop(message.sensor); }
    }
}
   
void rgbShow()
{
  time = millis();
  redval = 128+250*cos(2*PI/300000*time);
  greenval = 128+250*cos(2*PI/300000*time-222);
  blueval = 128+250*cos(2*PI/300000*time-111);
  // limits range of sensor values to between 0 and 255 
  redval = constrain(redval, 0, 255);
  greenval = constrain(greenval, 0, 255);
  blueval = constrain(blueval, 0, 255);
}

void rgbShowOn()
{
  // define show On
  isShow=1;
  // Save state
  gw.saveState(0, 1); 
  // Write some debug info
  Serial.println("Show must go on");
}
   
void rgbShowOff()
{
  // define show Off
  isShow=0;
  // Save state
  gw.saveState(0, 0);
  // Save RGB value to eeprom
  gw.saveState(RED_PIN, 100 * redval / 255); 
  gw.saveState(GREEN_PIN, 100 * greenval / 255);
  gw.saveState(BLUE_PIN, 100 * blueval / 255);
  // Write some debug info
  Serial.println("Stop the show");
  // Send actual RGB value and state to controler and request ack back (true/false)
  Serial.println("Send eeprom value to controler"); 
  gw.send( RedStatus.set(gw.loadState(RED_PIN)), false );    
  gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false );
  gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false );
  gw.send( rgbShowState.set(0), false);
}

void rgbShowStop(int sensor)
{
   // define show Off
   isShow=0;
   // Save state
   gw.saveState(0, 0);
   // Write some debug info
   Serial.println("Stop the show");
   // Send actual RGB value and state to controler and request ack back (true/false)
   Serial.println("Send eeprom value to controler"); 
   if (sensor != RED_PIN)
   {
        gw.saveState(RED_PIN, 100 * redval / 255); 
        gw.send( RedStatus.set(gw.loadState(RED_PIN)), false );  
    }
    if (sensor != GREEN_PIN)
    {
        gw.saveState(GREEN_PIN, 100 * greenval / 255); 
        gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false );
    }
    if (sensor != BLUE_PIN)
    {
        gw.saveState(BLUE_PIN, 100 * blueval / 255);
        gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false );
    }
    gw.send( rgbShowState.set(0), false);
}
           
   
   
Répondre