Skip to content

Instantly share code, notes, and snippets.

@mjoyce91
Last active September 7, 2017 03:31
Show Gist options
  • Save mjoyce91/43e26468eab43fcbc580fce33e16d81f to your computer and use it in GitHub Desktop.
Save mjoyce91/43e26468eab43fcbc580fce33e16d81f to your computer and use it in GitHub Desktop.
Arduino Soil Moisture Check
const int VAL_PROBE = 0; // Analog pin 0
const int MOISTURE_LEVEL = 90; // the value after the LED goes ON
void setup() {
Serial.begin(9600);
}
void LedState(int state) {
digitalWrite(13, state); // connect LED to 13
}
void loop() {
int moisture = analogRead(VAL_PROBE);
float val = fmap(moisture, 0, 1023, 0.0, 5.0); // use 5V
int percent = val / 5.0 * 100; // convert to percent
Serial.println(percent); // print percent
if(percent < MOISTURE_LEVEL) {
LedState(HIGH); // if less than 90%, alert us with LED on
} else {
LedState(LOW); // else, turn off LED
}
delay(1000); // repeat every...
}
float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment