本日用到器材
![]() |
| 可變電阻 |
|
|
名詞
PWM(Pulse Width Modulation)--一種以數位方式來達到類比輸出的技術,一般 Arduino 板子其 digital 的 3、5、6、9、10 以及 11 接腳均是,詳情請見官網。程式部分
Fade 範例
程式碼可由 File -> Examples -> Basics -> Fade 找到。之前說明過的部分會省略掉,麻煩自己看之前的筆記,謝謝。/** * SyntaxHighlighter */ function foo() { if (counter <= 10) return; // it works! }
/* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. This example code is in the public domain. */ int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); }說明:
- 第 23 行
analogWrite(led, brightness);:將brightness的值以analogWrite的方式寫入 9 號接腳(pwm)。 - 第 26 行
brightness = brightness + fadeAmount;:先看等號右手邊的部分,這是將原本的brightness的值加上fadeAmount後,然後再將值賦與左手邊的brightness。 - 第 29~31 行:此處包含一個判斷區塊,底下拆分說明。
- 第 29 行
if (brightness == 0 || brightness == 255): 此為if判斷式,如果在()為真,則會執行{}內的內容,通常會與else搭配,不過此範例中else的內容為空,所以沒有寫出來。()中的brightness == 0代表判斷brightness是否為0,是的話為true,否則為false。
而連結兩個判斷式的 「||」 為邏輯語法的 or(或)語句,代表前或後其中一個對即成立。同為邏輯語法的還有 「&&」 ,代表 and(且)語句,前後均需為 true 才會返回 true 。還有 「!」 代表 not(否定),也就是原本為 true 會變成 false ,而原本為 false 的會變為 true 。兩者的關係如下表:||T F T T T F T F &&T F T T F F F F - 第 30 行
fadeAmount = -fadeAmount;:目的是將fadeAmount變號(負變正,正變負)。 - 由整個
loop的內容來看,此函式區塊會每 30 毫秒(millisecond)執行一次,一開始的brightness為0,之後每次遞增5(fadeAmount的值),一直到brightness的值為255,此時fadeAmount會被改為-5,所經過第 26 行的時候,brightness的值會一直遞減,直到0為止,此時又會將fadeAmount設為5,如此一來, LED 燈就會達到漸亮→漸暗→漸亮的效果。
程式執行結果:
Digital Read 範例
程式碼的部分可以由 File -> Examples -> Basics -> DigitalReadSerial 找到。而接線方法如下:/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor This example code is in the public domain. */ // digital pin 2 has a pushbutton attached to it. Give it a name: 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 }
說明:
- 第 14 行
Serial.begin(9600);:設定傳送資料的速率,常用的速度為 9600 bits/secs ,詳細資料請自行查閱官網。 - 第 22 行
int buttonState = digitalRead(pushButton);:將pushButton的資料讀進來,並放入變數buttonState中。 - 第 24 行
Serial.println(buttonState);:將資料由 serial port 印出,而與Serial.print()的不同是,他會自動幫你在每一個輸出後面加上換行符號,以利閱讀。
![]() |
| Serial Monitor |
接線方式如下:/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor This example code is in the public domain. */ // digital pin 2 has a pushbutton attached to it. Give it a name: int pushButton = 2; const int LED = 13; // 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); // make the LED's pin an output: pinMode(LED, OUTPUT); } // 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); // check the value of buttonState and trun on/off the LED: if (buttonState == 1) { digitalWrite(LED, HIGH); } else { digitalWrite(LED, LOW); } delay(1); // delay in between reads for stability }
說明:
- 第 10 行
const int LED = 13;:其中的const為修飾此LED為一常數,故賦值之後不可再修改,通常會於宣告時一起賦值。 - 由原本的例子可以得到按下去的結果為
1,放開的結果為0。
Analog Read 範例
程式碼可以在 File -> Examples -> Basics -> AnalogReadSerial 中找到。而接線方法如下:/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); delay(1); // delay in between reads for stability }
說明:
- 第 18 行
int sensorValue = analogRead(A0);:將A0的值以analogRead的方式讀入,並紀錄在變數sensorValue中。
接線方式如下,因怕 LED 會燒掉,所以還多加一顆 10KΩ 的電阻做保護。/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */ const int LED = 11; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // make the LED's pin an OUTPUT pinMode(LED, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: int mappedValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(LED, mappedValue); Serial.println(sensorValue); delay(1); // delay in between reads for stability }
說明:
- 由原本的例子可以得到,所取得的
sensorValue會落在 0 ~ 1023 之間。 - 第 22 行的
int mappedValue = map(sensorValue, 0, 1023, 0, 255);:因為使用 analogWrite 時,其值得落在 0 ~ 255 之間,但所取得的值明顯超出,故以一 map 函式將值轉換到我們要的區間之內,其傳入的參數第一個為原本的值,第二、三個參數為原本的最小、最大值,第四、第五個參數為目標的最小、最大值。






沒有留言:
張貼留言