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

Dependencies:   mbed

Committer:
hagi_hara
Date:
Sat Aug 22 09:59:21 2015 +0000
Revision:
2:8fb2cbbe8c0f
Parent:
1:27e47a17243e
Child:
3:9f7097f89b78
Change for NUCLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hagi_hara 0:bc2ad5b63ea7 1 //example_02_button
hagi_hara 1:27e47a17243e 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 2:8fb2cbbe8c0f 10 PwmOut speaker(A0);//A0ピンを出力ピンに設定
hagi_hara 2:8fb2cbbe8c0f 11 DigitalIn button(D7);//D7ピンをデジタル入力ピンに設定
hagi_hara 0:bc2ad5b63ea7 12
hagi_hara 0:bc2ad5b63ea7 13 int main()
hagi_hara 0:bc2ad5b63ea7 14 {
hagi_hara 1:27e47a17243e 15 button.mode(PullUp);
hagi_hara 0:bc2ad5b63ea7 16 while(true) {
hagi_hara 0:bc2ad5b63ea7 17 //もしButtonが押されていたら
hagi_hara 1:27e47a17243e 18 if(button == 0) {
hagi_hara 0:bc2ad5b63ea7 19 //音を鳴らす
hagi_hara 0:bc2ad5b63ea7 20 speaker.period(1.0/mC);
hagi_hara 0:bc2ad5b63ea7 21 speaker.write(SP_ON);
hagi_hara 0:bc2ad5b63ea7 22 } else {
hagi_hara 0:bc2ad5b63ea7 23 //音を止める
hagi_hara 0:bc2ad5b63ea7 24 speaker.write(SP_OFF);
hagi_hara 0:bc2ad5b63ea7 25 }
hagi_hara 0:bc2ad5b63ea7 26 wait(0.1);
hagi_hara 0:bc2ad5b63ea7 27 }
hagi_hara 0:bc2ad5b63ea7 28 }