Arduino Multiple AC outlet control ..

I was using this blog to control AC signal with Arduino .
[I'll give some time for you guys to read and understand what this guy was doing]

Now I'm stuck as I want Arduino to control separately at-least 5 outlets at the same time . By control I mean different behaviors. Say one is blinking and another is stagnant and another is blinking slowly.
Now knowing all the limitations of Interrupt service routines.

My current condition:
I’ve 5 such circuits.
All of them are connected to an interrupt pin of Arduino Leonardo to detect zero crossing.
All of them are connected to a Digital Pin of Arduino Leonardo to control the Triac.
Have 5 separate service routines with 5 variables which I can control in loop (without using delay — just millis () to do some blinking and other stuff so that they can run parallely).

The intent is to control 5 such circuits(i.e run 5 service routines) at the same time with one Arduino, so that I can have desired effects for each Load(Bulb.)..

Trying with 3 AC LOADS, this is my code:

Also
I tried using only one ISR(which is kind of a pseudo PWM) and controlling other outputs within that function. But that way I’ve only one variable and all the 5 bulbs have same behaviour. If I use just one Interrupt Pin and one ISR then Can I control different outputs at the same same time differently? Within ISR I can not do Arduino multitasking as it doesn't allow

millis();

and

micros();

If this can be done, then we don’t need to run separate ISRs in parallel(which I’m not sure we can).
This is what I was doing

int AC_LOAD = 10;    // Output to Opto Triac pin
int AC_LOAD_TWO = 6;
int AC_LOAD_THREE = 4;
int AC_LOAD_FOUR = 5;
int AC_LOAD_FIVE = 8;

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

void setup()
{
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  pinMode(AC_LOAD_TWO, OUTPUT);
  pinMode(AC_LOAD_THREE, OUTPUT);
  pinMode(AC_LOAD_FOUR, OUTPUT);
  pinMode(AC_LOAD_FIVE, OUTPUT);

  attachInterrupt(4, 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
{
  int dimtime = (65 * dimming); // For 60Hz =>65
  delayMicroseconds(dimtime);    // Wait till firing the TRIAC
  digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  digitalWrite(AC_LOAD_TWO, HIGH);
  digitalWrite(AC_LOAD_THREE, HIGH);
  digitalWrite(AC_LOAD_FOUR, HIGH);
  digitalWrite(AC_LOAD_FIVE, HIGH);
  delayMicroseconds(8.33);       // triac On propogation delay (for 60Hz use 8.33)
  digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
  digitalWrite(AC_LOAD_TWO, LOW);
  digitalWrite(AC_LOAD_THREE, LOW);
  digitalWrite(AC_LOAD_FOUR, LOW);
  digitalWrite(AC_LOAD_FIVE, LOW);
}


void loop()  {
  dimming = 50;
  delay(1000);
  dimming = 128;
  delay(1000);
}

I know a lot of people don't work with AC and interrupts and all .. This is not a very popular problem.. But I'm stuck so helps are heart-fully appreciated..

You only need to detect zero crossing once - its the same mains to all the loads. Use one interrupt only
and drive all the loads from it. You should be using arrays for all your variables so that the same code
can handle any of them.

Thanks I'll take a look ..

MarkT:
You only need to detect zero crossing once - its the same mains to all the loads. Use one interrupt only
and drive all the loads from it. You should be using arrays for all your variables so that the same code
can handle any of them.

but would this do parallel multi load control .. ?

I agree with @MarkT

MarkT:
You only need to detect zero crossing once - its the same mains to all the loads. Use one interrupt only
and drive all the loads from it. You should be using arrays for all your variables so that the same code
can handle any of them.

you only detect zero-crossing once, then in a single ISR (which is not some sort of PWM but an Interrupt Service Routine which can be triggered by either a hardware interrupt or a Timer).

as far as I understand without having built the circuit (don't have any opto-coupler or Triac in my stock), you only need to detect the zero-crossing point (in the second example he detects the rising edge) for safety reasons.
I remember watching some video about it probably on AfrotechMods or EEVBlog where they talked about the issues you could have with the negative part of the sinus when plugging in.

once you detect the safe point you can control the on/off state of the Triac and thus modulate that in a productive/safe way.

I would not use delays anywhere except in the ISR (not really sure, would need to put the thing together).

I'd use a single method in lieu of an animation stepper, and keep animation data in some kind of structure (time start, time end, current frame).
There's also some easing library for Arduino, I found this but not sure it's the one I looked into a while back

u.

I would also suggest to take a look at these two videos, since I see the tutorial you mention uses 2 resistors on the mains, and it seems that a Capacitor or an Inductor may help make this safer :slight_smile:

Thanks to

ubidefeo:
I agree with @MarkT
you only detect zero-crossing once, then in a single ISR (which is not some sort of PWM but an Interrupt Service Routine which can be triggered by either a hardware interrupt or a Timer).

as far as I understand without having built the circuit (don't have any opto-coupler or Triac in my stock), you only need to detect the zero-crossing point (in the second example he detects the rising edge) for safety reasons.
I remember watching some video about it probably on AfrotechMods or EEVBlog where they talked about the issues you could have with the negative part of the sinus when plugging in.

once you detect the safe point you can control the on/off state of the Triac and thus modulate that in a productive/safe way.

I would not use delays anywhere except in the ISR (not really sure, would need to put the thing together).

I'd use a single method in lieu of an animation stepper, and keep animation data in some kind of structure (time start, time end, current frame).
There's also some easing library for Arduino, I found this but not sure it's the one I looked into a while back
Animation on the Arduino with easing functions | Andys Workshop

u.

I solved it with some guidance by you and from the main blog i was following :

Here's the code of my intent that i wrote: http://pastebin.com/raw/KeNynpkL