CODE: xxxx.bas
'Peizo Speaker demo
D0=16:D1=5:D2=4:D3=0:D4=2:D5=14:D6=12:D7=13:D8=15:D9=3:D10=1
' Use D2 (GPIO 4) for PWM
'The sound data, in format length of note, frequency
Data 12,2637,12, 2637,12, 0,12, 2637,12,0, 12,2093,12, 2637,12, 0,12,3136,12, 0,12, 0,12, 0,12,1568,12, 0,12, 0,12, 0
data 12,2093,12, 0,12, 0,12, 1568,12,0,12, 0,12, 1319,12, 0,12,0,12, 1760,12, 0,12, 1976,12,0,12, 1865,12, 1760,12, 0
data 9,1568,9, 2637,9, 3136,12,3520,12, 0,12, 2794,12, 3136,12,0,12, 2637,12, 0,12, 2093,12,2349,12, 1976,12, 0,12, 0
data 12,2093,12, 0,12, 0,12, 1568,12,0,12, 0,12, 1319,12, 0,12,0,12, 1760,12, 0,12, 1976,12,0,12, 1865,12, 1760,12, 0
data 9,1568,9, 2637,9, 3136,12,3520,12, 0,12, 2794,12, 3136,12,0,12, 2637,12, 0,12, 2093,12,2349,12, 1976,12, 0,12, 0
data 12,262,12, 523,12, 220,12, 440,12,233,12, 466,6, 0,3, 0,12,262,12, 523,12, 220,12, 440,12, 233,12, 466,6 0,3,0
data 12,175,12, 349,12, 147,12, 294,12,156,12, 311,6, 0,3,0,12,175,12, 349,12, 147,12, 294,12,156,12, 311,6, 0
data 6,0,18, 311,18, 277,18, 294,6,277,6, 311,6, 311,6, 208,6,196,6, 277,18,262,18 ,370,18, 349,18, 165,18, 466,18, 440
data 10,415,10, 311,10, 247,10,233,10, 220,10, 208,3,0,3, 0,3,0,12,-1
do
read l,f 'Get length and frequency of note
if f > -1 then ' if a is -1 then thats the last note, ans stop
if f = 0 then
pwm(D2)=0 ' if frequency zero then set output to zero as we cant do a pwmfreq of zero as it may cause a crash.likely to be fixed in later versions.
else
option.pwmfreq f 'set the PWM frequency
pwm(D2) = 100 ' this is volume 0 to 1023 , 1023 being the loudest .I found at about 100 I got a loud sound and going high made little difference, ocer 950 started getting quieter.
end if
end if
pause (15*l) 'pause for 15 milliseconds * l (length of note). Reducing 15 will make it play faster.
loop until f = -1
end