本帖最後由 u850241 於 2015-4-22 11:21 編輯
各位前輩好: 我想藉光敏電阻來控制LED 開或關,先在void setup ()透過光敏電阻取得當時環境的sensorValue當作THRESHOLD值 再由void loop() 去比較這時的sensorValue 與THRESHOLD的值來去控制LED,寫了下列程式碼,但TRESHOLD仍會跳動. 可否請各位前輩指點迷津,感謝 #define analogInPin 0 #define THRESHOLD sensorValue int sensorValue=0; void setup() { Serial.begin(9600); sensorValue=0.7*sensorValue+0.3*analogRead(analogInPin); pinMode(13,OUTPUT); } void loop() { int r=sensorValue; sensorValue=0.7*sensorValue+0.3*analogRead(analogInPin); delay(1000); if(r>THRESHOLD){ digitalWrite(13,LOW); } else{ digitalWrite(13,HIGH); } Serial.print("THRESHOLD = "); Serial.print(THRESHOLD); Serial.print(",r "); Serial.println(r); |