シリアル通信でボタンの状態を確認する。

Dependencies:   mbed

Committer:
hagi_hara
Date:
Thu Aug 27 13:32:40 2015 +0000
Revision:
1:b16e040b047b
Parent:
0:95b3a426f559
Child:
2:8c96a6f1fd6b
????

Who changed what in which revision?

UserRevisionLine numberNew 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 1:b16e040b047b 18 pc.printf("Hello World!\r\n"); //シリアル通信でマシンにHello World!(改行)を送る。
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 1:b16e040b047b 26 pc.printf("Button:ON\r\n");
hagi_hara 0:95b3a426f559 27 } else {
hagi_hara 0:95b3a426f559 28 //そうでなければ音を止める
hagi_hara 0:95b3a426f559 29 speaker.write(SP_OFF);
hagi_hara 1:b16e040b047b 30 pc.printf("Button:OFF\r\n");
hagi_hara 0:95b3a426f559 31 }
hagi_hara 0:95b3a426f559 32 wait(0.1);
hagi_hara 0:95b3a426f559 33 }
hagi_hara 0:95b3a426f559 34 }