@Boots33 I think I wasn't precise enough.
So below the sketch which is working fine but for one LED stripe. It remembers the last dimmer value.
I 've changed it a little bit. It works better for me to receive and display in Domoticz a % dimmer value info instead of information ON/OFF
send(lightMsg.set(dimmerSetting > 0 ? 1 : 0), false) changed to send(dimmerMsg.set(dimmerSetting));
Now my target is to use the maximum quantity of PWM pins in Arduino MEGA (in total I need 16 so I think I am gonna need two Arduinos)
Anyway could You help me please to add extra LEDs into this sketch ?
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#define MY_RF24_CE_PIN 49
#define MY_RF24_CS_PIN 53
#include <MySensors.h>
#define SN "Dimmable_LED"
#define SV "1.1"
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
static int16_t currentLevel = 0; // Current dim level...
int dimmerSetting = 10 ;
MyMessage dimmerMsg(0, V_PERCENTAGE);
MyMessage lightMsg(0, V_STATUS);
/***
* Dimmable LED initialization method
*/
void setup()
{
// Pull the gateway's current dim level - restore light level upon sendor node power-up
//request( 0, V_PERCENTAGE );
}
void presentation()
{
// Register the LED Dimmable Light with the gateway
present( 0, S_DIMMER );
sendSketchInfo(SN, SV);
}
/***
* Dimmable LED main processing loop
*/
void loop()
{
}
void receive(const MyMessage &message) {
switch (message.type) {
case V_STATUS: // message is from the switch
if(message.getBool()){
fadeToLevel( dimmerSetting ); // turn light on at current dimmer level
}
else fadeToLevel( 0 ); // fade light to 0 (off)
break;
case V_PERCENTAGE: // message is from the dimmer
dimmerSetting = message.getInt(); // get the new dimmer setting from the message
fadeToLevel( dimmerSetting ); // fade to the new dimmer setting
send(dimmerMsg.set(dimmerSetting)); // send switch state to controller , no ack requested
break;
}
}
/***
* This method provides a graceful fade up/down effect
*/
void fadeToLevel( int toLevel )
{
int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
while ( currentLevel != toLevel ) {
currentLevel += delta;
analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
wait( FADE_DELAY );
}
}