@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 );
}
}
As an OH3 user looking to upgrade, I'm wondering, do all of the binding points from OH to mysensor nodes, children etc work after upgrade and loading this new .jar?
Or do I have to re-build everything in OH?
Hi OSD, thanks for your input.
Those are the 4 relays on IO-MCU. I buzzed out the circuit and found that I had an open circuit on the 12v line to the relays.
Furthermore 2 of the relays indicator LEDs were illuminating, 2 were not.
I suspect, when I first juiced up the circuit on this MCU, the 2 relays fired, but I'd made the trace too thin for the required current. I wired up some jumper wire on the reverse of the PCB and the 2 relays started working.
The 2 that remain inoperable?
https://forum.arduino.cc/t/using-pins-a6-a7-on-pro-mini-compatible/118050
A6 and A7 can only be analogue inputs, cannot be digitally written to..... - so this part of my PCB is now redundant.
However, all in all, some good fault finding, found the two issues, and I'm now going away to re-think my PCB trace widths and reading the datasheets more accurately
Thanks
Hi, this may be a long one, but I'm seriously at my whits end and could do with some guidance on how to progress fault finding.
I've designed a PCB that, whilst looks confusing, is simple enough. It consists of 4 Arduino Pro Minis - 3 of which have MAX485 circuits, plumbed in to an RS485 network (in and out), Off PCB there is a MySensors gateway in to Openhab Instance.
On the whole, everything works - there are over 14 'nodes', arduino pro minis dotted around the network which all communicate properly, including the other 2 working nodes on this PCB. And I do believe that the defective node (#12 - IO_MCU) is communicating correctly, but relays just simply do not pull in.
I'm an electrical Engineer by trade, I've got the relays working on the Alarm MCU, and the circuit is exactly the same for each.
I think my problem is with the configuration of the Arduino Sketch - maybe something to do with the Analogue pins, output type, or something
https://pastebin.com/RH2X8Ja9 - Arduino Sketch for the IO_MCU.
// Dimmer
Tags bracket the PWM controller output sections
// Button
Tags bracket the button input sections
// Relay
Tags bracket the relay output sections
When listening over serial to the node, I can see it is getting comms, presents all its IO - and even received on and off commands for relay children - however, no action on the PCB.
Similarly Openhab logs give all of the node 12s children as inbox things, I can tie them to items and command them on / off.
__ __ ____
| \/ |_ _/ ___| ___ _ __ ___ ___ _ __ ___
| |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
| | | | |_| |___| | __/ | | \__ \ _ | | \__ \
|_| |_|\__, |____/ \___|_| |_|___/\___/|_| |___/
|___/ 2.3.2
16 MCO:BGN:INIT NODE,CP=RSNNA---,FQ=16,REL=255,VER=2.3.2
26 MCO:BGN:BFR
28 TSM:INIT
29 TSF:WUR:MS=0
30 TSM:INIT:TSP OK
32 TSM:INIT:STATID=12
34 TSF:SID:OK,ID=12
36 TSM:FPAR
61 ?TSF:MSG:SEND,12-12-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
346 TSF:MSG:READ,0-0-12,s=255,c=3,t=8,pt=1,l=1,sg=0:0
351 TSF:MSG:FPAR OK,ID=0,D=1
2069 TSM:FPAR:OK
2070 TSM:ID
2071 TSM:ID:OK
2073 TSM:UPL
2092 TSF:MSG:SEND,12-12-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
2121 TSF:MSG:READ,0-0-12,s=255,c=3,t=25,pt=1,l=1,sg=0:1
2126 TSF:MSG:PONG RECV,HP=1
2129 TSM:UPL:OK
2130 TSM:READY:ID=12,PAR=0,DIS=1
2152 TSF:MSG:SEND,12-12-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
2186 TSF:MSG:READ,0-0-12,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
2213 TSF:MSG:SEND,12-12-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.2
2247 TSF:MSG:SEND,12-12-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
2284 TSF:MSG:READ,0-0-12,s=255,c=3,t=6,pt=0,l=1,sg=0:M
2306 TSF:MSG:SEND,12-12-0-0,s=1,c=0,t=4,pt=0,l=0,sg=0,ft=0,st=OK:
2330 TSF:MSG:SEND,12-12-0-0,s=2,c=0,t=4,pt=0,l=0,sg=0,ft=0,st=OK:
2355 TSF:MSG:SEND,12-12-0-0,s=3,c=0,t=4,pt=0,l=0,sg=0,ft=0,st=OK:
2379 TSF:MSG:SEND,12-12-0-0,s=4,c=0,t=4,pt=0,l=0,sg=0,ft=0,st=OK:
2404 TSF:MSG:SEND,12-12-0-0,s=5,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2427 TSF:MSG:SEND,12-12-0-0,s=6,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2464 TSF:MSG:SEND,12-12-0-0,s=7,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2498 TSF:MSG:SEND,12-12-0-0,s=8,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2521 TSF:MSG:SEND,12-12-0-0,s=9,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2543 TSF:MSG:SEND,12-12-0-0,s=10,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2584 TSF:MSG:SEND,12-12-0-0,s=11,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2627 TSF:MSG:SEND,12-12-0-0,s=12,c=0,t=0,pt=0,l=0,sg=0,ft=0,st=OK:
2651 TSF:MSG:SEND,12-12-0-0,s=13,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2690 TSF:MSG:SEND,12-12-0-0,s=14,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2718 TSF:MSG:SEND,12-12-0-0,s=15,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2749 TSF:MSG:SEND,12-12-0-0,s=16,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2778 TSF:MSG:SEND,12-12-0-0,s=255,c=3,t=11,pt=0,l=4,sg=0,ft=0,st=OK:N012
2808 TSF:MSG:SEND,12-12-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
2814 MCO:REG:REQ
2845 TSF:MSG:SEND,12-12-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
2886 TSF:MSG:READ,0-0-12,s=255,c=3,t=27,pt=1,l=1,sg=0:1
2891 MCO:PIM:NODE REG=1
2893 MCO:BGN:STP
2912 TSF:MSG:SEND,12-12-0-0,s=1,c=2,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2951 TSF:MSG:SEND,12-12-0-0,s=2,c=2,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
2991 TSF:MSG:SEND,12-12-0-0,s=3,c=2,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
3017 TSF:MSG:SEND,12-12-0-0,s=4,c=2,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
3023 MCO:BGN:INIT OK,TSP=1
3054 TSF:MSG:READ,0-0-12,s=3,c=1,t=3,pt=0,l=1,sg=0:0
3090 TSF:MSG:SEND,12-12-0-0,s=3,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
3119 TSF:MSG:SEND,12-12-0-0,s=3,c=1,t=3,pt=2,l=2,sg=0,ft=0,st=OK:0
3155 TSF:MSG:READ,0-0-12,s=4,c=1,t=3,pt=0,l=1,sg=0:0
3184 TSF:MSG:SEND,12-12-0-0,s=4,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0
3227 TSF:MSG:SEND,12-12-0-0,s=4,c=1,t=3,pt=2,l=2,sg=0,ft=0,st=OK:0
92765 TSF:MSG:READ,0-0-12,s=16,c=1,t=2,pt=0,l=1,sg=0:1
99421 TSF:MSG:READ,0-0-12,s=16,c=1,t=2,pt=0,l=1,sg=0:0
202292 TSF:MSG:READ,0-0-12,s=16,c=1,t=2,pt=0,l=1,sg=0:1
202394 TSF:MSG:READ,0-0-12,s=16,c=1,t=2,pt=0,l=1,sg=0:0
203639 TSF:MSG:READ,0-0-12,s=16,c=1,t=2,pt=0,l=1,sg=0:1
204679 TSF:MSG:READ,0-0-12,s=16,c=1,t=2,pt=0,l=1,sg=0:0
Any and all help is appreciated. I get that this is a big ask, I'm more than willing to give any more data that is required - testing, serial prints etc etc. Just struggling right now.
Appreciate your time.
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