ボタンを押してスピーカーから音を鳴らす

Dependencies:   mbed

Committer:
hagi_hara
Date:
Sat Aug 15 08:18:47 2015 +0000
Revision:
0:bc2ad5b63ea7
Child:
1:27e47a17243e
?????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hagi_hara 0:bc2ad5b63ea7 1 //example_02_button
hagi_hara 0:bc2ad5b63ea7 2 //スピーカーを鳴らすサンプルコード
hagi_hara 0:bc2ad5b63ea7 3 #include "mbed.h"
hagi_hara 0:bc2ad5b63ea7 4
hagi_hara 0:bc2ad5b63ea7 5 #define SP_OFF 0.0
hagi_hara 0:bc2ad5b63ea7 6 #define SP_ON 0.5
hagi_hara 0:bc2ad5b63ea7 7
hagi_hara 0:bc2ad5b63ea7 8 #define mC 261.626 //「ド」の周波数(Hz)
hagi_hara 0:bc2ad5b63ea7 9
hagi_hara 0:bc2ad5b63ea7 10 PwmOut speaker(dp1);//dp1ピンを出力ピンに設定
hagi_hara 0:bc2ad5b63ea7 11 DigitalIn button(dp14);//dp14ピンをデジタル入力ピンに設定
hagi_hara 0:bc2ad5b63ea7 12
hagi_hara 0:bc2ad5b63ea7 13 int main()
hagi_hara 0:bc2ad5b63ea7 14 {
hagi_hara 0:bc2ad5b63ea7 15 while(true) {
hagi_hara 0:bc2ad5b63ea7 16 //もしButtonが押されていたら
hagi_hara 0:bc2ad5b63ea7 17 if(button == true) {
hagi_hara 0:bc2ad5b63ea7 18 //音を鳴らす
hagi_hara 0:bc2ad5b63ea7 19 speaker.period(1.0/mC);
hagi_hara 0:bc2ad5b63ea7 20 speaker.write(SP_ON);
hagi_hara 0:bc2ad5b63ea7 21 } else {
hagi_hara 0:bc2ad5b63ea7 22 //音を止める
hagi_hara 0:bc2ad5b63ea7 23 speaker.write(SP_OFF);
hagi_hara 0:bc2ad5b63ea7 24 }
hagi_hara 0:bc2ad5b63ea7 25 wait(0.1);
hagi_hara 0:bc2ad5b63ea7 26 }
hagi_hara 0:bc2ad5b63ea7 27 }