Issue with gate pulses for PWM Rectifier

I am designing a PWM rectifier with IGBT switches ,here i am using Zero crossing detector for synchronization purpose for firing pulses . I have written a code to generate 5 pulses in one half cycle (50 Hz input Frequency) During testing for one switch i have have found some drift in gating pulse .I mean sometime there is 5 pulses inside the first half cycle (10 millisecond) and sometime there is 3 pulses , there is movement is gating pulses which is not desirable .

int flag1=1;
int flag2=1;
int ZCD = 2;// Input from zcd
void setup() {
pinMode(ZCD, INPUT); 
pinMode(12, OUTPUT); 
pinMode(9, OUTPUT); 
//Serial.begin(9600);
}
// the loop function runs over and over again forever 
void loop() { 

int sinewave = digitalRead(ZCD); // ZCD output

if (sinewave==1 && flag1==1)
 {
 digitalWrite(12, LOW);
 
for(int i =0;i<5;i++)
{
digitalWrite(9,HIGH);
delay(1);
digitalWrite(9,LOW);
delay(1);

}

flag1=0;
flag2=1;
} 
else if (sinewave==0 && flag2==1) 
{ 
digitalWrite(9, LOW);
for(int i =0;i<5;i++)
{
digitalWrite(12,HIGH);
delay(1);
digitalWrite(12,LOW);
delay(1);
}
flag2=0;
flag1=1; 
} 
else
{ 
digitalWrite(9, LOW); 
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
 }
}

Q1) What is the delay from the actual AC Zero Cross and when the digitalRead(ZCD) function returns a 1?

Q2) Does Delay(1) always delay 1 millisecond?

Q3) What other interrupts are running that can cause JITTER?

Q4) How can you rewrite your code to be deterministic.

After detecting the AC Zero Cross,
the code may process for longer than 10 milliseconds
before checking for the next AC Zero Cross.
Is that acceptable?

here i am using Zero crossing detector for synchronization purpose for firing pulses

And you are polling it. If you care about EXACTLY when the zero crossing happens, you'll have to use interrupts.

First of all thanks for your replies .I have written a new code where i am using interrupts but still in this code i am facing the same problem.Even this time i am trying with four pulses to avoid the timing problem with ZCD (which is 10 ms for one half)

volatile int a=0;
volatile int b=0;
void setup()
{
 // noInterrupts();
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  attachInterrupt(0,ISR_RISING,RISING);
  attachInterrupt(1,ISR_FALLING,FALLING);
  //interrupts();
}
void ISR_RISING()

 {
   a=1;
  
 }
   
   void ISR_FALLING()
 {
   b=1;
  
 }
void loop()
{
  if (a==1)
  { digitalWrite(10,LOW);

    for(int i =0;i<4;i++)
{
digitalWrite(9,HIGH);
delay(1);
digitalWrite(9,LOW);
delay(1);

}
   a=0;
  }
  
if(b==1)
  {digitalWrite(9,LOW);

  for(int i =0;i<4;i++)
{
digitalWrite(10,HIGH);
delay(1);
digitalWrite(10,LOW);
delay(1);

}
   
   b=0;
  }
}

It appears that you are still having problems with fitting your 8 ms pulse routines in 10 ms.

One simple way to speed things up is to Replace the digitalwrite() with bitWrite(). It is significantly faster.

Your routines leave the pins LOW, so there is no reason to have an additional digitalWrite()of the other pin before each pulse set.

I would also make turn your delays into delayMicroseconds(1000) for greater precision.

I will assume you have an Arduino Uno(Nano,Mini) where pin9 and pin 10 are written at PORTB1 and PORTB2
See if this code runs within the 10 ms cycle.

volatile int a=0;
volatile int b=0;
void setup()
{
 // noInterrupts();
  pinMode(9,OUTPUT);//PB1 ATmeg328
  pinMode(10,OUTPUT);//PB2 ATmega328
  attachInterrupt(0,ISR_RISING,RISING);
  attachInterrupt(1,ISR_FALLING,FALLING);
  //interrupts();
}
void ISR_RISING()

 {
   a=1;
  
 }
   
   void ISR_FALLING()
 {
   b=1;
  
 }
void loop()
{
  if (a==1){//added  deleted brace
    for(int i =0;i<4;i++)
{
bitWrite(PORTB,2,HIGH);
delayMicroseconds(1000);
bitWrite(PORTB,2,LOW);
delayMicroseconds(1000);

}
   a=0;
  }
  
if(b==1){//added  deleted brace
  
  for(int i =0;i<4;i++)
{
bitWrite(PORTB,3,HIGH);
delayMicroseconds(1000);
bitWrite(PORTB,3,LOW);
delayMicroseconds(1000);

}
   
   b=0;
  }
}

Edit: I inadvertanly chopped some brackets when I took out the first digitalWrite() preset within the conditional tests. I haved added them back in.

durgesh11oct:
First of all thanks for your replies .I have written a new code where i am using interrupts but still in this code i am facing the same problem.Even this time i am trying with four pulses to avoid the timing problem with ZCD (which is 10 ms for one half)

volatile int a=0;

volatile int b=0;
void setup()
{
// noInterrupts();
 pinMode(9,OUTPUT);
 pinMode(10,OUTPUT);
 attachInterrupt(0,ISR_RISING,RISING);
 attachInterrupt(1,ISR_FALLING,FALLING);
 //interrupts();
}
void ISR_RISING()

{
  a=1;
 
}
 
  void ISR_FALLING()
{
  b=1;
 
}
void loop()
{
 if (a==1)
 { digitalWrite(10,LOW);

for(int i =0;i<4;i++)
{
digitalWrite(9,HIGH);
delay(1);
digitalWrite(9,LOW);
delay(1);

}
  a=0;
 }
 
if(b==1)
 {digitalWrite(9,LOW);

for(int i =0;i<4;i++)
{
digitalWrite(10,HIGH);
delay(1);
digitalWrite(10,LOW);
delay(1);

}
 
  b=0;
 }
}

I see you are still using DELAY(1)

So I will ask you again ...
Does delay(1) always delay for exactly 1 millisecond?

I think it is very difficult to make Software PWM (what you coded) to be as accurate as Hardware PWM (which is what you desire).

Yes, you used Interrupts but then in the LOOP() function you still POLLED the Interrupt Flag.
Maybe, you need to trigger "Hardware PWM" from inside the Interrupt instead of setting a FLAG?