标题: 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;
   }
}
连接图:






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1