Help With Timing Problem For Mosfet AC Dimmer Using NETIO

Hello... i am new to the Arduino and i am having a little trouble with timing and interrupts on the project i am currently working on and i was hoping someone on this forum could help / point me in the right direction as i have been stuck on this for a few days and now i am just re-reading all the sites on google that i have previously read that is close to my problem... but i have not found a solution, so here goes...

I have built a circuit to control an AC bulb with a MOSFET from the Arduino. the circuit has a zero cross detector and will be used to change from leading and trailing edge dimming. the circuit works fine and if i create a loop to increment the dim time then it all works fine and smooth... the problems start when i add the Arduino Yun bridge into the mix which i think is also interrupt based and seems that the values i send in over NETIO are just being lost straight away and it will not operate... i have got it to work nearly correct but i have had to enable interrupts in two places (see code below) but this has its own problems again... when the value is low and thus the dim time is low then it will fade smoothly on the NETIO slider... the problems start when i get past about half way... because of the delayMicro in the zero interrupt section it then takes longer to complete and has less time for void loop... so it is nearly working but i cant seem to put the finishing touches on it and i am a little lost how to proceed from this point.

i will post the code below and hope that someone can steer me in the right direction to get this slider smooth. if you need any more information then please ask and i will provide... Thank You for looking.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server(23);
String cmd;
int AC_LOAD = 11;
int dimVal = 0; // 0-128

void setup()
{
Serial.begin(9600);
pinMode(AC_LOAD, OUTPUT);
attachInterrupt(0, zero_cross_int, RISING);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.noListenOnLocalhost();
server.begin();
}

void zero_cross_int()
{
interrupts();
int dimtime = (75*dimVal);
digitalWrite(AC_LOAD, HIGH);
delayMicroseconds(dimtime);
digitalWrite(AC_LOAD, LOW);
}


void loop()
{
YunClient client = server.accept();
client.setTimeout(0);
  while(client.connected())
  {
    if(client.available())
    {
    interrupts();
    cmd = client.readString();
    client.print(cmd);
    dimVal = cmd.toInt();
    }
  }
}

This subject has been covered at length before

Basically, you should not put calls to delay inside an Interrupt Service Route

This is because you will effectively stop all other processing for virtually the whole time, i.e as you increase delayMicroseconds(dimtime);

ie your code will be constantly in delayMicroseconds(dimtime); and won't have time to do anything else

The best thing to do is to use one of the hardware timers available on the 32U4 (I think Timer2 is available) to time your delay.

so that all that the ISR does is to trigger timer 2 to start for a specific period and then the Timer 2 ISR does the work of turning on the triac

Actually if you want full range 0 to 100% it gets even more complicated, and most implementations just presume the 95% or above = always on, and 5% or below = always off.

If you want full range, from what I recall you need to use 2 hardware timers (I can't remember why, even though I have written and published the code for this, as it must have been 2 years ago that I looked at this stuff)

Hi rogerClark... thank you for your help... i did not have the Yun at that point so i had to wait to get it back... i followed your basic outline guidance and i have now sorted it... thank you very much for steering me in the right direction.

The 32U4 does not have a Timer2 but Timer1 and Timer3 (16-bit timers) was available to me on the Yun... but i will be using 328p's so i will just be using Timer1. i am going with the 5% -> 95% as of right now but as you have mentioned full range i will look into this very soon.

This is a late reply but i would like to say thank you for your help.

Cheers.