tone() and duty cycle

Hello,
I am interested in changing the duty cycle of a sound signal, in order to play with cancelling sound note harmonics. As I see, the tone() function seems to fix duty cycle at 50%. Is there any work around to this? Another library? Or should I generate the signal on my own?

Thanks

In that case, your best option is the timer1 PWM outputs, they are the most flexible ones.

To simplify it's usage, get the TimerOne library. Here you can set frequency (from 0.125 Hz up to 1 MHz) and duty cycle.
Worth mentioning that:

  • Timer1 is bound to only two specific pins (e.g. pins 9 and 10 on ATmega328P-based boards).
  • The frequency affects both outputs, but the duty cycle is independent from each other.
  • You actually set the frequency by its period (1/f), which is given in microseconds.
  • Unlike the usual analogWrite(), the duty cycle is determined by a 10-bit value (0 to 1023, being 511 the 50%).

In fact, since the most popular Arduinos have 3 timers, they can serve as a three-voice square wave sound synthesizer (that is if you don't mind breaking up mills(), micros() and delay()). It's actually more capable from what tone() offers.