シリアル通信でボタンの状態を確認する。
Dependencies: mbed
main.cpp@0:95b3a426f559, 2015-08-27 (annotated)
- Committer:
- hagi_hara
- Date:
- Thu Aug 27 13:30:56 2015 +0000
- Revision:
- 0:95b3a426f559
- Child:
- 1:b16e040b047b
??????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hagi_hara | 0:95b3a426f559 | 1 | //example_02_button |
hagi_hara | 0:95b3a426f559 | 2 | //ボタンを押すとスピーカーを鳴らすサンプルコード |
hagi_hara | 0:95b3a426f559 | 3 | #include "mbed.h" |
hagi_hara | 0:95b3a426f559 | 4 | |
hagi_hara | 0:95b3a426f559 | 5 | #define SP_OFF 0.0 |
hagi_hara | 0:95b3a426f559 | 6 | #define SP_ON 0.5 |
hagi_hara | 0:95b3a426f559 | 7 | |
hagi_hara | 0:95b3a426f559 | 8 | #define mC 261.626 //「ド」の周波数(Hz) |
hagi_hara | 0:95b3a426f559 | 9 | |
hagi_hara | 0:95b3a426f559 | 10 | PwmOut speaker(A0);//A0ピンを出力ピンに設定 |
hagi_hara | 0:95b3a426f559 | 11 | DigitalIn button(D7);//D7ピンをデジタル入力ピンに設定 |
hagi_hara | 0:95b3a426f559 | 12 | |
hagi_hara | 0:95b3a426f559 | 13 | //シリアル通信設定 |
hagi_hara | 0:95b3a426f559 | 14 | Serial pc(USBTX, USBRX); |
hagi_hara | 0:95b3a426f559 | 15 | |
hagi_hara | 0:95b3a426f559 | 16 | int main() |
hagi_hara | 0:95b3a426f559 | 17 | { |
hagi_hara | 0:95b3a426f559 | 18 | pc.printf("Hello World!\r\n"); |
hagi_hara | 0:95b3a426f559 | 19 | button.mode(PullUp); |
hagi_hara | 0:95b3a426f559 | 20 | while(true) { |
hagi_hara | 0:95b3a426f559 | 21 | //もしButtonが押されていたら(Btuttonからの入力が0Vであれば) |
hagi_hara | 0:95b3a426f559 | 22 | if(button == 0) { |
hagi_hara | 0:95b3a426f559 | 23 | //音を鳴らす |
hagi_hara | 0:95b3a426f559 | 24 | speaker.period(1.0/mC); |
hagi_hara | 0:95b3a426f559 | 25 | speaker.write(SP_ON); |
hagi_hara | 0:95b3a426f559 | 26 | pc.printf("Button:ON"); |
hagi_hara | 0:95b3a426f559 | 27 | } else { |
hagi_hara | 0:95b3a426f559 | 28 | //そうでなければ音を止める |
hagi_hara | 0:95b3a426f559 | 29 | speaker.write(SP_OFF); |
hagi_hara | 0:95b3a426f559 | 30 | pc.printf("Button:OFF"); |
hagi_hara | 0:95b3a426f559 | 31 | } |
hagi_hara | 0:95b3a426f559 | 32 | wait(0.1); |
hagi_hara | 0:95b3a426f559 | 33 | } |
hagi_hara | 0:95b3a426f559 | 34 | } |