My mate Tom made a little Arduino piano which could play one note at a time. He asked me to calculate the frequencies of the notes for him since I am both a pianist and a physicist. I duly complied. He also challenged me to make a polyphonic keyboard so that one could press multiple notes and generate multiple tones on the speaker.

I did this by computing a composite square wave which consists of multiple pulse trains superimposed on each other. Each individual pulse train has it's own parameters (period and duty cycle) . The code keeps track of how many loops it's gone around and decides whether each pulse should be high or low at the present loop. If the button corresponding to that note is depressed and the pulse should HIGH then the output is set to HIGH otherwise it is set to LOW.

Very simple and quite effective.