Playing PWM music //Borrowed from Rob Toulson and Tim Wilmshurst: ARM mbed Course Material - PWM //Link: https://developer.mbed.org/cookbook/Course-Notes

Dependencies:   mbed

Playing PWM music

Borrowed from Rob Toulson and Tim Wilmshurst: ARM mbed Course Material - PWM

Link: https://developer.mbed.org/cookbook/Course-Notes

Committer:
cspista
Date:
Wed Oct 13 13:25:13 2021 +0000
Revision:
0:c805c9f396c1
Final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cspista 0:c805c9f396c1 1 //Borrowed from Rob Toulson and Tim Wilmshurst: ARM mbed Course Material - PWM
cspista 0:c805c9f396c1 2 //Link: https://developer.mbed.org/cookbook/Course-Notes
cspista 0:c805c9f396c1 3 #include "mbed.h"
cspista 0:c805c9f396c1 4
cspista 0:c805c9f396c1 5 PwmOut buzzer(D3);
cspista 0:c805c9f396c1 6 float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440}; //frequency array
cspista 0:c805c9f396c1 7 float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2}; //beat array
cspista 0:c805c9f396c1 8
cspista 0:c805c9f396c1 9 int main() {
cspista 0:c805c9f396c1 10 while (1) {
cspista 0:c805c9f396c1 11 for (int i=0; i<12; i++) {
cspista 0:c805c9f396c1 12 buzzer.period(1/(frequency[i])); // set PWM period
cspista 0:c805c9f396c1 13 buzzer=0.5; // set duty cycle
cspista 0:c805c9f396c1 14 wait(0.5*beat[i]); // hold for beat period
cspista 0:c805c9f396c1 15 }
cspista 0:c805c9f396c1 16 }
cspista 0:c805c9f396c1 17 }