Fade a frequency using ToneAC

My objective is to get the Arduino to play a certain frequency then fade out, but I can’t seem to get it to work with the toneAC function. I know my wiring is correct because the tutorial example worked fine going from 150Hz to 15kHz. Any ideas?

Code I’m using:

for (unsigned long freq = 1000; freq <= 0; freq-=1)
{
toneAC(freq);
delay(1);
}
toneAC(0);
}

How do you achieve fading with a 0/5V only signal?
I suppose you could hold the same frequency and make the duty cycle smaller, like fading an LED out.
The timbre of the tone will change as that happens, but it will be quieter.

CrossRoads:
How do you achieve fading with a 0/5V only signal?
I suppose you could hold the same frequency and make the duty cycle smaller, like fading an LED out.
The timbre of the tone will change as that happens, but it will be quieter.

You are correct. I can see now that even if my code did work, it wouldn't be fading 1000Hz it would just be playing every frequency below it until it reaches zero. So then I guess my question is how would I play a frequency then adjust its duty cycle?

I've only done it manually, blink with delay style, changing the amount of on time after some # of cycles.

Alright here is another idea. Since the ToneAC library has a volume function. Would it be possible to play the frequency and then just create a for loop to fade the volume out?

CrossRoads:
I've only done it manually, blink with delay style, changing the amount of on time after some # of cycles.

I've tried to change the volume by changing the duty cycle. It doesn't really change the volume much, but it changes the sound of the wave and it can produce lots of weird overtones.

I made a simple volume control by clicking a number of pins at the same time. Then put different values of resistors on each pin. For example, use pins 3,4,5 and put resistors 150, 300, 600 ohm on each pin and then you have 8 levels of volume by clicking some or all of the pins. A poor man's DAC :slight_smile: