Arduino Uno zero crossing strange behavior

Hi,

I'm currently trying to replace a defect dimmer motherboard by an Arduino Uno.
i've done a very simple program, to start :

int pin=10;


void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, zerocross, RISING);
}
void loop()
  {
  }

void zerocross()
  {
  delayMicroseconds(7000);
  digitalWrite(pin, HIGH);
  delayMicroseconds(8.33);
  digitalWrite(pin, LOW);  
}

I live in France so delay must be between 1/60s (full) and 1/whatever dimming range you want second (60Hz).

the program works fine except that the light blinks ramdomly. What is strange is that I have the same trouble if I replace the arduino by a correctly programmed PC paralell port.

Is there something I don't understand or the electronic part is probably involved ?

Thank you very much,

Best regards

GrAaL

60Hz in france? strange indeed.. all EU has 50Hz.

Also you should brace the intrerrupt routine with nointerrupts() interrupts() statements.
This makes no sense:

delayMicroseconds(8.33)

delayMicroseconds takes an integer as argument, not a floating point value. The floating point value i converted to integer, a waste of time (unless the compiler handles it)

Hi,

I've added noInterrupts() and Interrupts() and change 8.33 to 10 :

 noInterrupts();
   delayMicroseconds(7000);
   digitalWrite(pin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pin, LOW);
   interrupts();

but I have exactly the same issue : dimming seems fine but the lamp flashes 3-5 times ni 10 seconds. As long as I can see, it's completely random.
The PC version using the paralell port was doing the same but I was thinking that it was due to other system paralell tasks.

thank you for your help !

Best regards,

GrAaL

Do not do delays in an interrupt!
If the zero crossing occurs, set a global flag and get out.
Use loop() to toggle the LED.

Hi,

I'ave already try that first in fact but without disabling interrupt during the "if".
It works fine now, thank you very much !

Here is that code if anyone needs it :

int pin=10;
volatile int dimmer_trigger=0;
void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, zerocross, RISING);
}
void loop()
  {
   noInterrupts();
   delayMicroseconds(80);
   dimmer_trigger++;
   if ( dimmer_trigger == 75 )
     {
     digitalWrite(pin, HIGH);
     }
   else
     {
     digitalWrite(pin, LOW);
     }
   interrupts();   

  }

void zerocross()
  {
  dimmer_trigger=0;
  }

Best regards,

GrAaL