AC light dimmer using Attiny13

Fundamentally, If I am able to control the ON time after every zero-cross of the AC signal, I should be able to achieve dimming effect.

Therefore In the below code I am trying for a 5ms Delay after zero cross detection to turn on the load.

volatile boolean flag = false;
void setup() {
pinMode(4, OUTPUT); // initialize pin 4 ( ATtiny leg 3) as an output.
pinMode(0, INPUT);
pinMode(2, INPUT); 
attachInterrupt(0,blink, RISING);
GIMSK = 0x40;
PCMSK = 0;
digitalWrite(4, LOW);
void loop() {
  static unsigned long StopMillis;

  if ( flag == true ) {
    digitalWrite(4, HIGH);
    StopMillis = millis() + 5;
    while ( millis() < StopMillis );   // Hang on for 5 milliseconds
    digitalWrite(4, LOW);
    flag = false;
 void blink()
 flag = true;  

I notice a flicker in the light therefore, when I saw the waveform on output (in blue) and input to INT0 (yellow), I noticed the code is not working as expected.

I have used the 1.2MHz internal oscillator, changed it to 9.6MHz but behavior stays the same.

Maybee this link could help you out.

it helpt me a lot building my own dimmer.