Generating 40kHz square wave

PWM frequency on the Playground:
http://playground.arduino.cc//Code/PwmFrequency

Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).