Adapting Example Code for Krida Triac Dimmer

Hi, I'm new. Bought a 4-channel Krida Triac Dimmer and an Uno and HC-5 module. I would like to use for 2 circuits of Led lamps in new pantry. I couldn't download a photo, but will keep at it. I was able to put Krida supplied code into IDE and have that below. I have it working as advertised, but need to change program for my application.

The plan is to have the lights come on to the desired level, and stay on while the power is on. Once I close door, both the Arduino and the dimmer will have the power cut off. When the pantry door opens, mains ac is switched on to both the Arduino's power adapter, and to the high-power side of the dimmer board. Only two channels are used on the dimmer.

Here is what I'd like to modify:

[code]
// This is me trying to make imported Krida Bluetooth 50hz work for 60hz.  Don't know why it's called secondtry.  Here goes.
//
// Testing sketch for 50Hz !!!
//
// Bluetooth Module HC-05, Speed 9600kbps
//
// How to connect dimmer and hc-05 modules to Arduino UNO board
//
// Dimmer  Arduino
//   |             |
//  GND           GND
//  VCC           5V
//  SYNC        DIGITAL.3
//  CH1         DIGITAL.7
//  CH2         DIGITAL.6
//  CH3         DIGITAL.5
//  CH4         DIGITAL.4
//
//  HC-05       Arduino UNO  
//   |             |
//  GND           GND
//  VCC           5V
//  TX            RX (DIGITAL.0) 


#include <TimerOne.h>    // download this library from arduino.cc

unsigned char channel_1 = 7; // Output to Opto Triac pin, channel 1
unsigned char channel_2 = 6; // Output to Opto Triac pin, channel 2
unsigned char channel_3 = 5; // Output to Opto Triac pin, channel 3
unsigned char channel_4 = 4; // Output to Opto Triac pin, channel 4
unsigned char CH1, CH2, CH3, CH4;
unsigned char i=0;
unsigned int delay_time=2500; // delay ms or SPEED
unsigned char clock_tick; // variable for Timer1
int incomingByte = 0;
int mas[3];


void setup() {

Serial.begin(9600);
pinMode(channel_1, OUTPUT);// Set AC Load pin as output
pinMode(channel_2, OUTPUT);// Set AC Load pin as output
pinMode(channel_3, OUTPUT);// Set AC Load pin as output
pinMode(channel_4, OUTPUT);// Set AC Load pin as output
attachInterrupt(1, zero_crosss_int, RISING);
Timer1.initialize(83); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
Timer1.attachInterrupt( timerIsr ); // attach the service routine here

CH1=CH2=CH3=CH4=95; // WHEN ARDUINO START, ALL LIGHT OFF (95 - FULLY OFF ; 5 - FULLY ON) !!!

}

void timerIsr()
{
clock_tick++;

if (CH1==clock_tick)
{
digitalWrite(channel_1, HIGH); // triac firing
delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_1, LOW); // triac Off
}

if (CH2==clock_tick)
{
digitalWrite(channel_2, HIGH); // triac firing
delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_2, LOW); // triac Off
}

if (CH3==clock_tick)
{
digitalWrite(channel_3, HIGH); // triac firing
delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_3, LOW); // triac Off
}

if (CH4==clock_tick)
{
digitalWrite(channel_4, HIGH); // triac firing
delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_4, LOW); // triac Off
}
}



void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms ; For 60Hz (1/2 Cycle) => 8.33ms
// 10ms=10000us , 8.33ms=8330us

clock_tick=0;
}



void loop() {

if (Serial.available() > 0) { 

incomingByte = Serial.read();

if (incomingByte=='X')
{
mas[0]=Serial.read();
mas[1]=Serial.read();
mas[2]=Serial.read();
Serial.flush();
}

if (mas[0]=='A') { if(mas[1]>=0) {CH1=mas[1]; CH1=100-CH1; if (CH1<5) {CH1=5;} if (CH1>95) {CH1=95;}}}
if (mas[0]=='B') { if(mas[1]>=0) {CH2=mas[1]; CH2=100-CH2; if (CH2<5) {CH2=5;} if (CH2>95) {CH2=95;}}}
if (mas[0]=='C') { if(mas[1]>=0) {CH3=mas[1]; CH3=100-CH3; if (CH3<5) {CH3=5;} if (CH3>95) {CH3=95;}}}
if (mas[0]=='D') { if(mas[1]>=0) {CH4=mas[1]; CH4=100-CH4; if (CH4<5) {CH4=5;} if (CH4>95) {CH4=95;}}}

}

Serial.print("CH1 ");Serial.print(CH1);
Serial.print("; CH2 ");Serial.print(CH2);
Serial.print("; CH3 ");Serial.print(CH3);
Serial.print("; CH4 ");Serial.println(CH4);


}
[/code]`Preformatted text`

What I need instead is code which performs differently in two respects.

I do not need to use a phone app to turn on and dim lights. Far better to have the brightness levels written into the program. They will be different for each channle, but will not vary after dimmer is installed. How can I substitute constants for the target brightness in this code?

I'd like the two channels to ramp up to the set brightness level. In some of the trial and error I've been at the brightness increased incrementally each loop, and I could adjust a delay which effected the fade rate. That was in a different sketch, using a different library, and I'd like to do that here.

I know it's alot. Perhaps I can take these one at a time. First lose all the code for the blutooth and the unnecessary channels, and identfy where and how to write in the target brightness values.

Thanks for having a look.

Note:
I don't have the dimmer so can't test.

The below from your code sets the initial values of the brightness.

  CH1 = CH2 = CH3 = CH4 = 95; // WHEN ARDUINO START, ALL LIGHT OFF (95 - FULLY OFF ; 5 - FULLY ON) !!!

You can use something like

CH1=33;
CH2 = 55;
CH3 = 95;
CH4 = 95;

To get rid of the code for the HC-05, just empty loop() for starters.

void loop()
{
}

With those changes, you should have a code that will switch on the lights at a given brightness when you power on the Arduino and the dimmer.

I will try to find some time to get you on the way with the fading. Do you want the levels and ramp speed to be configurable so you can change them via Serial or the phone app?

Sterretje:

Thanks for the help. Can't wait to implement, later today. About the fade-on, it does not need to be configurable if I understand.

Once I'm done testing and install the Uno and dimmer board, I will not have to make any changes.

I'll post to forum tonight, and will include a bit from another sketch. There's a expression for brightness which ends with a +.01, and then changing the value in a delay is how to change fade rate. Scale 0.0 to 1.0.

But I could not think how to make work with two discreet brightness values. Also, I was stumped on how to get it to stay on. The code just ramped up to one level, the looped back to zero and started over.

Will post soon. Thank you, again

I think I must have missed something. The part about identifying the variables to set brightness, that is probably right. Could it be that when I emptied the void loop () {} I inhibited more than the BT part of the program? When I tried this, I did not get the light to come on.

I had a voltage I could measure, but it would not light leds. When I took the dimmer out of the circuit [ I connected the line AC into the 24v led driver directly] the light came on. So, one thing at a time, right? Can anyone see why this doesn't dim now? The original code will dim, when the HC-5 module is connected and the phone is brought into the mix. Below is the sketch I uploaded and ran, today.

[code]
//No Soap!  Measured 18vDC on way to leds - can't think why it doesn't light.  Lights with dimmer out of circuit, at full 24VDC.
//
//Here is version to get rid of bluetooth, and have two preset brightness values.  Help from forum, last night.
//
//
//
//This is me trying to make imported Krida Bluetooth 50hz work for 60hz.  Don't know why it's called secondtry.  Here goes.
//
// Testing sketch for 50Hz !!!
//
//
//
// Bluetooth Module HC-05, Speed 9600kbps
//
// How to connect dimmer and hc-05 modules to Arduino UNO board
//
// Dimmer  Arduino
//   |             |
//  GND           GND
//  VCC           5V
//  SYNC        DIGITAL.3
//  CH1         DIGITAL.7
//  CH2         DIGITAL.6
//  CH3         DIGITAL.5
//  CH4         DIGITAL.4
//
//  HC-05       Arduino UNO
//   |             |
//  GND           GND
//  VCC           5V
//  TX            RX (DIGITAL.0)


#include <TimerOne.h>    // download this library from arduino.cc

unsigned char channel_1 = 7; // Output to Opto Triac pin, channel 1
unsigned char channel_2 = 6; // Output to Opto Triac pin, channel 2
unsigned char channel_3 = 5; // Output to Opto Triac pin, channel 3
unsigned char channel_4 = 4; // Output to Opto Triac pin, channel 4
unsigned char CH1, CH2, CH3, CH4;
unsigned char i = 0;
unsigned int delay_time = 2500; // delay ms or SPEED
unsigned char clock_tick; // variable for Timer1
int incomingByte = 0;
int mas[3];


void setup() {

  Serial.begin(9600);
  pinMode(channel_1, OUTPUT);// Set AC Load pin as output
  pinMode(channel_2, OUTPUT);// Set AC Load pin as output
  pinMode(channel_3, OUTPUT);// Set AC Load pin as output
  pinMode(channel_4, OUTPUT);// Set AC Load pin as output
  attachInterrupt(1, zero_crosss_int, RISING);
  Timer1.initialize(83); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here

  CH1 = 80;
  CH2 = 40;
  CH3 = CH4 = 95; // WHEN ARDUINO START, ALL LIGHT OFF (95 - FULLY OFF ; 5 - FULLY ON) !!!

}

void timerIsr()
{
  clock_tick++;

  if (CH1 == clock_tick)
  {
    digitalWrite(channel_1, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_1, LOW); // triac Off
  }

  if (CH2 == clock_tick)
  {
    digitalWrite(channel_2, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_2, LOW); // triac Off
  }

  if (CH3 == clock_tick)
  {
    digitalWrite(channel_3, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_3, LOW); // triac Off
  }

  if (CH4 == clock_tick)
  {
    digitalWrite(channel_4, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_4, LOW); // triac Off
  }
}



void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms ; For 60Hz (1/2 Cycle) => 8.33ms
  // 10ms=10000us , 8.33ms=8330us

  clock_tick = 0;
}



void loop() {

}
[/code]

Thanks very much for taking the time to look.

The code that I provided just sets a fixed brightness for the channels.

I've checked your original code and I can not see how emptying loop() would affect the working of a fixed brightness.

Which led driver? We were talking about a dimmer module, not a led driver.

Ok Hi, sorry it does work.

I just now finished testing. I wish I could say why I got that wrong before I posted the last two times, but I just imagine I became frazzled and made a careless mistake. I have been trying to adapt two different examples, and they use different pins. Probably that was it.

So now thanks to kind help I have a sketch which will turn on two channels of led lighting to different preset brightness levels. The range is 95 (off) to 5 (fully on.) Above 60 there is a perceptible flicker, and below 20 the leds are quite bright. Nonetheless, I can work within the 60 to 20 range, and am very happy about this!

Very briefly, we were talking about a led module, which is now no longer operative. From the triacs, each channel feeds a phase dimmable led driver. Input 120VAC output 24VDC. The manufacturer of these drivers says that they are triac dimmable. This is what put me searching for 2 channel triac dimmers in the first place, and which led me to the Krida board. The two power outputs from that board are controlled by digital pins 3,7 & 6 of the Uno. Those match with SYNCH, CH1 & CH2, respectively.

Now my problem has become finding a way to modify what I have - if possible - to make the brightness level ramp up in both channels.

What the this edited Krida example sketch had going for it in the first place is that originally it did permit me to adjust two brightness levels; I knew that part of it worked. But I have no indication that this can be made to ramp up to level. In case you have any ideas of what type of changes I might try, I'd appreciate the help. Even in case you remember a sketch I might study for hints, that would be great.

Based on the sketch in post #4

The first step is to rename the CHx variables to give them more sensible names as they now reflect the maximum brightnes.
The change would be from

unsigned char CH1, CH2, CH3, CH4;

to

unsigned char maxBrightness1, maxBrightness2, maxBrightness3, maxBrightness4;

And e.g. in setup() from

  CH1 = 80;
  CH2 = 40;
  CH3 = CH4 = 95; // WHEN ARDUINO START, ALL LIGHT OFF (95 - FULLY OFF ; 5 - FULLY ON) !!!

to

  maxBrightness1 = 80;
  maxBrightness2 = 40;
  maxBrightness3 = maxBrightness4 = 95;

There are more places where it needs to be changed; easiest is to do a search and replace.

Next you need variables to keep track of the current brightness during the ramping; they will start with 95 which indicates OFF.

unsigned char curBrightness1 = 95, curBrightness2 = 95, curBrightness3 = 95, curBrightness4 = 95;

You also need variables for the speed of the ramping and possibly the steps for each increase of the brightness. In the below, all dimmers will use the same setting.

// increase brightness by 5 every second
unsigned int rampDelay = 1000;
unsigned char rampStep = 5;

Now you can implement a ramping in e.g. loop().

void loop()
{
  delay(rampDelay);
  curBrightness1 -= rampStep;
  if(curBrightness1 < maxBrightness1)
  {
    curBrightness1 = maxBrightness1;
  }
  Serial.print("CH1 brightness = ");
  Serial.println(curBrightness1);
}

You can repeat this for the second dimmer; the delay statement only occurs once in loop(), not for every dimmer.

Some comments; I've kept the 'mistakes' in the code for now.

  1. delayMicroseconds does not take a flout as the argument but an unsigned long. So 8.33will be rounded to 8.
  2. The first argument of attachInterrupt(1, zero_crosss_int, RISING) is the interrupt vector for pin 3; if you ever want to move your sketch to e.g. a Mega or Leonardo, it might not work. It's better to use attachInterrupt(digitalPinToInterrupt(3), zero_crosss_int, RISING). And it would even be better if you used a name for the pin like you did for the channel_X and use that name instead of 3.
  3. I guess that this isn't true: // Testing sketch for 50Hz !!!. See the comment in zero_crosss_int().

The full sketch below. You can use Serial Monitor to follow the dimming. If it works for one channel, you can add the second one yourself. One thing that changed, compared to the above description, was the type of variables from unsigned char to unsigned int.

Let me know if it works, there are a number of optimisations that can be made. E.g. each time that you start numbering variables like channel_1, channel_2 etc, you must start thinking arrays. And channel_1 is associated with maxBrightness1 and curBrightness1 and you should start thing of structs or classes.

// https://forum.arduino.cc/t/adapting-example-code-for-krida-triac-dimmer/949432

//No Soap!  Measured 18vDC on way to leds - can't think why it doesn't light.  Lights with dimmer out of circuit, at full 24VDC.
//
//Here is version to get rid of bluetooth, and have two preset brightness values.  Help from forum, last night.
//
//This is me trying to make imported Krida Bluetooth 50hz work for 60hz.  Don't know why it's called secondtry.  Here goes.
//
// Testing sketch for 50Hz !!!
//
//
//
// How to connect dimmer and hc-05 modules to Arduino UNO board
//
// Dimmer  Arduino
//   |             |
//  GND           GND
//  VCC           5V
//  SYNC        DIGITAL.3
//  maxBrightness1         DIGITAL.7
//  maxBrightness2         DIGITAL.6
//  maxBrightness3         DIGITAL.5
//  maxBrightness4         DIGITAL.4
//


#include <TimerOne.h>    // download this library from arduino.cc

unsigned char channel_1 = 7; // Output to Opto Triac pin, channel 1
unsigned char channel_2 = 6; // Output to Opto Triac pin, channel 2
unsigned char channel_3 = 5; // Output to Opto Triac pin, channel 3
unsigned char channel_4 = 4; // Output to Opto Triac pin, channel 4
unsigned int maxBrightness1, maxBrightness2, maxBrightness3, maxBrightness4;
unsigned int curBrightness1 = 95, curBrightness2 = 95, curBrightness3 = 95, curBrightness4 = 95;
unsigned char clock_tick; // variable for Timer1

// increase brightness by 5 every second
unsigned int rampDelay = 1000;
unsigned char rampStep = 5;


void setup()
{

  Serial.begin(9600);
  pinMode(channel_1, OUTPUT);// Set AC Load pin as output
  pinMode(channel_2, OUTPUT);// Set AC Load pin as output
  pinMode(channel_3, OUTPUT);// Set AC Load pin as output
  pinMode(channel_4, OUTPUT);// Set AC Load pin as output
  attachInterrupt(digitalPinToInterrupt(3), zero_crosss_int, RISING);
  Timer1.initialize(83); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here

  maxBrightness1 = 80;
  maxBrightness2 = 40;
  maxBrightness3 = maxBrightness4 = 95; // WHEN ARDUINO START, ALL LIGHT OFF (95 - FULLY OFF ; 5 - FULLY ON) !!!

}

void timerIsr()
{
  clock_tick++;

  if (maxBrightness1 == clock_tick)
  {
    digitalWrite(channel_1, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_1, LOW); // triac Off
  }

  if (maxBrightness2 == clock_tick)
  {
    digitalWrite(channel_2, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_2, LOW); // triac Off
  }

  if (maxBrightness3 == clock_tick)
  {
    digitalWrite(channel_3, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_3, LOW); // triac Off
  }

  if (maxBrightness4 == clock_tick)
  {
    digitalWrite(channel_4, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_4, LOW); // triac Off
  }
}


void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms ; For 60Hz (1/2 Cycle) => 8.33ms
  // 10ms=10000us , 8.33ms=8330us

  clock_tick = 0;
}

void loop()
{
  delay(rampDelay);
  // adjust the brightness
  curBrightness1 -= rampStep;
  // check if max brightness reached
  if (curBrightness1 < maxBrightness1)
  {
    curBrightness1 = maxBrightness1;
  }
  // info
  Serial.print("CH1 brightness = ");
  Serial.println(curBrightness1);
}

Hi. I have tried to implement the changes first, and then afterwards I attempted to run the code you were kind enough to add just above.

Below is the updated version where I made many of the changes, in particular to add variables for a ramp - up loop. I think I copied the channel 1 ramp up loop at the end of your code correctly. It compiles and runs (but no ramp up) if I leave off my try at adding channel 2 ramp into the loop. When I put the same code but for channel 2 on after your channel 1 code, the sketch would not compile.

`

Preformatted text`void loop() // effort at ramping up channel 1

{

  delay(rampDelay);
  curBrightness1 -= rampStep;

  if (curBrightness1 < maxBrightness1)
  {
    curBrightness1 = maxBrightness1;
  }
  Serial.print("CH1 brightness = ");
  Serial.println(curBrightness1);

  curBrightness2 -= rampStep;
  if (curBrightness2 < maxBrightness2)
  {
    curBrightness2 = maxBrightness2;
  }
  Serial.print ("CH2 brightness =");
  Serial.println(curBrightness2);

[/code]`Preformatted text```Preformatted text`

This is what I could not compile. The error message was :
'curBrightness2' was not declared in this scope.

I then left off the bit for channel 2, and could upload the sketch. Here is that code

`Preformatted text`[code]

// This sketch will turn on to two preset brightness values.  Anything greater than 60 will result in a low dim level that flickers perceptably.  Anything less than 20 is really very bright.
// 40 and 60 work great for me.    If there's a way to make this ramp-up and hold for two channels, it'll be all I need.


// This isn't working as expected.  Tried implementing new directions, and 1) couldn't get ramp-up for channel 2 to compile.  I tried tacking it on after the one for channel 1, but kept getting
// message:  curBrightness2 is not declared in this scope.  2)  Uploaded and ran, and both lamps came on at same time.  I expected channel 1 to ramp up.


//
//
//
// How to connect dimmer modules to Arduino UNO board
//
// Dimmer  Arduino
//   |             |
//  GND           GND
//  VCC           5V
//  SYNC        DIGITAL.3
//  CH1         DIGITAL.7
//  CH2         DIGITAL.6
//  CH3         DIGITAL.5
//  CH4         DIGITAL.4
//
//


#include <TimerOne.h>    // download this library from arduino.cc

unsigned char channel_1 = 7; // Output to Opto Triac pin, channel 1
unsigned char channel_2 = 6; // Output to Opto Triac pin, channel 2
unsigned char channel_3 = 5; // Output to Opto Triac pin, channel 3
unsigned char channel_4 = 4; // Output to Opto Triac pin, channel 4
unsigned int maxBrightness1, maxBrightness2, maxBrightness3, maxBrightness4;
unsigned int curBrightness1 = 95, curBrighness2 = 95, curBrightness3 = 95, curBrightness4 = 95;
unsigned char i = 0;
unsigned int delay_time = 2500; // delay ms or SPEED
unsigned char clock_tick; // variable for Timer1
int incomingByte = 0;
int mas[3];

// increase brightness by 5 every second

unsigned int rampDelay = 1000;
unsigned char rampStep = 5;


void setup() {

  Serial.begin(9600);
  pinMode(channel_1, OUTPUT);// Set AC Load pin as output
  pinMode(channel_2, OUTPUT);// Set AC Load pin as output
  pinMode(channel_3, OUTPUT);// Set AC Load pin as output
  pinMode(channel_4, OUTPUT);// Set AC Load pin as output
  attachInterrupt(digitalPinToInterrupt(3), zero_crosss_int, RISING);
  Timer1.initialize(83); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here

  maxBrightness1 = 40; //  set maxBrightness for channel 1
  maxBrightness2 = 60; //  set maxBrightness for channel 2
  maxBrightness3 = maxBrightness4 = 95; //  (95 - FULLY OFF ; 5 - FULLY ON) !!!

}

void timerIsr()
{
  clock_tick++;

  if (maxBrightness1 == clock_tick)
  {
    digitalWrite(channel_1, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_1, LOW); // triac Off
  }

  if (maxBrightness2 == clock_tick)
  {
    digitalWrite(channel_2, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_2, LOW); // triac Off
  }

  if (maxBrightness3 == clock_tick)
  {
    digitalWrite(channel_3, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_3, LOW); // triac Off
  }

  if (maxBrightness4 == clock_tick)
  {
    digitalWrite(channel_4, HIGH); // triac firing
    delayMicroseconds(8.33); // triac On propogation delay (for 60Hz use 8.33)
    digitalWrite(channel_4, LOW); // triac Off
  }
}



void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms ; For 60Hz (1/2 Cycle) => 8.33ms
  // 10ms=10000us , 8.33ms=8330us

  clock_tick = 0;
}
void loop() // effort at ramping up channel 1

{

  delay(rampDelay);
  curBrightness1 -= rampStep;

  if (curBrightness1 < maxBrightness1)
  {
    curBrightness1 = maxBrightness1;
  }
  Serial.print("CH1 brightness = ");
  Serial.println(curBrightness1);

}
[/code]

I am sorry - I find it difficult to think clearly about this. It's possible the problem is with the Serial.print() . I have been running the sketches with two of the actual shelves from my pantry (they have lamps on underside) connected to the channel1 and channel2 outputs from the Krida dimmer. Maybe that the code you suggested needs different in order to feed back into the digital pins 6 & 7, for the actual output. I am just guessing.

The lamps came on as before: each at the pre-set brightness level for that channel. Only, had this worked, I expect the lamp for channel 1 would have ramped up to the pre-set level over time.

Finally, I attempted to run the sketch you provided - instead of the one I edited - in case somehow there was a difference I could not recognize. Here was another puzzle: the compiler kept telling me that 'timerIsr' was not defined for this scope. This is thoroughly baffling to me. I compared the lines in the void setup() of several past versions of this sketch with the one you provided, and could not see any difference.

There is the change from

[code]
  attachInterrupt(1, zero_crosss_int, RISING);

[/code]

to this

[

code]
  attachInterrupt(digitalPinToInterrupt(3), zero_crosss_int, RISING);
  Timer1.initialize(83); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here

So...

for the same language in the setup loop, two different sketches behave differently when I compile them. The sketch which I edited, which I included complete in this post, that sketch complied. But the sketch which I cut and pasted from the previous forum post - which has the same three lines in the set-up - yields the error message:

'timerIsr' was not declared in this scope.

I will say I feel that perhaps with your help I have got close. Also, I am becoming a bit more adept with this process. However, I am very much out of my depth with this code. Thanks as ever, and please do let me know if you see something obviously wrong.

Without seeing the full sketch and the exact error I can't say why it did not compile. Note that the black output pain usually contains more info than the last few lines that you see. For future:

  1. Click in the output window, select all text (CTRLA)
  2. Copy it (CTRLC)
  3. Paste it in a reply (preferably using code tags).
    IDE 1.8.x also has a button that is visible in the orange bar when errors occur.

I can not say if you made a mistake or not. I've loaded my full sketch from post #7 and it compiles without errors.
I'm not sure how you did the cut-and-paste; the forum provides a small icon that is visible when your mouse is over a code block; clicking that copies the complete code in the code block.

.
If you did it another way, you might have missed something.

So the big question is if it did the job of dimming the one channel?

No it didn't. I ran it several times, and even changed the rampStep to 1, thinking that would perhaps slow down dimming in case it was happening too quickly for me to see.

curBrightness1 -= rampStep;

It should mean that every second, curBrighness1 is decreased by 5. So 95 would become 90, then 85, and so on.

The ` if(curBrightness1 < maxBrightness1)

would become true once the loop had repeated after a few seconds, when the curBrightness1 value had decreased to the target level. Then the

{
    curBrightness1 = maxBrightness1;
  }

would kick in. It seems like it should work. But I just tried it again, and it doesn't ramp up channel_1. Both channels come on, at different maxBrightness levels, but they both come on suddenly and at the same time. The complete code I ran is in post 8. Please let me know if you can see the mistake.

Thanks for the hint to use the icon in the forum window for copying the complete code in the code block. This time the sketch compiled, and I just tested it. To be clear, its the complete sketch from post 7.

Both lights come on to the same brightness level, without any ramping that I could see.

Blockquote

The full sketch below. You can use Serial Monitor to follow the dimming. If it works for one

I have not been using Serial Monitor. I have just been plugging in the lights. It's not possible that this is the problem, is it? I hope this post is a bit more clear and concise. Thanks again for the help.

I suggest that you use serial monitor to see what is happening; having it open or not should not make much of a difference. But at least you have an indication if you get decreasing values for the currBrightness.

Ok. I've sent for one. It should arrive in two days or so, and I will report again after I've tried that.

What about that both channels came on at same brightness level for the sketch I copied from post 7, while the one from post 8 output two different Max brightness values? I can't see the difference in the two sketches.

Will print out both and compare them in good light with a colored marker. They are supposed to be the same. Neither ramps up channel 1, but I will confirm with serial monitor. Only the one I typed using your suggestions puts out two different values. This must be a clue.

I'll be back in two or three days. Thanks as ever!

Please explain; if it refers to serial monitor, that is part of the IDE.

Ok. I didn't know that. Spent a few dollars on a cheap digital display, from Amazon. Software is just never what I think of first!

I'll try using the tool in the IDE as you suggest.

Tools -> Serial Monitor in the IDE menu.

I feel kinda foolish. Now I can see that channel 1 is in fact ramping up.

The Serial Monitor let me see what was happening. Actually, a maxBrightness value 80 does not turn on the led. I was looking at channel 2, which did not have any dimming in the void loop().

After quite a bit of experimentation, I think adjusting the rampDelay to 100 and the rampStep1 yields a pretty good ramp-up. I do wish the led came on when it was dimmer, so that the visual effect was more pronounced.

However, this is just to make the coming on of my pantry lights to be a little more attractive. The main thing was to get the dimmer to work (since it looked like a tanning booth in there with the parts I bought.) I need to regain some perspective. I have made so many mistakes.

Sterretje, this time I used the code you provided - properly copied from post - and added on a channel 2 ramp up just like the one you wrote for channel 1. This time I had no difficulty compiling the sketch and uploading. When I ran it, I found it again difficult to perceive the effect. Using Serial Monitor there was no doubt that the code is achieving what I asked for.

I would like to thank you for your kind attention to my posts. The soft start on a lighting circuit will never be quite the same to me - it took me so long to get here. If I added a third circuit to complete the effect, and perfected the ramp up, it would be analogous to the trouble that bell makers used to take centuries ago. All for the sound the bell makes. It is incredible.

I'm not quite sure how to interpret this. Does it not go fast enough in the beginning so you hardly see the effect. Or does it go to fast in the beginning and there is barely a change when getting further in the ramping?

I adjusted the delay, hoping to understand the results. When delay is small (25 millisecond) then I can't see effect at all. Just looks like lights come on.

When delay is 600 to 1000 millisecond what I see is the lights going from bright to brighter.

maxBrightness 95 = off.
maxBrightness 80 = still off.
maxBrightness 65 = already pretty bright.

Between 50 and 35 you have to blink so much that it's hard to know if your eyes are playing tricks.

I have read recently that brightness increases are perceived in a non-linear way. From what I have observed, two factors are combining which subdue the ramp effect.

  1. it really seems like minimum dimming level is too bright

  2. for an optimal effect the ramp would accelerate. This way more of the ramp would show dimmer light.

There is another library called 'tricDimmer' which I played with before settling on the example from the 'timerone' library. Perhaps I will make an effort to apply what I've learned, with it.

As I recall, the basic triacDimmer example has superior low level performance. I could not solve how to stop it from forever fading on and then off.

I am proceeding with my pantry and the current sketch. My family has been very patient with me and this project. But I think I can still play with another example sketch in parallel. I will certainly post again in case I meet with some success.

Did you ever post the code? Only thing I remember to have seen is something with the HC05 :wink:

My experience with normal leds is that the lowest step is already pretty bright and at the bright end you hardly see a difference if you increase the brightness.

If you want to speed up, you can use something like

void loop()
{
  delay(rampDelay);
  rampDelay /= 2;

  other existing code
  ...
  ...

Play with rampDelay and the divisor.

If you consider the topic solved, you can click the solution button under the most useful post to let others that might look for a solution know that a solution was provided.