How to sync PWM signal using zero crossing detection

I am trying to sync my sine PWM (50Hz) to a zero cross detector signal. So basically I want my PWM generation to restart once I got the signal from the ZCD, right? I can not seem to manage that (I tried more than just setting TCNT1 = 0). The frequency of the PWM is almost correct so it just keeps shifting very slowly.

You can see on the attached photos the PWM signal (of one pin), the signal on which to lock onto and the ZCD signal.

But I am not great with Arduino code and especially with using the timers and PWM coding.

int i=0;
int x=0;
int OK=0;
int val = 0;
int test = 0;
int analogPin = 2; 
int sinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,

void setup() {

pinMode(5, OUTPUT);

cli();// stop interrupts
TCCR0A=0;//reset the value
TCCR0B=0;//reset the value
TCNT0=0;//reset the value
//0b allow me to write bits in binary
TCCR0A=0b10100001;//phase correct pwm mode
TCCR0B=0b00000001; //no prescaler
TCCR1A=0;//reset the value
TCCR1B=0;//reset the value
TCNT1=0;//reset the value
OCR1A=507;// compare match value
TCCR1B=0b00001001; //WGM12 bit is 1 and no prescaler

TIMSK1 |=(1 << OCIE1A);

sei();// enable interrupts
ISR(TIMER1_COMPA_vect){// interrupt when timer 1 match with OCR1A value
if(i>313 && OK==0){// final value from vector for pin 6
i=0;// go to first value of vector
OK=1;//enable pin 5
if(i>313 && OK==1){// final value from vector for pin 5
i=0;//go to firs value of vector
OK=0;//enable pin 6
x=sinPWM[i];// x take the value from vector corresponding to position i(i is zero indexed)
i=i+1;// go to the next position
OCR0B=0;//make pin 5 0
OCR0A=x;//enable pin 6 to corresponding duty cycle
OCR0A=0;//make pin 6 0
OCR0B=x;//enable pin 5 to corresponding duty cycle

void loop() {
  TCNT1 = 0;

See my reply on your duplicate topic

Duplicate removed.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.

Posting tips.

  • Your OS and version can be valuable information, please include it along with extra security you are using.
  • Always list the version of the IDE you are using and the board version if applicable.
  • How to insert an image into your post. ( Thanks Robin2 )
  • Add your sketch where applicable but please use CODE TAGS ( </> )
  • Add a SCHEMATIC were needed even if it is hand drawn
  • Add working links to any specific hardware as needed (NOT links to similar items)
  • Remember that the people trying to help cannot see your problem so give as much information as you can

I recommend, instead of poling Pin 2, use its interrupt capability and set your sinewave counter back to 0.

volatile int i;  // Must be 'volatile' so the compiler knows that an interrupt might change it at any time.

void setup()
  attachInterrupt(DigitalPinToInterrupt(2), ZeroCross, RISING);

void ZeroCross()
  i = 0;

Note: Since 'i' is longer than one byte you really should turn off interrupts while you fetch a copy of it. If you don't, you will get occasional glitches where you read one byte of 'i' and then the ISR sets 'i' to zero before you fetch the other byte.