Last active
May 13, 2018 20:58
-
-
Save ronnietucker/e03a5c491f95476a8848 to your computer and use it in GitHub Desktop.
Moisture Detection With Two Nails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int moistureSensor = 0; // nail to read from is pin A0 | |
int moisture_val; | |
const int redLEDPin = 3; // red LED connected to digital pin | |
const int yellowLEDPin = 4; // yellow LED connected to digital pin | |
const int greenLEDPin = 5; // green LED connected to digital pin | |
const int blueLEDPin = 7; // blue LED connected to digital pin | |
const int buttonPin = 9; // button connected to digital pin | |
const int voltage = 8; // the voltage nail is on pin D8 | |
int buttonState = 0; // initialise button as off | |
void setup() { | |
Serial.begin(9600); //open serial port | |
pinMode(redLEDPin, OUTPUT); | |
pinMode(greenLEDPin, OUTPUT); //set the LED pins as outputs | |
pinMode(yellowLEDPin, OUTPUT); | |
pinMode(blueLEDPin, OUTPUT); | |
} | |
void loop() { | |
buttonState = digitalRead(buttonPin); | |
if (buttonState == HIGH) { | |
digitalWrite(blueLEDPin, HIGH); | |
digitalWrite(voltage, HIGH); | |
moisture_val = analogRead(moistureSensor); //read the value from the nail | |
Serial.print("moisture level: "); // | |
Serial.println( moisture_val ); //print moisture level 0-1024 | |
delay(1000); // fake thinking... | |
digitalWrite(blueLEDPin, LOW); | |
if (moisture_val < 170) { | |
digitalWrite(redLEDPin, HIGH); // if moisture is less than 190 have red on, others off | |
digitalWrite(yellowLEDPin, LOW); | |
digitalWrite(greenLEDPin, LOW); | |
} | |
if (moisture_val < 200 && moisture_val > 170) { | |
digitalWrite(redLEDPin, LOW); | |
digitalWrite(yellowLEDPin, HIGH); // if moisture is less than 230 but over 190 have yellow on, others off | |
digitalWrite(greenLEDPin, LOW); | |
} | |
if (moisture_val > 200 && moisture_val < 240) { | |
digitalWrite(redLEDPin, LOW); | |
digitalWrite(yellowLEDPin, LOW); | |
digitalWrite(greenLEDPin, HIGH); // if moisture is over 230 and less than 300 have green on, others off | |
} | |
if (moisture_val > 280) { | |
digitalWrite(redLEDPin, HIGH); // if moisture is over than 300 (possibly flooded) have red on, others off | |
digitalWrite(yellowLEDPin, LOW); | |
digitalWrite(greenLEDPin, LOW); | |
} | |
digitalWrite(voltage, LOW); // reset button state | |
delay(5000); // wait 5 ses | |
digitalWrite(redLEDPin, LOW); // if moisture is over than 300 (possibly flooded) have red on, others off | |
digitalWrite(yellowLEDPin, LOW); | |
digitalWrite(greenLEDPin, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment