實作:用光線感應器控制LED的閃爍
開啟Arduino IDE程式
將光線模組連接到控制IO擴充版上。
注意線的對應:GND -- G ,VCC -- V, S -- S
開啟 範例 Analog --> Analog Input
確定開發版、序列埠有設定正確。
執行。
程式說明:
/* 多行註解開始
Analog Input
...
http://www.arduino.cc/en/Tutorial/AnalogInput
*/ 多行註解結束
宣告整數變數
int sensorPin = A0; // 從A0輸入類比值select the input pin for the potentiometer
int ledPin = 13; // 13腳位作為燈光控制輸出select the pin for the LED
int sensorValue = 0; // 輸入的值存放在此variable to store the value coming from the sensor
初始化設定開始,只有執行一次
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
} //初始化結束
永久迴圈不停止的執行
void loop() {
// 讀取類比輸入值,並放入sensorValue變數中 read the value from the sensor:
sensorValue = analogRead(sensorPin);
// 點亮LED turn the ledPin on
digitalWrite(ledPin, HIGH);
// 以sensorValue為延遲時間 stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// 熄滅LED turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
問題:
Arduino UNO的類比輸入值的範圍是介於____~ ____?
delay () 的計時單位是 ____秒?
在這裡LED的亮與滅是數位訊號還是類比訊號?
發想:
輸入的裝置可以換成光感應器嗎?
輸出的裝置可以換成蜂鳴器嗎?
可以製作一個夏日蟬鳴模擬器嗎?