I'm a beginner at Arduino.
I have a soil moisture sensor and I mapped the lowest and highest moisture value to a value in between 0 and 100.
What I was trying to do was if the value was less than 50, then the LED should turn on. But even when the value was less than 50 (e.g 25) the LED doesn't turn on.
The moisture detection or the value mapping works perfectly fine.
Does the issue lie on my code or my LED wiring?
Code:
int mostMoist = 180;
int leastMoist = 600;
int percent;
int raw;
const int threshold = 50;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
raw = analogRead(A0);
percent = map(raw,leastMoist,mostMoist,0,100);
if (percent < 0){
percent = 0;
}
if (percent < threshold){
digitalWrite(LED_BUILTIN,HIGH);
} else {
digitalWrite(LED_BUILTIN,LOW);
}
Serial.println(percent);
delay(2);
}