#defineLDR_PINA0voidsetup(){// put your setup code here, to run once:Serial.begin(9600);}voidloop(){// put your main code here, to run repeatedly:int lightLevel;lightLevel =analogRead(LDR_PIN);Serial.println(lightLevel);delay(500);}
函数说明
analogRead():模拟引脚数据读取,获取模拟信号。
根据亮度控制灯光
#defineLDR_PINA0#defineLEDD4// 阈值,光照的阈值int threshold =400;voidsetup(){// put your setup code here, to run once:Serial.begin(9600);pinMode(LED,OUTPUT);}voidloop(){// put your main code here, to run repeatedly:int lightLevel;// 获取光照强度lightLevel =analogRead(LDR_PIN);Serial.println(lightLevel);delay(500);// 根据光照强度决定灯是否亮if(lightLevel < threshold){digitalWrite(LED,HIGH);}else{digitalWrite(LED,LOW);}}