void setup() {
Serial.begin(115200);
//INPUT_PULLUP 開始保持高
//INPUT_PULLDOWN 開始持保
pinMode(34, INPUT);
pinMode(15, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(15, LOW);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
void loop() {
int value = analogRead(34);
Serial.println(value);
delay(100);
if (value > 2000) {
digitalWrite(15, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
} else if (value > 1000) {
digitalWrite(15, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
} else if (value > 500) {
digitalWrite(15, HIGH);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
} else {
digitalWrite(15, LOW);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}
}
模擬器
拉動離感測器 顯示不同顆數燈號
https://wokwi.com/projects/340307338283975252