Hey @mfalkvidd,
I was working on a similar project with @petewill and worked up a non-blocking version of @blacey code which allows you to use the dimmer controller as a repeating node.
here is what I came up with, sorry the class wasn't pulled out into a header/implementation file.
It does compile and I have tested it, some notes on how to use in the code...
have fun building your project, whichever LEDs you choose.
/**
* 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)
* Version 1.2 - December 7, 2015 - converted to non-blocking code to facilitate multiple outputs and repeater node (pseudo-threading) <bulldoglowell>
*
* 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(s).
* 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.
*
* This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
* http://www.mysensors.org/build/dimmer
*/
#define SN "DimmableLED"
#define SV "1.1"
#include <MySensor.h>
#include <SPI.h>
#define LED_PIN_0 3 // Arduino PWM capable pin attached to MOSFET Gate pin
#define LED_PIN_1 4
#define FADE_DELAY 15 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
class Fade
{
public:
Fade() {};
~Fade() {};
Fade(int pin, uint32_t timeStep = 15, uint8_t min = 0, uint8_t max = 255);
void
write(int to),
update(void),
update(uint32_t time),
begin(void),
begin(int pin, uint32_t timeStep = 15),
begin(int pin, uint32_t timeStep = 15, uint8_t min = 0, uint8_t max = 255);
uint8_t
read(void),
getSetpoint(void);
uint32_t
readSpeed(void),
writeSpeed(uint32_t time);
private:
uint8_t
_min,
_max,
_targetFade,
_pwmRate,
_pin;
uint32_t
_timeStep,
_last;
};
Fade::Fade(int pin, uint32_t timeStep, uint8_t min, uint8_t max)
{
_pin = pin;
_timeStep = timeStep;
_min = min;
_max = max;
pinMode(_pin, OUTPUT);
analogWrite(_pin, _min);
_pwmRate = _min;
}
void Fade::begin(void)
{
pinMode(_pin, OUTPUT);
}
void Fade::begin(int pin, uint32_t timeStep)
{
_pin = pin;
_timeStep = timeStep;
_min = 0;
_max = 255;
pinMode(_pin, OUTPUT);
analogWrite(_pin, _min);
_pwmRate = _min;
}
void Fade::begin(int pin, uint32_t timeStep, uint8_t min, uint8_t max)
{
_pin = pin;
_timeStep = timeStep;
_min = min;
_max = max;
pinMode(_pin, OUTPUT);
analogWrite(_pin, _min);
_pwmRate = _min;
}
void Fade::write(int to)
{
_targetFade = (uint8_t) constrain(to, _min, _max);
this->update();
}
void Fade::update()
{
this->update(millis());
}
void Fade::update(uint32_t time)
{
if (time - _timeStep > _last)
{
_last = time;
if (_pwmRate > _targetFade) analogWrite(_pin, --_pwmRate);
if (_pwmRate < _targetFade) analogWrite(_pin, ++_pwmRate);
}
}
uint8_t Fade::getSetpoint()
{
return _targetFade;
}
uint8_t Fade::read()
{
return _pwmRate;
}
uint32_t Fade::readSpeed()
{
return _timeStep;
}
uint32_t Fade::writeSpeed(uint32_t time)
{
_timeStep = time;
}
Fade led[2]; // Create two Fade objects, led[0] & led[1]
MySensor gw;
MyMessage dimmer0Msg(0, V_DIMMER);
MyMessage lightM0sg(0, V_LIGHT);
MyMessage dimmer1Msg(1, V_DIMMER);
MyMessage light1Msg(1, V_LIGHT);
void setup()
{
//Serial.begin(9600);
led[0].begin(LED_PIN_0, FADE_DELAY, 0, 255); // initialize the Fade objects
led[0].write(0); // set initial value to "off"
led[1].begin(LED_PIN_1, FADE_DELAY, 0, 255);
led[1].write(0);
gw.begin(incomingMessage, true);
gw.present( 0, S_DIMMER );
gw.request( 0, V_DIMMER );
gw.present( 1, S_DIMMER );
gw.request( 1, V_DIMMER );
gw.sendSketchInfo(SN, SV);
}
void loop()
{
gw.process();
led[0].update(); // must keep this command in loop for every Fade object
led[1].update(); // calling update transitions the PWM towards the setpoint, without blocking
}
void incomingMessage(const MyMessage &message)
{
if (message.type == V_LIGHT || message.type == V_DIMMER)
{
int requestedLevel = atoi(message.data);
requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
Serial.print(F("Changing led["));
Serial.print(message.sensor);
Serial.print(F("] level to "));
Serial.print(requestedLevel);
Serial.print(F(" from "));
Serial.println(map(led[message.sensor].getSetpoint(), 0, 255, 0, 100));
led[message.sensor].write(map(requestedLevel, 0, 100, 0, 255));
}
}