That sketch is playing a tune making square waves of different frequencies. That's not really the same as PWM, which varies the length of the pulse, but the frequency stays constant. There are a few ways to generate a square wave of a given freq. in Pd, if that is your goal. If you want to do it by modeling the Arduino's use of a timer to turn a signal on and off, that might be an interesting exercise. One easier way, though, would be to amplify and clip a sine wave.

[osc~ 440]
|
[*~ 50]
|
[clip~ -0.5 0.5]

then if you plug a 50 cent piezo buzzer into the sound card of your $1000 laptop, it will sound just like a $3 avr!

Then you could store the note durations and frequencies in 2 separate arrays, like in the Arduino sketch, where the freq is sent to the [osc~] and the duration is sent to a [delay] that sends a bang to a counter that increments to the next array elements. Then it will play "Twinkle, twinkle," and what could be better than that!

Collin



Message: 6
Date: Sat, 07 Nov 2009 22:59:36 +0100
From: altern <altern2@gmail.com>
Subject: [PD] arduino melody example in PD?
To: the PureData - mailinglist <pd-list@iem.at>
Message-ID: <4AF5EDC8.6040407@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

hi

i have been trying to port this example to PD

http://arduino.cc/en/Tutorial/Melody

it just uses a piezo to buzz a song. i dont understand very well this
PWM controls and also I am finding quite difficult to translate the
arduino code to PD. So after some headache i thought maybe someone has
already done this for PD?

thanks

enrike