Tone function with variable duty cycle

Hello!
Since i could not find it, i guess there is no such thing but maybe i just dit not see it.

Is there a replacement for the tone() library with a variable duty cycle?

I could find toneAC, toneAC2 and NewTone.
Tone AC2 has no variable duty cycle and NewTone either.

The toneAC library has the desired function but it uses the one of the hardware SPI Pins.

Greetings,
Peter

I think you may be confusing frequency with duty cycle. What function are you trying to generate?

On the blog web page, ToneAC provides a code example of dynamically varying frequency output to a speaker.

If you don't need to do anything else while generating a tone you can simply use the delay(), delayMicroseconds(), or other timing commands, and basically just speed-up the Blink LED example with variable (or different) on and off times.

If you vary the duty cycle on a tone you do not hear anything diffrent about the tone until the duty cycle approaches a very small value. Then the tone tends to sound a bit thin and reed like as the harmonic contents of the wave change.

Hello and thanks for everybodys reply!

borland:
I think you may be confusing frequency with duty cycle. What function are you trying to generate?

No, i would like to change the volume.
The recuction of the duty cyle reduces the output from the >100dB alarm sound to non deafening levels when indoors.
The only other alternative is shifting the frequency away from the resonant point, i guess i will have to stick to that solution.

No, i would like to change the volume.

Changing the duty cycle will not do that.

If you think it is then there is something else going on here that is not normal.

Grumpy_Mike:
Changing the duty cycle will not do that.

If you think it is then there is something else going on here that is not normal.

Well, but it does?
Hooked up directly through currrent limiting resistors to the port pins i see the correct waveform on the scope, no voltage spikes. Both pins toggeled 180° out of phase to each other.

At 50% duty cycle i need earpro, at 10% it is quieter but the tone still has the right pitch.

This is definitely a bare piezo element with a resonant chamber, i bought quite a few of them out of surplus and disassembled some to make vibration sensors out of them.
I would like to use the piezos because i can not get the magnetic buzzer annymore i wanted to use.

Well, but it does?

But it shouldn't, when you listen to a tone on a proper speaker with a proper amplifier it does not.

So what looks like it is happening is that it is some interaction with the piezo element, and you are seeing the response from an impulse, where as a square wave must be damping the piezo element for a period.

This is not a technique you can transfer to any other situation.