double frequency = F_CPU / (sum / count);
That's not the greatest piece of code. You may want to change it.