標題: Arduino VS photoresistor [打印本頁]
作者: liuyang 時間: 2013-7-18 04:24
標題: Arduino VS photoresistor
程序如下:
const int sensorMin = 0; // sensor minimum, discovered through experiment 模擬口讀到的值的最小值,由實驗得
const int sensorMax = 600; // sensor maximum, discovered through experiment 模擬口讀到的值的最大值,由實驗得
//*有以上可知,光敏電阻的阻值大概在0到24k左右
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the sensor:
int sensorReading = analogRead(0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);//此命令非常巧妙,直接把讀來的模擬值轉化成了對應得0-3的整型值
// do something different depending on the
// range value:
switch (range) { //使用switch命令,對0-3四種情況區別對待,數值越小,代表亮度越低
case 0: // your hand is on the sensor
Serial.println("dark");delay(100);
break;
case 1: // your hand is close to the sensor
Serial.println("dim");delay(100);
break;
case 2: // your hand is a few inches from the sensor
Serial.println("medium");delay(100);
break;
case 3: // your hand is nowhere near the sensor
Serial.println("bright");delay(100);
break;
}
}
連接圖:
1.JPG (7.23 KB, 下載次數: 59)
下載附件
2013-7-18 04:24 上傳
歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |