前幾篇是由電腦傳訊號到UNO板,再回傳到電腦,
本篇是介紹由按鍵或其他感應開關等觸發訊號回傳到電腦,
準備硬體
開關
10K或1k電阻
↓打開內建範例,,檔案>範例>Basics>DigitalReadSerial

會跳出另一個視窗↓

int pushButton = 2;
// the setup routine runs once when
you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over
again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in
between reads for stability
}搭配好硬體↓

上傳後打開監控視窗,會一直顯示0,
表示Pin2腳位是斷開的,當按下按鈕時閉通,則會回傳1,
若是2腳開關接一正一負,3腳開關則一正一電阻一負,
4腳開關可先使用電錶確認要接的是常開腳,
因為只是要觸發訊號,所以加了電阻作保護,
↓再打開另一個內建範例,,檔案>範例>Digital>DigitalInputPullup

一要會跳出另一個視窗,一樣上傳後打開監控視窗,
按下開關後可控制內建的LED燈,並回傳訊息,
www.ee543.com
沒有留言:
張貼留言