ボタンを押してスピーカーから音を鳴らす
Dependencies: mbed
main.cpp@3:9f7097f89b78, 2015-08-27 (annotated)
- Committer:
- hagi_hara
- Date:
- Thu Aug 27 12:58:06 2015 +0000
- Revision:
- 3:9f7097f89b78
- Parent:
- 2:8fb2cbbe8c0f
???????
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:9f7097f89b78 | 17 | //もしButtonが押されていたら(Btuttonからの入力が0Vであれば) |
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 | 3:9f7097f89b78 | 22 | } else { |
hagi_hara | 3:9f7097f89b78 | 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 | } |