Probably something stupid, but I can't figure out how to work with these large numbers, then cast the result (which will be small) to an integer. I keep getting negative numbers, which I think means I'm overflowing somewhere.
int freq = 440;
int len = 256;
long timer = 2000000.0 / (freq * len);
int roundedTimer = int(timer);
I should get 18, but I'm getting -108...
freq * len
needs to be explicitly a long too: long timer = 2000000L / ((long) freq * len);