I needed something to send tones to a piezo on pin 10, with out bit banging (desperately need the cpu cycles for other things). Borrowing from the servo threads, this is what I came up with. I believe this is a different approach that what is currently in the playground.
//pin 10 pwm sound with pitch and volume control
#define piezoPin 10 //pin 9 works too
#define minVol 0
#define maxVol 13 //rough guess, any more uses more power but not much louder
#define minPitch 0x0FFF
#define maxPitch 0x01FF
//add some defines for specific notes and play a song without bitbanging
void setup(){
pinMode(piezoPin,OUTPUT);
TCCR1A = 0x00; // sets timer control bits to PWM Phase and Frequency Correct mode
TCCR1B = 0x12; // sets timer control bits to Prescaler N = 8
analogWrite(piezoPin, 3); //something less than full volume
}
void loop(){
//control the pitch
for(int j=0;j<3;j++){ //control pitch
for(int x = minPitch; x > maxPitch;x-=10){ //
ICR1 = x;
delay(10);
}
}
//control the volume
ICR1 = (minPitch + maxPitch)/2;
for(int j=0;j<3;j++){
for(int x = minVol; x < maxVol;x++){ //
analogWrite(piezoPin, x);
delay(500);
}
}
}