AC Dimmer time control

Hello Forum,

I try to make a program to control my AC Dimmer in time.
I already tried to use the Timealarms libary.
But some how it doesn’t work.

/*
 * TimeAlarmExample.pde
 *
 * This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
 * and simulates turning lights on at night and off in the morning
 * A weekly timer is set for Saturdays at 8:30:30
 *
 * A timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 *
 * At startup the time is set to Jan 1 2011  8:29 am
 */

// Questions?  Ask them here:
// http://forum.arduino.cc/index.php?topic=66054.0

#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor

  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day
  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday

  // create timers, to trigger relative to when they're created
  Alarm.timerRepeat(15, Repeats);           // timer for every 15 seconds
  id = Alarm.timerRepeat(2, Repeats2);      // timer for every 2 seconds
  Alarm.timerOnce(10, OnceOnly);            // called once after 10 seconds
}

void loop() {
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm() {
  Serial.println("Alarm: - turn lights off");
}

void EveningAlarm() {
  Serial.println("Alarm: - turn lights on");
}

void WeeklyAlarm() {
  Serial.println("Alarm: - its Monday Morning");
}

void ExplicitAlarm() {
  Serial.println("Alarm: - this triggers only at the given date and time");
}

void Repeats() {
  Serial.println("15 second timer");
}

void Repeats2() {
  Serial.println("2 second timer");
}

void OnceOnly() {
  Serial.println("This timer only triggers once, stop the 2 second timer");
  // use Alarm.free() to disable a timer and recycle its memory.
  Alarm.free(id);
  // optional, but safest to "forget" the ID after memory recycled
  id = dtINVALID_ALARM_ID;
  // you can also use Alarm.disable() to turn the timer off, but keep
  // it in memory, to turn back on later with Alarm.enable().
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Example:
18:00 hours 100% both outputs.
21:00 hours 75% output 1 and 25% ouput 2.

/*

AC Light Dimmer
AC Voltage dimmer with Zero cross detection

Attach the Zero cross pin of the module to Arduino External Interrupt pin
Select the correct Interrupt # from the below table

Pin | Interrrupt # | Arduino Platform
---------------------------------------
2 | 0 | All
3 | 1 | All
18 | 5 | Arduino Mega Only
19 | 4 | Arduino Mega Only
20 | 3 | Arduino Mega Only
21 | 2 | Arduino Mega Only

*/

int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup()
{
pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output
attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime1 = (75*dimming1);
int dimtime2 = (75*dimming2);

if ( dimtime1 > dimtime2)
   { delayMicroseconds(dimtime2);
     digitalWrite(AC_LOAD2, HIGH);
     delayMicroseconds(10);
     digitalWrite(AC_LOAD2, LOW);

     delayMicroseconds( dimtime1 - dimtime2);
     digitalWrite(AC_LOAD1, HIGH);
     delayMicroseconds(10);
     digitalWrite(AC_LOAD1, LOW);
    }
    else
    { delayMicroseconds(dimtime1);
      digitalWrite(AC_LOAD1, HIGH);
      delayMicroseconds(10);
      digitalWrite(AC_LOAD1, LOW);

      delayMicroseconds( dimtime2 - dimtime1);
      digitalWrite(AC_LOAD2, HIGH);
      delayMicroseconds(10);
      digitalWrite(AC_LOAD2, LOW);
    }



}
void loop()
{

dimming1 = 128; // Min Setting 124
dimming2 = 128;
delay(1000);


}

Best Regards,

Henk,

You have two separate sketches. Do they both work separately?

In your dimmer sketch, what happens if dimtime2 is greater than dimtime1?

      delayMicroseconds( dimtime1 - dimtime2);

Hello John,

The two sketces separately works fine. In the Dimmer code i can simple add code voor PIR or LDR, works fine. But i do not know how to code to get it time switch.

When i switch with the dimtimes every works fine. Below i used "SchSet" to switch with values.

  //Schakelaar.  
if (SchSet == HIGH)  
  {      
   Carport = 110;
   Voordeur = 10;
   //Console.println ("Buiten_Full_Hand");  
  }


  else
  {
    Carport = 10;
    Voordeur = 110;
    //Console.println ("Off");

The dimmer code for the two outputs looks correct.

But i do not know how to code to get it time switch.

Set the time alarms for when want to change the dimming values. The alarm function should set the values. In your time alarms example, morning alarm would set dimming1 and dimming2 instead of the serial print statment.

Can you write down a table of the times and dimming values you want?

Hello again,
Its going futher.
Its the complete code with the switch modes.
I want to use “TimerSet1” voor switch on and off in the evening.
Who ha an idea?

Regards Henk,

// Ingangen en uitgangen benoemen.

// LDR.
int Ldr = A0;     //ingang van ldr=a0.
int LdrVal = 0;   //startwaarde van ldr=0.

//Schakelaar.
int Sch = A1;     //ingang van schakelaar=a1.
int SchVal = 0;   //startwaarde van sch=0.

//Pir.
int Pir = A2;      //ingang van pir=4.
int PirVal = 0;   //startwaarde van pir=0.

// Variabelen benoemen.

int LdrSet= LOW;
int SchSet= LOW;
int PirSet= LOW;
int Count= LOW;
int TimerSet1 = HIGH;
int TimerSet2 = LOW;
//int TimerSet3 = HIGH;
int Counter= 30;

int AC_LOAD1 =  10;   // Output to Opto Triac pin
int AC_LOAD2 =  11;   // Output to Opto Triac pin
int Carport = 128;   // Dimming1 level (0-128) 0 = ON, 128 = OFF
int Voordeur = 128;   // Dimming2 level (0-128) 0 = ON, 128 = OFF



//-------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600); 


   
  pinMode(AC_LOAD1, OUTPUT);
  pinMode(AC_LOAD2, OUTPUT);

  
  

  attachInterrupt(0, zero_crosss_int, RISING);
}
//-------------------------------------------------------------------------
void zero_crosss_int()
{  
   int dimtime1 = (75 * Voordeur);  // (10000us - 10us) / 128 = 75 (Approx)
   int dimtime2 = (75 * Carport);  // (10000us - 10us) / 128 = 75 (Approx)
    
   if ( dimtime1 > dimtime2)
   { delayMicroseconds(dimtime2);
     digitalWrite(AC_LOAD2, HIGH);
     delayMicroseconds(10);
     digitalWrite(AC_LOAD2, LOW);
 
     delayMicroseconds( dimtime1 - dimtime2);
     digitalWrite(AC_LOAD1, HIGH);
     delayMicroseconds(10);
     digitalWrite(AC_LOAD1, LOW);
    }
    else
    { delayMicroseconds(dimtime1);
      digitalWrite(AC_LOAD1, HIGH);
      delayMicroseconds(10);
      digitalWrite(AC_LOAD1, LOW);
 
      delayMicroseconds( dimtime2 - dimtime1);
      digitalWrite(AC_LOAD2, HIGH);
      delayMicroseconds(10);
      digitalWrite(AC_LOAD2, LOW);
    }
  
  
}

//-------------------------------------------------------------------------
void loop()
{
  LdrVal=analogRead (Ldr);
  PirVal=analogRead (Pir);
  SchVal=analogRead (Sch);
  
  
     
  if (LdrVal < 30) {LdrSet = HIGH;} 
  if (LdrVal > 50) {LdrSet = LOW;} 

  if (PirVal > 400) {PirSet = HIGH;} 
  if (PirVal < 200) {PirSet = LOW;} 
  
  if (SchVal <500) {SchSet = HIGH;} 
  if (SchVal >800) {SchSet = LOW;} 
  
  if (Counter < 1) {TimerSet2 = LOW;} 
  
  //Schakelaar.  
if (SchSet == HIGH)  
  {      
   Carport = 0;
   Voordeur = 0;
   //Console.println ("Buiten_Full_Hand");  
  }

//Donker en Beweging
else if (PirSet == HIGH && LdrSet == HIGH )  
  {      
    Carport = 0;
    Voordeur = 0;
    Counter = 30;
    TimerSet2 = HIGH;
    //Console.println ("FullLight");  
  }

//Donker en Beweging en Deur full
 else if (PirSet == LOW && LdrSet == HIGH && TimerSet1 == HIGH && TimerSet2 == HIGH)  
  {      
    Carport = 120;
    Voordeur = 0;
    Counter--;
    //Console.println ("Doorlight");  
  }    

else if (PirSet == LOW && LdrSet == HIGH && TimerSet1 == HIGH && TimerSet2 == LOW)
  { 
    Carport = 128;
    Voordeur = 128;
    //Console.println ("LowLight"); 
  }
  
  else
  {
    Carport = 128;
    Voordeur = 128;
    //Console.println ("Off"); 
  }
  delay(100);
}//loop

I want to use "TimerSet1" voor switch on and off in the evening.

TimerSet1 is never changed from the initial value of HIGH and all your compound conditional test look for that HIGH value.

How will you detect evening? When do you want to set the value to LOW?

What do you want to do when the value of TimerSet1 is HIGH or LOW?

Your requirements unclear to me.

I want to use TimerSet1 as condition from the clock.
The clock switch to on in the evening lets say 18:00 hours en switch Off on 23:00 hours.
I already tried to use <TimeAlarms.h> but its not working, maybe i made a programming fault, but i can’t whats goes wrong.

Regards Henk,

I want to use TimerSet1 as condition from the clock.
The clock switch to on in the evening lets say 18:00 hours en switch Off on 23:00 hours.
I already tried to use <TimeAlarms.h> but its not working, maybe i made a programming fault, but i can’t whats goes wrong.

TimeAlarms sounds like the correct approach. The alarm function called at the desired time should set the value of Timer1. If Timer1 is set to 0 or 1 by the alarm function, you should be able to use it as a condition.

Post you best effort at the code using the time alarms. What did you not understand about reply #3.

The clock switch to on in the evening lets say 18:00 hours en switch Off on 23:00 hours.

Can you write a simple TimeAlarms program which prints “ON” at 18:00 and “OFF” at 23:00? The sketch should be very similar to the library example program with the morning and evening alarms. Try and modify it.

Once you can get the alarms working, its strait forward to set up the dimming values. Instead of printing “ON” and “OFF” you will modify dimming values in the called alarm functions.

If i use the code below nothing is happening.

The timealarm code works fine but if i insert the dimmer code it doesn’t work any more.
I think i make a mistake with logical order.

#include <Console.h>
#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup() {
 
  // Initialize Console and wait for port to open:
  Bridge.begin();
  Console.begin();
  
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8,30,0, LightOn);  // 8:30am every day
  Alarm.alarmRepeat(8,35,0, LightOff);  // 8:35am every day
  

  pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
  pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output
  
  attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void loop() {
  digitalClockDisplay();
  Alarm.delay(100); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void LightOn() {
  dimming1 = 0; //Full = 0
  dimming2 = 0; //Full = 0
}

void LightOff() {
  dimming1 = 128; //Off = 128
  dimming2 = 128; //Off = 128
}

void digitalClockDisplay() {
  // digital clock display of the time
  //Console.print(hour());
  printDigits(minute());
  printDigits(second());
  //Console.println();
}

void printDigits(int digits) {
  //Console.print(":");
  if (digits < 10)
    Console.print('0');
  //Console.print(digits);
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime1 = (75*dimming1);
int dimtime2 = (75*dimming2);

if ( dimtime1 > dimtime2)
   { delayMicroseconds(dimtime2);
     digitalWrite(AC_LOAD2, HIGH);
     delayMicroseconds(10);
     digitalWrite(AC_LOAD2, LOW);

     delayMicroseconds( dimtime1 - dimtime2);
     digitalWrite(AC_LOAD1, HIGH);
     delayMicroseconds(10);
     digitalWrite(AC_LOAD1, LOW);
    }
    else
    { delayMicroseconds(dimtime1);
      digitalWrite(AC_LOAD1, HIGH);
      delayMicroseconds(10);
      digitalWrite(AC_LOAD1, LOW);

      delayMicroseconds( dimtime2 - dimtime1);
      digitalWrite(AC_LOAD2, HIGH);
      delayMicroseconds(10);
      digitalWrite(AC_LOAD2, LOW);
    }

The timealarm code works fine but if i insert the dimmer code it doesn’t work any more.
I think i make a mistake with logical order.

I don’t se anything obviously wrong with the sketch.

I don’t understand the use of Console and Bridge. Are you using a Yun?

Here is a serial print version I used and I can see the alarms working and the dimming values changing.

One suggestion is to make the interrupt variables volatile

volatile int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
volatile int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

Another would be to change the the target dimming values so that they are not the same.

delayMicroseconds() may not work correctly for 0. The function reference talks about a minimum value of 3.

I would also confirm that the hardware setup is still correct and the dimming code can still work stand alone.

#include <TimeLib.h>
#include <TimeAlarms.h>

//AlarmId id;

int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
volatile int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
volatile int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup() {

  // Initialize Serial and wait for port to open:
  // Bridge.begin();
  Serial.begin(115200);

  setTime(8, 29, 30, 1, 1, 11); // set time to Saturday 8:29:30am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8, 30, 0, LightOn); // 8:30am every day
  Alarm.alarmRepeat(8, 33, 30, LightOff); // 8:30;30am every day


  pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
  pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output

  attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void loop() {
  Serial.print("min/sec ");
  digitalClockDisplay();
  Serial.println("dimming values");
  Serial.print(dimming1);
  Serial.print(":");
  Serial.println(dimming2);
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void LightOn() {
  dimming1 = 0; //Full = 0
  dimming2 = 0; //Full = 0
}

void LightOff() {
  dimming1 = 128; //Off = 128
  dimming2 = 128; //Off = 128
}

void digitalClockDisplay() {
  // digital clock display of the time
  //Serial.print(hour());
  printDigits(minute());
  Serial.print(":");
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  //Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
  // (10000us - 10us) / 128 = 75 (Approx)
  int dimtime1 = (75 * dimming1);
  int dimtime2 = (75 * dimming2);

  if ( dimtime1 > dimtime2)
  { delayMicroseconds(dimtime2);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);

    delayMicroseconds( dimtime1 - dimtime2);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);
  }
  else
  { delayMicroseconds(dimtime1);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);

    delayMicroseconds( dimtime2 - dimtime1);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);
  }
}

I am using a yun. Today i used a serial connection. What happend is: When i start the board with the program the clock goes to slow its counts the seconds ones up every 7 seconds. If "LightOn" the light goes "on" and the clock changes every second, so thats oke. If "LightOff" with a value 50 on dimming1 and 2 to clock goes slows down. So i think there is a relation between the clock and the dimmer value.

I am using a yun.

  attachInterrupt(0, zero_crosss_int, RISING); 

Attach the Zero cross pin of the module to Arduino External Interrupt pin
Select the correct Interrupt # from the below table

Pin | Interrrupt # | Arduino Platform
---------------------------------------
2 | 0 | All
3 | 1 | All
18 | 5 | Arduino Mega Only
19 | 4 | Arduino Mega Only
20 | 3 | Arduino Mega Only
21 | 2 | Arduino Mega Only

I'm not familiar with the Yun, but I believe it is 32U4 based, and from this documentation, INT0 on the Yun is pin 3. What pin is used your zero cross interrupt input?

https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

When i start the board with the program the clock goes to slow its counts the seconds ones up every 7 seconds. If "LightOn" the light goes "on" and the clock changes every second, so thats oke. If "LightOff" with a value 50 on dimming1 and 2 to clock goes slows down. So i think there is a relation between the clock and the dimmer value.

Please post your current code.

Previously you said that a dimming value of 128 was OFF, and 0 was on which makes sense for the waveform chopping. How is 50 a value to produce OFF?

Can you please post a circuit diagram of your dimmer hardware.

There are some tests to do to understand the interaction between the clock and the interrupt/dimmer, but I'm not clear about what you have for code and circuitry. If you don't use the interrupt and the dimming delay, can you turn the light on and off by just applying 5v and 0v to the MOC. Does the clock slow down?

I use as board this one.
https://www.hackerstore.nl/Artikel/311
and for 230volt output this.
http://www.inmojo.com/store/krida-electronics/item/4-channel-ac-light-dimmer-arduino--v2/#shipping
The i use is the code you send to my.

#include <TimeLib.h>
#include <TimeAlarms.h>

//AlarmId id;

int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
volatile int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
volatile int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup() {

  // Initialize Serial and wait for port to open:
  // Bridge.begin();
  Serial.begin(115200);

  setTime(8, 29, 30, 1, 1, 11); // set time to Saturday 8:29:30am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8, 30, 0, LightOn); // 8:30am every day
  Alarm.alarmRepeat(8, 33, 30, LightOff); // 8:30;30am every day


  pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
  pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output

  attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void loop() {
  Serial.print("min/sec ");
  digitalClockDisplay();
  Serial.println("dimming values");
  Serial.print(dimming1);
  Serial.print(":");
  Serial.println(dimming2);
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void LightOn() {
  dimming1 = 10; //Full = 0
  dimming2 = 1; //Full = 0
}

void LightOff() {
  dimming1 = 128; //Off = 128
  dimming2 = 120; //Off = 128
}

void digitalClockDisplay() {
  // digital clock display of the time
  //Serial.print(hour());
  printDigits(minute());
  Serial.print(":");
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  //Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
  // (10000us - 10us) / 128 = 75 (Approx)
  int dimtime1 = (75 * dimming1);
  int dimtime2 = (75 * dimming2);

  if ( dimtime1 > dimtime2)
  { delayMicroseconds(dimtime2);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);

    delayMicroseconds( dimtime1 - dimtime2);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);
  }
  else
  { delayMicroseconds(dimtime1);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);

    delayMicroseconds( dimtime2 - dimtime1);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);
  }
}

A little bit misunderstanding, i changed the value “LightOff” from 0 to 50.
To find out how the board works with the value"50" this to see what happens with the time .

Regards Henk,

When i start the board with the program the clock goes to slow its counts the seconds ones up every 7 seconds.

Are you still seeing the interaction of the dimmer with the time?

What happens if you comment out the alarms and the attachInterrupt() statement but digitalWrite() the Triac trigger pins HIGH. The lights should be full on. Is there slowness to the increment of seconds

#include <TimeLib.h>
#include <TimeAlarms.h>

//AlarmId id;

int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
//volatile int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
//volatile int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup() {

  // Initialize Serial and wait for port to open:
  // Bridge.begin();
  Serial.begin(115200);

  setTime(8, 29, 30, 1, 1, 11); // set time to Saturday 8:29:30am Jan 1 2011

  // create the alarms, to trigger at specific times
  //Alarm.alarmRepeat(8, 30, 0, LightOn); // 8:30am every day
  //Alarm.alarmRepeat(8, 33, 30, LightOff); // 8:30;30am every day


  pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
  pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output

  //attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void loop() {
  Serial.print("min/sec ");
  digitalClockDisplay();
  //Serial.println("dimming values");
  //Serial.print(dimming1);
  //Serial.print(":");
  //Serial.println(dimming2);
  digitalWrite(AC_LOAD1, HIGH);
  digitalWrite(AC_LOAD2, HIGH);
  Alarm.delay(1000); // wait one second between clock display
}
/*
// functions to be called when an alarm triggers:
void LightOn() {
  dimming1 = 10; //Full = 0
  dimming2 = 1; //Full = 0
}

void LightOff() {
  dimming1 = 128; //Off = 128
  dimming2 = 120; //Off = 128
}
*/
void digitalClockDisplay() {
  // digital clock display of the time
  //Serial.print(hour());
  printDigits(minute());
  Serial.print(":");
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  //Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
/*
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
  // (10000us - 10us) / 128 = 75 (Approx)
  int dimtime1 = (75 * dimming1);
  int dimtime2 = (75 * dimming2);

  if ( dimtime1 > dimtime2)
  { delayMicroseconds(dimtime2);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);

    delayMicroseconds( dimtime1 - dimtime2);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);
  }
  else
  { delayMicroseconds(dimtime1);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);

    delayMicroseconds( dimtime2 - dimtime1);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);
  }
}
*/

I load the program. There is no slownes in time. The lights are full on.

The TimeAlarms works fine wen i use just on/off.

I load the program. There is no slownes in time.
The lights are full on.
The TimeAlarms works fine wen i use just on/off.

OK. Now you need to start sequentially incorporating the commented out parts of the program to see when the clock fails.

You say that with the alarms enabled but only on/off at the selected time, the clock does not slow,

What I would do next is uncomment the attachInterrupt() so the interrupt is enabled, but do not change the dimming, or trigger the triac in the ISR. This way we can separate out the effect of the interrupt on the sketch from the effect of the actual triac managment.

If the clock slows, then the problem is the interrupt itself and its effect on the processor, if it does not, then begin to bring back the still uncommented pieces of the ISR, and remove the full on digitalWrite() to the triac.

You could start with something like this

#include <TimeLib.h>
#include <TimeAlarms.h>

//AlarmId id;

int AC_LOAD1 = 10; // Output to Opto Triac pin
int AC_LOAD2 = 11; // Output to Opto Triac pin
volatile int dimming1 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
volatile int dimming2 = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup() {

  // Initialize Serial and wait for port to open:
  // Bridge.begin();
  Serial.begin(115200);

  setTime(8, 29, 30, 1, 1, 11); // set time to Saturday 8:29:30am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8, 30, 0, LightOn); // 8:30am every day
  Alarm.alarmRepeat(8, 33, 30, LightOff); // 8:30;30am every day


  pinMode(AC_LOAD1, OUTPUT); // Set the AC Load as output
  pinMode(AC_LOAD2, OUTPUT); // Set the AC Load as output

  attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

void loop() {
  Serial.print("min/sec ");
  digitalClockDisplay();
  Serial.println("dimming values");
  Serial.print(dimming1);
  Serial.print(":");
  Serial.println(dimming2);
  digitalWrite(AC_LOAD1, HIGH);
  digitalWrite(AC_LOAD2, HIGH);
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void LightOn() {
  dimming1 = 10; //Full = 0
  dimming2 = 1; //Full = 0
}

void LightOff() {
  dimming1 = 128; //Off = 128
  dimming2 = 120; //Off = 128
}

void digitalClockDisplay() {
  // digital clock display of the time
  //Serial.print(hour());
  printDigits(minute());
  Serial.print(":");
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  //Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
  // (10000us - 10us) / 128 = 75 (Approx)
  int dimtime1 = (75 * dimming1);
  int dimtime2 = (75 * dimming2);

 /* if ( dimtime1 > dimtime2)
  { delayMicroseconds(dimtime2);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);

    delayMicroseconds( dimtime1 - dimtime2);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);
  }
  else
  { delayMicroseconds(dimtime1);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);

    delayMicroseconds( dimtime2 - dimtime1);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);
  }
  */
}

With this code the timer works oke. After i made the last part of the code active the timer is not working oke anymore.

/* if ( dimtime1 > dimtime2)
  { delayMicroseconds(dimtime2);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);

    delayMicroseconds( dimtime1 - dimtime2);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);
  }
  else
  { delayMicroseconds(dimtime1);
    digitalWrite(AC_LOAD1, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD1, LOW);

    delayMicroseconds( dimtime2 - dimtime1);
    digitalWrite(AC_LOAD2, HIGH);
    delayMicroseconds(10);
    digitalWrite(AC_LOAD2, LOW);
  }
  */

So it looks that

attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above

has influence on the timer.

With this code the timer works oke. After i made the last part of the code active the timer is not working oke anymore.

So it looks that Code: [Select] attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above has influence on the timer.

You are not being clear. Is it the attachInterrupt() and the triggering of the calculations in the ISR which is influencing the timer, or is it the active Triac management part of the ISR with the delayMicroseconds() and digitalWrite() which is influencing the timer?

Sorry,

In Void Setup is writen. attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above

this calls, void zero_crosss_int() // function to be fired at the zero crossing to dim the light.

In this part of the i made this active.

/* if ( dimtime1 > dimtime2) { delayMicroseconds(dimtime2); digitalWrite(AC_LOAD2, HIGH); delayMicroseconds(10); digitalWrite(AC_LOAD2, LOW);

delayMicroseconds( dimtime1 - dimtime2); digitalWrite(AC_LOAD1, HIGH); delayMicroseconds(10); digitalWrite(AC_LOAD1, LOW); } else { delayMicroseconds(dimtime1); digitalWrite(AC_LOAD1, HIGH); delayMicroseconds(10); digitalWrite(AC_LOAD1, LOW);

delayMicroseconds( dimtime2 - dimtime1); digitalWrite(AC_LOAD2, HIGH); delayMicroseconds(10); digitalWrite(AC_LOAD2, LOW); } */

Maybe i do not understand what you mean?

Thank you. It is the delayMicroseconds() part of the interrupt service routine which is interactive with the timer.

While delayMicroseconds() works within the ISR, the entire ISR is taking a long time to finish. Other interrupts are disabled within the ISR, and the Timer0 overflow interrupt which drives the millis count is being blocked.

One thing to do to correct this is to re-enable interrupts within the ISR. This can lead to problems, but in your case, where the ISR completes before the next zero cross interrupt occurs, I think it will be OK.

Add this first line to the ISR

void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
sei(); //re-enable interrupts
//rest of isr

The entire approach to ac phase management using delayMicroseconds() is simple and functional, but indeed can create problems with more than simple sketches. There is an alternative approach using the hardware Timers to control the triac delays which is shown in the ac phase control tutorial. Adapting it for two outputs is even more complex.https://playground.arduino.cc/Main/ACPhaseControl