Generating 40kHz square wave

One of my sketches on the page below generates 50 KHz:

By changing this line you should be able to get 40 KHz:

const long frequency = 50000L;  // Hz