學習目標 : 認識faya按鍵模組的功能及使用方式
學習時間 : 35min
示範模組 : (1) faya brickNano
(2) faya 按鍵模組
(3) faya 直流馬達模組
工具 : (1) 樂高積木底板 (相容)
====================功能介紹====================
faya按鍵模組使用了五個排列成十字型的按鍵,每個按鍵(PB0~PB4)都對應一輸出埠(PB0~PB4),對應關係如下:
- 當按下PB0時,輸出埠PB0 = HIGH,當放開PB0時,輸出埠PB0 = LOW
- 當按下PB1時,輸出埠PB1 = HIGH,當放開PB1時,輸出埠PB1 = LOW
- 當按下PB2時,輸出埠PB2 = HIGH,當放開PB2時,輸出埠PB2 = LOW
- 當按下PB3時,輸出埠PB3 = HIGH,當放開PB3時,輸出埠PB3 = LOW
- 當按下PB4時,輸出埠PB4 = HIGH,當放開PB4時,輸出埠PB4 = LOW
====================原理知識====================
以下解釋提供給有需要知道背後原理的人:
由電路圖可發現透過簡單的分壓定理,我們可以輕易的獲得預期的輸出訊號,以PB0為例,當按鍵未被按下時,輸出埠接地,因此輸出為LOW,當按下按鍵時,輸出埠PB0的電壓為:
(10k / (1k + 10k)) * Vcc = 0.9Vcc = HIGH
====================範例實作====================
了解模組功能(原理)後,我們用以下範例來展示模組的功能:
目標:
(1)當手指頭按下PB1時,直流馬達吹出低速風
(2)當手指頭按下PB2時,直流馬達吹出中速風
(3)當手指頭按下PB3時,直流馬達吹出高速風
(4)當手指頭按下PB4時,直流馬達停止運轉
接線:
(1) 電源線連接
如下圖所示,連接的說明請看這篇文章 或簡易版
(2) 訊號線連接
Arduino_D3 ===> 按鍵模組_PB1
Arduino_D4 ===> 按鍵模組_PB2
Arduino_D5 ===> 按鍵模組_PB3
Arduino_D12 ===> 按鍵模組_PB4
Arduino_D6 ===> 直流馬達_SIG
Arduino_A0 ===> 直流馬達_DIR
範例程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2017/6/2 | |
// Faya-Nugget 範例程式 (TactSwitch_1.ino) | |
// 單元: 模組介紹-faya觸控開關 | |
// 網址: http://fayalab.blogspot.com/2017/06/faya.html | |
// 目標: (1)當手指頭按下PB1時,直流馬達吹出弱風 | |
// (2)當手指頭按下PB2時,直流馬達吹出中風 | |
// (3)當手指頭按下PB3時,直流馬達吹出強風 | |
// (4)當手指頭按下PB4時,直流馬達停止運轉 | |
// 接線: Arduino ==> faya模組 | |
// 3 ==> PB1 (按鍵模組) | |
// 4 ==> PB2 (按鍵模組) | |
// 5 ==> PB3 (按鍵模組) | |
// 12 ==> PB4 (按鍵模組) | |
// 6 ==> SIG (直流馬達模組) | |
// A0 ==> DIR (直流馬達模組) | |
int buttonPin_PB1 = 3; // 設定按鍵腳位 | |
int buttonPin_PB2 = 4; | |
int buttonPin_PB3 = 5; | |
int buttonPin_PB4 = 12; | |
int motorPin_SIG = 6; // 輸出PWM訊號 | |
int motorPin_DIR = A0; // 當數位腳位使用 | |
void setup() { | |
pinMode(buttonPin_PB1, INPUT); // 設定按鍵為輸入腳位 | |
pinMode(buttonPin_PB2, INPUT); | |
pinMode(buttonPin_PB3, INPUT); | |
pinMode(buttonPin_PB4, INPUT); | |
pinMode(motorPin_SIG, OUTPUT); //設定馬達為輸出腳位 | |
pinMode(motorPin_DIR, OUTPUT); | |
digitalWrite(motorPin_DIR, HIGH); // 設定馬達旋轉方向 | |
// 如果馬達的葉片是逆向的,請設為LOW | |
} | |
void loop() { | |
if (digitalRead(buttonPin_PB4) == HIGH) //當按下PB4時 | |
{ | |
analogWrite(motorPin_SIG, 0); //PB4按下時停止運轉 | |
} | |
else | |
{ | |
if (digitalRead(buttonPin_PB1) == HIGH) //當按下PB1時 | |
{ | |
analogWrite(motorPin_SIG, 120); //低速風 | |
} | |
if (digitalRead(buttonPin_PB2) == HIGH) //當按下PB2時 | |
{ | |
analogWrite(motorPin_SIG, 180); //中速風 | |
} | |
if (digitalRead(buttonPin_PB3) == HIGH) //當按下PB3時 | |
{ | |
analogWrite(motorPin_SIG, 240); //高速風 | |
} | |
} | |
} |
- L14: 透過PWM訊號讓直流馬達能夠有不同的輸出風速,NANO能夠輸出PWM訊號的腳位有D3/D5/D6/D9/D10/D11,這個範例中我們使用D6
- L45/L49/L53: 利用analogWrite輸出低/中/高電壓驅動直流馬達,達到低/中/高轉速
範例結果:
討論:
機械式的按鍵開關都有彈跳的問題,也就是說當我們按下按鈕然後放開後,開關的狀態是on-off-on-off來回好幾次的,從示波器裡我們可以看到開關的狀態如下畫面
通常我們可以透過硬體或程式消除彈跳對作品的影響,但由於在我們的程式中,我們未定義每個按鍵LOW的狀態,引此當遇到此狀態時,系統不予處理,因此對我們的範例不會造成影響,往後,我們再利用一篇文章整理一下除彈跳的技巧!
歡迎大家在底下留言或到我們的粉絲團留言喔!
====================================
fayalab 粉絲團
FB本篇留言版
沒有留言:
張貼留言