Code for PWM sine wave generator + some questions.

1.) Every second, there is a noticeable rise in pitch or volume (I can't tell which). I don't know if it's at the beginning or end of the second, but it's probably a problem with my code. Any ideas?

My guess: average falls outside the range 0-255. analogWrite does not validate the value.