代码
int analogPin = A5; // 连接模拟针脚A5 int val = 0; // variable to store the value read int value = 0; #define ledPin 9 #define LIGHT_MAX 70 void setup() { Serial.begin(9600); // setup serial pinMode(ledPin, OUTPUT); } void loop() { val = analogRead(analogPin); // read the input pin Serial.println(val); // debug value value = map(val, 0, 1023, 100, 0); Serial.print("LIGHT="); Serial.println(value); // debug value if (value <= 70) { digitalWrite(ledPin, HIGH); // 亮灯 } else { digitalWrite(ledPin, LOW); // 熄灭 } delay(1000); }