Skip to content

Instantly share code, notes, and snippets.

@refriedchicken
Created May 6, 2015 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save refriedchicken/dc3686e07f140c8bcf0e to your computer and use it in GitHub Desktop.
Save refriedchicken/dc3686e07f140c8bcf0e to your computer and use it in GitHub Desktop.
Soil Moisture
int soil=0;
int rEye = 10; //define right eye as pin10 on the Arduino
int lEye = 11; //define left eye as pin11 on the Arduino
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(rEye, OUTPUT); //set right eye for output
pinMode(lEye, OUTPUT); //set left eye for output
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(rEye, LOW);
digitalWrite(lEye, LOW);
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
sensorValue = constrain(sensorValue, 485, 1023);
// print out the value you read:
//Serial.println(sensorValue);
//map the value to a percentage
soil = map(sensorValue, 485, 1023, 100, 0);
// print out the soil water percentage you calculated:
if(soil<21){
digitalWrite(rEye, HIGH);
delay(1000);
digitalWrite(rEye, LOW);
delay(1000);
digitalWrite(rEye, HIGH);
}
if(soil>20){
digitalWrite(lEye, HIGH);
digitalWrite(rEye, LOW);
}
Serial.print(soil);
Serial.println("%");
delay(1000); // delay in between reads for stability
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment