@mfalkvidd many thanks for coming back. I think I'll keep more complex things seperate, however will explore multiple binary sensors and relays on the same node.
Thanks
@mfalkvidd many thanks for coming back. I think I'll keep more complex things seperate, however will explore multiple binary sensors and relays on the same node.
Thanks
No way - found it. Large comment at the top of sketch (not copied to this forum) had a line of *********s across the top.
Removed and all ok. Thanks for your help anyway @mfalkvidd
@BearWithBeard said in Relay Actuator with momentary (pulse) action:
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON)
Many thanks for coming back @BearWithBeard, its a huge help.
So my logic for correcting the State problem at the bottom would be to do the same calculation to get them in to 0 and 1.
saveState(message.getSensor()-7, message.getBool());
Compiled correctly and then uploaded and it works! Fantastic. The following sketch has 3 dimmers, 4 buttons and 2 relay outputs.
Thank you very much - I'm learning
/**
* 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 - February 15, 2014 - Bruce Lacey
* Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
*
* DESCRIPTION
* This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
* <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
* Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
*
* The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
* The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
* to the LED negative terminal and the MOSFET Source pin is connected to ground.
*
*/
// Button
// The below includes 4 buttons using code examples from here: https://forum.mysensors.org/topic/9538/multiple-switch-inputs/8
// Button
// Relay
// The below includes 2 relays from https://www.mysensors.org/build/relay
// https://forum.mysensors.org/topic/5906/relay-actuator-with-momentary-pulse-action/2
// Relay
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
// Enable RS485 transport layer
#define MY_RS485
// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
#define MY_NODE_ID 4
#include <SPI.h>
#include <MySensors.h>
// Button
#include <Bounce2.h>
//
#define SN "N004"
#define SV "1.0"
#define noLEDs 2
const int LED_Pin[] = {5, 6};
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
static int currentLevel1 = 0; // Current dim level...
static int currentLevel2 = 0; // Current dim level...
MyMessage dimmer1Msg(1, V_DIMMER);
MyMessage light1Msg(1, V_LIGHT);
MyMessage dimmer2Msg(2, V_DIMMER);
MyMessage light2Msg(2, V_LIGHT);
// Button
#define FIRST_BUTTON_ID 3
#define MAX_BUTTON 4
const uint8_t buttonPin[] = {4, 10, 11, 12}; // switch around pins to your desire
Bounce debouncer[MAX_BUTTON];
MyMessage buttonMsg(0, V_TRIPPED);
bool oldButton[MAX_BUTTON] = {false};
// Button
// Relay
#define FIRST_RELAY_ID 7
#define MAX_RELAY 2
const uint8_t relayPin[] = {3, 7}; // Pins of relays
MyMessage relayMsg(0, V_STATUS);
#define RELAY_ON 1
#define RELAY_OFF 0
// Relay
void before()
{
}
/***
* Dimmable LED initialization method
*/
void setup()
{
// LEDS
// Pull the gateway's current dim level - restore light level upon sendor node power-up
for (int sensor=1; sensor<=noLEDs; sensor++){
request( sensor, V_DIMMER );
}
// Button
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i] = Bounce(); // initialize debouncer
debouncer[i].attach(buttonPin[i], INPUT_PULLUP);
debouncer[i].interval(5);
oldButton[i] = debouncer[i].read();
}
// Button
// Relay
for (uint8_t i = 0; i < MAX_RELAY; i++) {
pinMode(relayPin[i], OUTPUT);
//digitalWrite(relayPin, loadState(i)?RELAY_ON:RELAY_OFF);
if (loadState(i) == RELAY_ON) {
digitalWrite(relayPin, RELAY_ON);
wait(500);
digitalWrite(relayPin, RELAY_OFF);
}
}
// Relay
}
void presentation() {
// Register the LED Dimmable Light with the gateway
for (int sensor=1; sensor<=noLEDs; sensor++){
present(sensor, S_DIMMER);
wait(2);
}
// Button
for (int i = 0; i < MAX_BUTTON; i++) { //i < numSensors &&
present(FIRST_BUTTON_ID + i, S_DOOR);
}
// Button
// Relay
for (int i = 0; i < MAX_RELAY; i++) {
present(FIRST_RELAY_ID + i, S_BINARY);
}
// Relay
sendSketchInfo(SN, SV);
}
/***
* Dimmable LED main processing loop
*/
void loop()
{
// Button
bool button[MAX_BUTTON];
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i].update();
button[i] = debouncer[i].read();
if (button[i] != oldButton[i]) {
send(buttonMsg.setSensor(FIRST_BUTTON_ID + i).set( button[i])); // Send tripped value to gw
oldButton[i] = button[i];
}
}
// Button
}
void receive(const MyMessage &message) {
if (message.type == V_LIGHT || message.type == V_DIMMER) {
if (message.sensor == 1) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel1 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel1 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel1 = requestedLevel1 > 100 ? 100 : requestedLevel1;
requestedLevel1 = requestedLevel1 < 0 ? 0 : requestedLevel1;
fadeToLevel1( requestedLevel1, message.sensor );
send(light1Msg.set(currentLevel1 > 0 ? 1 : 0));
send(dimmer1Msg.set(currentLevel1) );}
if (message.sensor == 2) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel2 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel2 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel2 = requestedLevel2 > 100 ? 100 : requestedLevel2;
requestedLevel2 = requestedLevel2 < 0 ? 0 : requestedLevel2;
fadeToLevel2( requestedLevel2, message.sensor );
send(light2Msg.set(currentLevel2 > 0 ? 1 : 0));
send(dimmer2Msg.set(currentLevel2) );}
}
// Relay
if (message.type == V_STATUS) {
if (message.sensor == 7) {
if (message.getBool() == RELAY_ON) {
//digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON);
wait(500);
//digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
digitalWrite(relayPin[message.getSensor()-7], RELAY_OFF);
}
saveState(message.getSensor()-7, message.getBool());
}
if (message.sensor == 8) {
if (message.getBool() == RELAY_ON) {
//digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON);
wait(500);
//digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
digitalWrite(relayPin[message.getSensor()-7], RELAY_OFF);
}
saveState(message.getSensor()-7, message.getBool());
}
}
// Relay
}
/***
* This method provides a graceful fade up/down effect
*/
void fadeToLevel1( int toLevel1, int ledid1 ) {
int delta1 = ( toLevel1 - currentLevel1 ) < 0 ? -1 : 1;
while ( currentLevel1 != toLevel1 ) {
currentLevel1 += delta1;
analogWrite(LED_Pin[ledid1-1], (int)(currentLevel1 / 100. * 255) );
wait( FADE_DELAY );
}
}
void fadeToLevel2( int toLevel2, int ledid2 ) {
int delta2 = ( toLevel2 - currentLevel2 ) < 0 ? -1 : 1;
while ( currentLevel2 != toLevel2 ) {
currentLevel2 += delta2;
analogWrite(LED_Pin[ledid2-1], (int)(currentLevel2 / 100. * 255) );
wait( FADE_DELAY );
}
}
Hi @nagelc , thanks for coming back to me - Apologies I didn't realise you'd replied nearer the time.
Am I right in thinking, so long as I define the new patterns function later on in the sketch - I can include it in the switch of function updateLightPattern, and it will be added to the mapping?
How do we know what definitive figures the 0 to 100 percentage map to in 0 to 15? Or is that basic arithmetic and each switch state will be called in each 'chunk'. I also only count 10 items in the switch state. Does that mean we can add 5 more? and if wanting more than 15 simply change our mapping size to have more?
Appreciate the help.
void updateLightPattern(){
// global: curPattern, updatePatternDelay, lastPatternUpdate
unsigned long now = millis() ;
if (now > lastPatternUpdate + updatePatternDelay){ // check if time for update
switch (curPattern) {
case pAlarm: // flash light
patternAlarm();
break ;
case pFire: // wild fire
patternFire();
break ;
case pFire2: // cosy fire
patternFire2();
break ;
case pCandle: // flame
patternCandle();
break ;
case pCircle: // flame
patternCircle();
break ;
case pRainbow: // rotating rainbow
patternRainbow();
break ;
case pSinelon: // rotating rainbow
patternSinelon();
break ;
case pSolid: // do nothing fall through
case pOff:
case pOn:
default : // def
break ;
}
lastPatternUpdate = now ;
}
}
Morning all, apologies for waking up an old thread, but it's pertinent to this chain of thought:
I'd like to build on this code and introduce a few more LED settings according to the percentage figure sent in the RGB dimmer.
Can anyone explain which part looks at the percentage and translates it out to the pattern?
How would I go about adding a new pattern to a chosen percentage value?
Many thanks
Adam
/*
PROJECT: MySensors / RGB light NEOPIXEL
PROGRAMMER: AWI
DATE: october 10, 2015/ last update: september 20, 2016
FILE: AWI_Wall_LIght_x.ino
LICENSE: Public domain
https://forum.mysensors.org/topic/4934/wall-mounted-mood-light-v2
Hardware: Nano and MySensors 2.0, Wall light 16 WS2812B leds (neopixel)
Special:
uses Fastled library with NeoPixel (great & fast RBG/HSV universal library) https://github.com/FastLED/FastLED
SUMMARY:
Different patterns and brightness settings
Button switches on/off and cycles through all Color patterns on long press
Remarks:
Fixed node-id
Change log:
20160915 - Updated to MySensors 2.0
20160920 - Changed state change to dimmer i.s.o. switch()
*/
/*
RGB Dimmer percentages = pattern
0 - 6 = Solid Colour
7 - 19 = Dim (think supposed to be off)
20 - 26 - Alarm (white and red light)
27 - 34 = Fire
35 - 46 = Fire2
47 - = one way Runner (set the colour first)
= Two way Runner (White)
You will be limited by RAM as to how many LEDs you can run
Using a Mega you can run 2000 LEDs
Arduino Nano = 2kb ram = 600 pixels
Arduino Uno = 2kb ram = 600 pixels
Arduino Mega = 8kb ram = 2400 pixels
*/
//**** MySensors *****
// Enable debug prints to serial monitor
#define MY_DEBUG
//#define MY_RADIO_NRF24 // Enable and select radio type attached
//#define MY_RF24_CHANNEL 80 // radio channel, default = 76
#define MY_RS485
#define MY_RS485_DE_PIN 2
#define MY_RS485_BAUD_RATE 9600
#define MY_NODE_ID 2
#define NODE_TXT "WS2812 N2" // Text to add to sensor name
// #define MY_RF24_CE_PIN 7 // Ceech board, 3.3v (7,8) (pin default 9,10)
// #define MY_RF24_CS_PIN 8
// helpers
#define LOCAL_DEBUG // enable if print wanted
#ifdef LOCAL_DEBUG
#define Sprint(a) (Serial.print(a)) // macro as substitute for print, enable if no print wanted
#define Sprintln(a) (Serial.println(a)) // macro as substitute for println
#else
#define Sprint(a)
#define Sprintln(a)
#endif
#include <SPI.h> // My Sensors
#include <MySensors.h>
#include <FastLED.h> // https://github.com/FastLED/FastLED
#include "JC_Button.h" // https://github.com/JChristensen/Button
const int stripPin = 4 ; // pin where 2812 LED strip is connected
const int buttonPin = 5 ; // push button
const int numPixel = 156 ; // set to number of pixels (x top / y bottom)
const int RGB_LightChild = 0 ; // Child Id's, standard light child on/off/ dim
const int RGB_RGBChild = 1 ; // RGB light child (on/off/dim/color, if controller supports V_RBG))
const int RGB_SolidColorChild = 2 ; // when set, node reads Color text from ColorTextChild
const int RGB_TextColorChild = 3 ; // Holds Text value for color (custom colors from controller)
const int RGB_AlarmPatternChild = 4 ; // Switches to alarm status
const int RGB_NextPatternChild = 5 ; // Move to next pattern when set
CRGB leds[numPixel];
// Kelving colors: Light & daylight (in Fastled reference only)
/// 1900 Kelvin Candle=0xFF9329 /* 1900 K, 255, 147, 41 */,
/// 2600 Kelvin Tungsten40W=0xFFC58F /* 2600 K, 255, 197, 143 */,
/// 2850 Kelvin Tungsten100W=0xFFD6AA /* 2850 K, 255, 214, 170 */,
/// 3200 Kelvin Halogen=0xFFF1E0 /* 3200 K, 255, 241, 224 */,
/// 5200 Kelvin CarbonArc=0xFFFAF4 /* 5200 K, 255, 250, 244 */,
/// 5400 Kelvin HighNoonSun=0xFFFFFB /* 5400 K, 255, 255, 251 */,
/// 6000 Kelvin DirectSunlight=0xFFFFFF /* 6000 K, 255, 255, 255 */,
/// 7000 Kelvin OvercastSky=0xC9E2FF /* 7000 K, 201, 226, 255 */,
/// 20000 Kelvin ClearBlueSky=0x409CFF /* 20000 K, 64, 156, 255 */
char setRGBvalue[] = "FFC58F"; // Controller sent RGB value, default tungsten40W
uint16_t curBrightness = 0x7F, setBrightness = 0x7F ; // Brightness globals (actualBrightness)
unsigned long updateBrightnessDelay, lastBrightnessUpdate ; // Brightness timers
int RGBonoff ; // OnOff flag
enum { pSolid, pOff, pOn, pAlarm, pFire, pFire2, pCandle, pCircle, pSinelon, pRainbow} ; // Pattern states (stored in int for convenience)
const int lastPatternIdx = pRainbow + 1 ; // use last pattern for patterncount
int curPattern = pSolid ; // current pattern
int setPattern = pSolid ; // set pattern (controller)
unsigned long updatePatternDelay, lastPatternUpdate ; // Pattern timers
unsigned long idleTimer = millis() ; // return to idle timer
const unsigned long idleTime = 10000UL; // return to idle after 10 secs
const unsigned long dimTime = 1000UL; // dim period
const unsigned long heartbeatInterval = 1 * 60UL * 1000UL ; // heartbeatinterval, just to let the controller know I am alive
unsigned long heartbeatCounter = 0 ;
MyMessage lightRGBMsg(RGB_LightChild, V_RGB); // standard messages, light
MyMessage lightdimmerMsG(RGB_LightChild ,V_DIMMER);
MyMessage lightOnOffMessage(RGB_LightChild, V_STATUS);
Button myBtn(buttonPin, true, true, 20); //Declare the button (pin, pull_up, invert, debounce_ms)
// Simple state machine for button state
enum {sIdle, sBrightness, sPattern} ; // simple state machine for button press
int State ;
void setup() {
FastLED.addLeds<WS2812B, stripPin, RGB >(leds, numPixel); // initialize led strip (NEOPIXEL =WS...)
for(int i = 0 ; i < 6 ; i++) { // get color value from EEPROM (6 char)
setRGBvalue[i] = loadState(i) ;
}
setLightPattern(pSolid, 0) ; // default controller Solid
FastLED.show();
State = sIdle ; // Initial state
//randomSeed(analogRead(0));
}
void presentation(){
// MySensors
sendSketchInfo("AWI RGB Wall " NODE_TXT, "2.0");
present(RGB_RGBChild, S_RGB_LIGHT, "RGB Wall RGB " NODE_TXT);// present to controller
present(RGB_LightChild, S_LIGHT, "RGB Wall Light " NODE_TXT);
present(RGB_SolidColorChild, S_LIGHT, "RGB Set Solid color (text) " NODE_TXT);
present(RGB_TextColorChild, S_INFO, "RGB Wall textcolor " NODE_TXT);
present(RGB_AlarmPatternChild, S_BINARY, "RGB Wall Alarm " NODE_TXT);
present(RGB_NextPatternChild, S_DIMMER, "RGB Wall Pattern " NODE_TXT);
}
// read button and act accordingly
// short press: on/off
// longer press: set patterns with following short press
// long press: set brightness increase
void loop() {
myBtn.read(); //Read the button (only read)
unsigned long now = millis(); // loop timer reference
switch (State) {
case sIdle: // default state, browse through patterns
if (myBtn.wasReleased()){ // light on/ off in idle
RGBonoff = !RGBonoff ; // invert light state
setLightBrightness((RGBonoff == 1)?setBrightness:0, dimTime);
send(lightOnOffMessage.set(RGBonoff)); // and update controller
} else if (myBtn.pressedFor(800)){ // move to Pattern update state with long press
idleTimer = now ; // return to idle after ...
State = sPattern ;
}
break ;
case sPattern: // entered after long press
if (myBtn.pressedFor(4000)){ // when press even longer move to Brightness update
State = sBrightness ;
} else if (myBtn.wasPressed()){
setPattern = (setPattern + 1) % lastPatternIdx ; // increase pattern and wrap
setLightPattern((setPattern), 500 );
idleTimer = now ;
} else if ( now > idleTime + idleTimer ){ // return to idle after ...
State = sIdle ;
}
break ;
case sBrightness: // entered after looong press
if (myBtn.wasPressed()){ // if pressed again increase brightness
setLightBrightness((curBrightness+0x1F) % 0xFF, 0) ; // increase brightness and wrap (0..0xFF)
idleTimer = now ;
} else if ( now > idleTime + idleTimer ){ // return to idle after ...
State = sIdle ;
}
break ;
default :
State = sIdle ;
break ;
}
updateLightBrightness(); // update Brightness if time
updateLightPattern(); // update Pattern if time
if ( now > heartbeatCounter + heartbeatInterval){ // heartbeat every hour
sendHeartbeat();
heartbeatCounter = now ;
}
}
// Sets the light brightness, takes value and time (ms) as input
void setLightBrightness(int newBrightness, unsigned long updateTime){
// global: curBrightness, actualBrightness, updateBrightnessDelay
updateBrightnessDelay = updateTime / 0xFF ; // delay = time / max steps
curBrightness = newBrightness ; // set curBrightness to new value, rest is done in update
}
// Update the light brightness if time
void updateLightBrightness(){
// global: curBrightness, actualBrightness, updateBrightnessDelay, lastBrightnessUpdate ;
static byte actualBrightness ; // store real brightness state for slow dim
unsigned long now = millis() ;
if (now > lastBrightnessUpdate + updateBrightnessDelay){// check if time for update
if ( actualBrightness > curBrightness) {
FastLED.setBrightness( --actualBrightness );
FastLED.show();
} else if ( actualBrightness < curBrightness){
FastLED.setBrightness( ++actualBrightness );
FastLED.show();
}
lastBrightnessUpdate = now ;
}
}
// **** Pattern routines *****
// Sets and initializes the light pattern if nescessary
void setLightPattern( int newPattern, unsigned long updateDelay){
// global: curPattern, updatePatternDelay
static int lastPattern = pSolid ; // last pattern for pOn / pOff virtual patterns
if (newPattern == pOff) lastPattern = curPattern ; // remember last pattern
if (newPattern == pOn) curPattern = lastPattern ; // only for pOn switch to last pattern
else curPattern = newPattern ;
updatePatternDelay = updateDelay ; // delay for next pattern update, can be changed in pattern
switch(curPattern){
case pSolid: // solid is set value in all pixels (and on)
for(int i = 0 ; i < numPixel ; i++) leds[i] = strtol( setRGBvalue, NULL, 16);
setLightBrightness(setBrightness, dimTime) ; // slow dim to on
FastLED.show();
break ;
case pOn: // On is set Brightness in all pixels
setLightBrightness(setBrightness, dimTime) ; // slow dim to on
FastLED.show();
break ;
case pOff: // off state all pixels off (add dim and pOn)
setLightBrightness(0, dimTime) ; // slow dim to off
FastLED.show();
break ;
case pCircle: // all pixels off
for(int i = 0 ; i < numPixel ; i++) leds[i] = 0 ;
for(int i = 0 ; i < 2 ; i++){
leds[i] = strtol( setRGBvalue, NULL, 16) ; // 1 pixel on
}
FastLED.show();
break ;
default :
setLightBrightness(setBrightness, dimTime) ; // slow dim to on
FastLED.show();
break ;
}
}
// Update the light pattern when time for it
void updateLightPattern(){
// global: curPattern, updatePatternDelay, lastPatternUpdate
unsigned long now = millis() ;
if (now > lastPatternUpdate + updatePatternDelay){ // check if time for update
switch (curPattern) {
case pAlarm: // flash light
patternAlarm();
break ;
case pFire: // wild fire
patternFire();
break ;
case pFire2: // cosy fire
patternFire2();
break ;
case pCandle: // flame
patternCandle();
break ;
case pCircle: // flame
patternCircle();
break ;
case pRainbow: // rotating rainbow
patternRainbow();
break ;
case pSinelon: // rotating rainbow
patternSinelon();
break ;
case pSolid: // do nothing fall through
case pOff:
case pOn:
default : // def
break ;
}
lastPatternUpdate = now ;
}
}
// Define the different patterns
// Alarm - intermittent white and red color, full intensity, intermittent top & bottom half
void patternAlarm() {
static boolean topBot ; // indicates direction for next entry
const CRGB colorTop = CRGB(0xFF, 0, 0 ); // red color
const CRGB colorBottom = CRGB(0xFF, 0xFF, 0xFF ); // white color
FastLED.setBrightness(0xFF); // set the strip brightness to max for Alarm
for(int i=0; i <= (numPixel / 2 - 1) ; i++) { // for half of strip size
leds[i] = topBot?colorTop:colorBottom ;
leds[i+ (numPixel/2)] = topBot?colorBottom:colorTop ;
}
topBot = !topBot ; // switch direction
FastLED.show();
}
// Simulate fire with red color, varying number of leds intensity & tempo
void patternFire() {
byte numberLeds = random(0,numPixel); // start number and end of led's for flickering
int lum = ((random(100,255) * curBrightness)) / 0xFF ; // set brightness and scale
CRGB color = CRGB(200, random(70,230),0 ); // get red color with varying green
for(int i=0; i <= numberLeds; i++) {
leds[i] = color ;
FastLED.setBrightness(lum); // set the strip brightness
FastLED.show();
wait(random(0,10)); // (blocking, need to be changed)
}
updatePatternDelay = 100 ;
}
// Simulate fire with red color and varying intensity & tempo
void patternFire2() {
CRGB color = CRGB(200, random(100,150),0); // get red color with varying green
for (byte p=0; p < numPixel; p++) {
leds[p] = color;
}
FastLED.setBrightness((random(50,255) * curBrightness)/ 0xFF ); // set Brightness and scale
FastLED.show();
updatePatternDelay = random(20,300); // variable delay
}
// Simulate candle based on fire with red color, varying number of leds intensity & tempo
void patternCandle() {
byte numberLeds = random(0,numPixel); // start number and end of led's for flickering
byte lum = ((random(100, 255) * curBrightness)/ 0xFF); // set brightness
CRGB color = CRGB(200, random(90,130),0 ); // get red color with varying green
for(int i=0; i <= numberLeds; i++) {
leds[i] = color ;
FastLED.setBrightness(lum); // set the strip brightness
FastLED.show();
wait(random(5,10)); // (blocking, need to be changed)
}
updatePatternDelay = 100 ;
}
// a colored dot sweeping back and forth, with fading trails, adapted from Fastled sinelon
void patternSinelon()
{
fadeToBlackBy( leds, numPixel, 10); // fade all leds a small amount
int pos = beatsin8(25,0,numPixel); // get a new position for the led (BPM = 13, min, max, )
leds[pos] += strtol( setRGBvalue, NULL, 16);
FastLED.show();
updatePatternDelay = 2 ;
}
// Rotate all Leds with current content and trail
void patternCircle() {
static int currentLed ; // indicated current led to light
// CRGB tempLed = leds[0]; // temporary variable for color
fadeToBlackBy( leds, numPixel, 128); // fade all leds for trail..
leds[currentLed] = strtol( setRGBvalue, NULL, 16); // set to current color
currentLed = (currentLed + 1) % numPixel ; // wrap
FastLED.show();
updatePatternDelay = 100 ;
}
void patternRainbow() {
static uint16_t hue ; // starting color
FastLED.clear();
// for(hue=10; hue<255*3; hue++) {
hue = (hue+1) % 0xFF ; // incerease hue and wrap
fill_rainbow( leds, numPixel , hue /*static hue value */, 1);// set a rainbow from hue to last in stepsize 1
FastLED.show();
updatePatternDelay = 100 ;
}
// Incoming messages from MySensors
void receive(const MyMessage &message) {
int ID = message.sensor;
Serial.print("Sensor: ");
Serial.println(ID);
switch (ID){
case RGB_LightChild: // same behaviour as RGB child/ fall through
case RGB_RGBChild: // if controller can handle V_RGB
if (message.type == V_RGB) { // check for RGB type
strcpy(setRGBvalue, message.getString()); // get the payload
setLightPattern(pSolid, 0); // and set solid pattern
} else if (message.type == V_DIMMER) { // if DIMMER type, adjust brightness
setBrightness = map(message.getInt(), 0, 100, 0, 255);
setLightBrightness(setBrightness, dimTime) ;
} else if (message.type == V_STATUS) { // if on/off type, toggle brightness
RGBonoff = message.getInt();
setLightBrightness((RGBonoff == 1)?setBrightness:0, dimTime);
}
break ;
case RGB_SolidColorChild: // request color from controller
if (message.type == V_STATUS) { // if get color from text child
request(RGB_TextColorChild, V_TEXT);
setLightPattern(pSolid, 0); // and set solid pattern (if not alre)
}
break ;
case RGB_TextColorChild: // Text color from controller
if (message.type == V_TEXT) { // if get color from text child
strcpy(setRGBvalue, message.getString()); // get the payload
for(int i = 0 ; i < 6 ; i++) { // save color value to EEPROM (6 char)
saveState(i, setRGBvalue[i]) ;} // Save to EEPROM
}
break ;
case RGB_AlarmPatternChild: // set Alarm pattern
if (message.type == V_STATUS) { // if get color from text child
if (message.getInt() == 1){
setLightPattern(pAlarm, 500); // set slow alarm pattern
} else {
setLightPattern(setPattern, 0); // and reset pattern
FastLED.setBrightness(setBrightness);
}
}
break ;
case RGB_NextPatternChild: // next pattern
if (message.type == V_PERCENTAGE) { // Percentage indicates the pattern
setPattern = map(message.getInt(), 0, 100, 0, 15) % lastPatternIdx ; // mapper dimmer value to state 0..9 and wrap
setLightPattern((setPattern), 500 );
Sprint("Pattern: ") ; Sprintln(setPattern) ;
} else if (message.type == V_STATUS){ // if off switch pattern to default == 0
setPattern = 0 ;
setLightPattern((setPattern), 500 );
Sprint("Pattern: ") ; Sprintln(setPattern) ;
}
break ;
}
FastLED.show();
dispRGBstat();
}
// debug
// display the status of all RGB: controller, requested, real
void dispRGBstat(void){
Serial.print(" Color: "); Serial.print(setRGBvalue);
Serial.print(" Brightness: "); Serial.println(setBrightness);
}
@BearWithBeard said in Relay Actuator with momentary (pulse) action:
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON)
Many thanks for coming back @BearWithBeard, its a huge help.
So my logic for correcting the State problem at the bottom would be to do the same calculation to get them in to 0 and 1.
saveState(message.getSensor()-7, message.getBool());
Compiled correctly and then uploaded and it works! Fantastic. The following sketch has 3 dimmers, 4 buttons and 2 relay outputs.
Thank you very much - I'm learning
/**
* 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 - February 15, 2014 - Bruce Lacey
* Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
*
* DESCRIPTION
* This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
* <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
* Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
*
* The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
* The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
* to the LED negative terminal and the MOSFET Source pin is connected to ground.
*
*/
// Button
// The below includes 4 buttons using code examples from here: https://forum.mysensors.org/topic/9538/multiple-switch-inputs/8
// Button
// Relay
// The below includes 2 relays from https://www.mysensors.org/build/relay
// https://forum.mysensors.org/topic/5906/relay-actuator-with-momentary-pulse-action/2
// Relay
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
// Enable RS485 transport layer
#define MY_RS485
// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
#define MY_NODE_ID 4
#include <SPI.h>
#include <MySensors.h>
// Button
#include <Bounce2.h>
//
#define SN "N004"
#define SV "1.0"
#define noLEDs 2
const int LED_Pin[] = {5, 6};
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
static int currentLevel1 = 0; // Current dim level...
static int currentLevel2 = 0; // Current dim level...
MyMessage dimmer1Msg(1, V_DIMMER);
MyMessage light1Msg(1, V_LIGHT);
MyMessage dimmer2Msg(2, V_DIMMER);
MyMessage light2Msg(2, V_LIGHT);
// Button
#define FIRST_BUTTON_ID 3
#define MAX_BUTTON 4
const uint8_t buttonPin[] = {4, 10, 11, 12}; // switch around pins to your desire
Bounce debouncer[MAX_BUTTON];
MyMessage buttonMsg(0, V_TRIPPED);
bool oldButton[MAX_BUTTON] = {false};
// Button
// Relay
#define FIRST_RELAY_ID 7
#define MAX_RELAY 2
const uint8_t relayPin[] = {3, 7}; // Pins of relays
MyMessage relayMsg(0, V_STATUS);
#define RELAY_ON 1
#define RELAY_OFF 0
// Relay
void before()
{
}
/***
* Dimmable LED initialization method
*/
void setup()
{
// LEDS
// Pull the gateway's current dim level - restore light level upon sendor node power-up
for (int sensor=1; sensor<=noLEDs; sensor++){
request( sensor, V_DIMMER );
}
// Button
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i] = Bounce(); // initialize debouncer
debouncer[i].attach(buttonPin[i], INPUT_PULLUP);
debouncer[i].interval(5);
oldButton[i] = debouncer[i].read();
}
// Button
// Relay
for (uint8_t i = 0; i < MAX_RELAY; i++) {
pinMode(relayPin[i], OUTPUT);
//digitalWrite(relayPin, loadState(i)?RELAY_ON:RELAY_OFF);
if (loadState(i) == RELAY_ON) {
digitalWrite(relayPin, RELAY_ON);
wait(500);
digitalWrite(relayPin, RELAY_OFF);
}
}
// Relay
}
void presentation() {
// Register the LED Dimmable Light with the gateway
for (int sensor=1; sensor<=noLEDs; sensor++){
present(sensor, S_DIMMER);
wait(2);
}
// Button
for (int i = 0; i < MAX_BUTTON; i++) { //i < numSensors &&
present(FIRST_BUTTON_ID + i, S_DOOR);
}
// Button
// Relay
for (int i = 0; i < MAX_RELAY; i++) {
present(FIRST_RELAY_ID + i, S_BINARY);
}
// Relay
sendSketchInfo(SN, SV);
}
/***
* Dimmable LED main processing loop
*/
void loop()
{
// Button
bool button[MAX_BUTTON];
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i].update();
button[i] = debouncer[i].read();
if (button[i] != oldButton[i]) {
send(buttonMsg.setSensor(FIRST_BUTTON_ID + i).set( button[i])); // Send tripped value to gw
oldButton[i] = button[i];
}
}
// Button
}
void receive(const MyMessage &message) {
if (message.type == V_LIGHT || message.type == V_DIMMER) {
if (message.sensor == 1) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel1 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel1 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel1 = requestedLevel1 > 100 ? 100 : requestedLevel1;
requestedLevel1 = requestedLevel1 < 0 ? 0 : requestedLevel1;
fadeToLevel1( requestedLevel1, message.sensor );
send(light1Msg.set(currentLevel1 > 0 ? 1 : 0));
send(dimmer1Msg.set(currentLevel1) );}
if (message.sensor == 2) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel2 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel2 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel2 = requestedLevel2 > 100 ? 100 : requestedLevel2;
requestedLevel2 = requestedLevel2 < 0 ? 0 : requestedLevel2;
fadeToLevel2( requestedLevel2, message.sensor );
send(light2Msg.set(currentLevel2 > 0 ? 1 : 0));
send(dimmer2Msg.set(currentLevel2) );}
}
// Relay
if (message.type == V_STATUS) {
if (message.sensor == 7) {
if (message.getBool() == RELAY_ON) {
//digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON);
wait(500);
//digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
digitalWrite(relayPin[message.getSensor()-7], RELAY_OFF);
}
saveState(message.getSensor()-7, message.getBool());
}
if (message.sensor == 8) {
if (message.getBool() == RELAY_ON) {
//digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
digitalWrite(relayPin[message.getSensor()-7], RELAY_ON);
wait(500);
//digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
digitalWrite(relayPin[message.getSensor()-7], RELAY_OFF);
}
saveState(message.getSensor()-7, message.getBool());
}
}
// Relay
}
/***
* This method provides a graceful fade up/down effect
*/
void fadeToLevel1( int toLevel1, int ledid1 ) {
int delta1 = ( toLevel1 - currentLevel1 ) < 0 ? -1 : 1;
while ( currentLevel1 != toLevel1 ) {
currentLevel1 += delta1;
analogWrite(LED_Pin[ledid1-1], (int)(currentLevel1 / 100. * 255) );
wait( FADE_DELAY );
}
}
void fadeToLevel2( int toLevel2, int ledid2 ) {
int delta2 = ( toLevel2 - currentLevel2 ) < 0 ? -1 : 1;
while ( currentLevel2 != toLevel2 ) {
currentLevel2 += delta2;
analogWrite(LED_Pin[ledid2-1], (int)(currentLevel2 / 100. * 255) );
wait( FADE_DELAY );
}
}
Morning all,
I'm smashing together 3 types of sketches here (3 x Dimmer, 4 x Button, 2 x relay) and am struggling with this 'momentary' relay action. The relays do not fire at all.
Everything is presented to the controller (Openhab) via an RS485 gateway, and the dimmer and buttons work and feedback their states. The log output shows that this device is receiving a 0 and 1 from Openhab on its relay nodes (7 and however, still no actuation.
I wonder if someone was able to take a cursery glance at my code. The relay sections of code are bracketed with // Relay
/**
* 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 - February 15, 2014 - Bruce Lacey
* Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
*
* DESCRIPTION
* This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
* <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
* Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
*
* The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
* The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
* to the LED negative terminal and the MOSFET Source pin is connected to ground.
*
*/
// Button
// The below includes 4 buttons using code examples from here: https://forum.mysensors.org/topic/9538/multiple-switch-inputs/8
// Button
// Relay
// The below includes 2 relays from https://www.mysensors.org/build/relay
// https://forum.mysensors.org/topic/5906/relay-actuator-with-momentary-pulse-action/2
// Relay
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
// Enable RS485 transport layer
#define MY_RS485
// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
#define MY_NODE_ID 4
#include <SPI.h>
#include <MySensors.h>
// Button
#include <Bounce2.h>
//
#define SN "N004"
#define SV "1.0"
#define noLEDs 2
const int LED_Pin[] = {5, 6};
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
static int currentLevel1 = 0; // Current dim level...
static int currentLevel2 = 0; // Current dim level...
MyMessage dimmer1Msg(1, V_DIMMER);
MyMessage light1Msg(1, V_LIGHT);
MyMessage dimmer2Msg(2, V_DIMMER);
MyMessage light2Msg(2, V_LIGHT);
// Button
#define FIRST_BUTTON_ID 3
#define MAX_BUTTON 4
const uint8_t buttonPin[] = {4, 10, 11, 12}; // switch around pins to your desire
Bounce debouncer[MAX_BUTTON];
MyMessage buttonMsg(0, V_TRIPPED);
bool oldButton[MAX_BUTTON] = {false};
// Button
// Relay
#define FIRST_RELAY_ID 7
#define MAX_RELAY 2
const uint8_t relayPin[] = {3, 7}; // Pins of relays
MyMessage relayMsg(0, V_STATUS);
#define RELAY_ON 1
#define RELAY_OFF 0
// Relay
void before()
{
}
/***
* Dimmable LED initialization method
*/
void setup()
{
// LEDS
// Pull the gateway's current dim level - restore light level upon sendor node power-up
for (int sensor=1; sensor<=noLEDs; sensor++){
request( sensor, V_DIMMER );
}
// Button
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i] = Bounce(); // initialize debouncer
debouncer[i].attach(buttonPin[i], INPUT_PULLUP);
debouncer[i].interval(5);
oldButton[i] = debouncer[i].read();
}
// Button
// Relay
for (uint8_t i = 0; i < MAX_RELAY; i++) {
pinMode(relayPin[i], OUTPUT);
//digitalWrite(relayPin, loadState(i)?RELAY_ON:RELAY_OFF);
if (loadState(i) == RELAY_ON) {
digitalWrite(relayPin, RELAY_ON);
wait(500);
digitalWrite(relayPin, RELAY_OFF);
}
}
// Relay
}
void presentation() {
// Register the LED Dimmable Light with the gateway
for (int sensor=1; sensor<=noLEDs; sensor++){
present(sensor, S_DIMMER);
wait(2);
}
// Button
for (int i = 0; i < MAX_BUTTON; i++) { //i < numSensors &&
present(FIRST_BUTTON_ID + i, S_DOOR);
}
// Button
// Relay
for (int i = 0; i < MAX_RELAY; i++) {
present(FIRST_RELAY_ID + i, S_BINARY);
}
// Relay
sendSketchInfo(SN, SV);
}
/***
* Dimmable LED main processing loop
*/
void loop()
{
// Button
bool button[MAX_BUTTON];
for (uint8_t i = 0; i < MAX_BUTTON; i++) {
debouncer[i].update();
button[i] = debouncer[i].read();
if (button[i] != oldButton[i]) {
send(buttonMsg.setSensor(FIRST_BUTTON_ID + i).set( button[i])); // Send tripped value to gw
oldButton[i] = button[i];
}
}
// Button
}
void receive(const MyMessage &message) {
if (message.type == V_LIGHT || message.type == V_DIMMER) {
if (message.sensor == 1) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel1 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel1 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel1 = requestedLevel1 > 100 ? 100 : requestedLevel1;
requestedLevel1 = requestedLevel1 < 0 ? 0 : requestedLevel1;
fadeToLevel1( requestedLevel1, message.sensor );
send(light1Msg.set(currentLevel1 > 0 ? 1 : 0));
send(dimmer1Msg.set(currentLevel1) );}
if (message.sensor == 2) {
// Retrieve the power or dim level from the incoming request message
int requestedLevel2 = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel2 *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100
requestedLevel2 = requestedLevel2 > 100 ? 100 : requestedLevel2;
requestedLevel2 = requestedLevel2 < 0 ? 0 : requestedLevel2;
fadeToLevel2( requestedLevel2, message.sensor );
send(light2Msg.set(currentLevel2 > 0 ? 1 : 0));
send(dimmer2Msg.set(currentLevel2) );}
}
// Relay
if (message.type == V_STATUS) {
if (message.sensor == 7) {
if (message.getBool() == RELAY_ON) {
digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
wait(500);
digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
}
saveState(message.sensor, message.getBool());
}
if (message.sensor == 8) {
if (message.getBool() == RELAY_ON) {
digitalWrite(message.getSensor()-1+relayPin, RELAY_ON);
wait(500);
digitalWrite(message.getSensor()-1+relayPin, RELAY_OFF);
}
}
}
// Relay
}
/***
* This method provides a graceful fade up/down effect
*/
void fadeToLevel1( int toLevel1, int ledid1 ) {
int delta1 = ( toLevel1 - currentLevel1 ) < 0 ? -1 : 1;
while ( currentLevel1 != toLevel1 ) {
currentLevel1 += delta1;
analogWrite(LED_Pin[ledid1-1], (int)(currentLevel1 / 100. * 255) );
wait( FADE_DELAY );
}
}
void fadeToLevel2( int toLevel2, int ledid2 ) {
int delta2 = ( toLevel2 - currentLevel2 ) < 0 ? -1 : 1;
while ( currentLevel2 != toLevel2 ) {
currentLevel2 += delta2;
analogWrite(LED_Pin[ledid2-1], (int)(currentLevel2 / 100. * 255) );
wait( FADE_DELAY );
}
}
Hello all, many thanks to @AWI for this great sketch - it performs very well and is my 'go to' for a WS28XX interface with Mysensors and Openhab.
I am however, starting to experience problems introduced with the FastLED 'wait' commands and the radio receiving a command - essentially, if I command the node to a pattern that is particularly blocking - I cant them command it to off, solid colour or another pattern.
Anyone solved this problem - or able to point me in the direction of solving. I'd be happy to refresh the sketch and re-share.
Does this need a resistor between the Arduino digital pin and the Gate of the MOSFET? To limit current draw?
No way - found it. Large comment at the top of sketch (not copied to this forum) had a line of *********s across the top.
Removed and all ok. Thanks for your help anyway @mfalkvidd
@mfalkvidd I had it running on a nano.
Now trying to put it in a mini pro.
However, cant get it to compile on either or an uno.
I'll re-install the IDE now and report back
@mfalkvidd just removed from the operating directory and installed via the Library Manager in the IDE - still same error.
Interestingly my RS485 Gateway sketch (Using the same library) compiles fine!
RS485GW (compiles fine)
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable RS485 transport layer
#define MY_RS485
// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
// Enable this if RS485 is connected to a hardware serial port
//#define MY_RS485_HWSERIAL Serial1
// Enable serial gateway
#define MY_GATEWAY_SERIAL
// 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
#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
#define MY_DEFAULT_RX_LED_PIN 5 // Receive led pin
#define MY_DEFAULT_TX_LED_PIN 6 // the PCB, on board LED
#include <MySensors.h>
void setup()
{
// Setup locally attached sensors
}
void presentation()
{
// Present locally attached sensors
}
void loop()
{
// Send locally attached sensor data here
}
RS485_WS2812_actuator (does not compile)
// Enable debug prints to serial monitor
#define MY_DEBUG
// Activate one of these
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#define MY_RS485
#define MY_RS485_DE_PIN 2
// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 9600
#include <MySensors.h>
#include <SPI.h>
#include <Adafruit_NeoPixel.h>
#define NUMPIXELS 58 // FIXME Number of connected pixels on a single datapin - Must be less than 500 due to RAM issues
#define PIN 4 // Digital output pin
#define MY_NODE_ID 1 // FIXME this needs to be decided and updated for this exact WS2812 node
#define CHILD_ID 0
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
long RGB_values[3] = {0,0,0};
void presentation()
{
sendSketchInfo("RS485_WS2812_Actuator", "1.0");
present(CHILD_ID, S_RGB_LIGHT);
}
void setup()
{
Serial.begin(9600);
Serial.println("Starting");
strip.begin();
strip.show(); // Update the strip, to start they are all 'off'
}
void loop()
{
}
void receive(const MyMessage &message) {
if (message.type==V_RGB) {
// starting to process the hex code
String hexstring = message.getString(); //here goes the hex color code coming from through MySensors (like FF9A00)
long number = (long) strtol( &hexstring[0], NULL, 16);
RGB_values[0] = number >> 16;
RGB_values[1] = number >> 8 & 0xFF;
RGB_values[2] = number & 0xFF;
colorWipe(Color(RGB_values[0],RGB_values[1],RGB_values[2]), 30);
}
if (message.type==V_DIMMER) {
strip.setBrightness(round((2.55*message.getInt())));
strip.show();
}
if (message.type==V_LIGHT) {
if (message.getInt() == 0) {
strip.clear();
strip.show();
}
}
}
void colorWipe(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
/* Helper functions */
// Create a 15 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
Good morning @mfalkvidd
I've not made amendements, I've tried to upload here, however received an error message with a 100% uploading. The library is 2.3.2 downlaoded from github.
[0_1608024751465_MyConfig.h](Uploading 100%)