I am currently writing a program that reads an output voltage from a capacitive moisture sensor and depending on the moisture content, it will water some plants accordingly. I would like to make it so that a user can change the condition in real time because different plants will require different amounts of water. This will give a little more freedom on how much water the user would prefer for their plant. This is the code I have so far:

int solenoidPin = 4; //connected to Digital 4
int moisturePin = A0; //connected to Analog A0

void setup() {
  pinMode(solenoidPin, OUTPUT);
void loop() {
  int val;
  moisture = analogRead(moisturePin); //connect sensor to Analog A0
  Serial.println(val); //print the value to serial port
  delay(1000); //wait 1 second for next reading

  if (moisture <= 350) //if moisture is less than reading 350 {
  digitalWrite(solenoidPin, HIGH); //switch solenoid ON
  delay(600000) //water for ten minutes
  digitalWrite(solenoidPin, LOW); //switch solenoid OFF
  else {                     
  digitalWrite(solenoidPin, LOW); //switch solenoid OFF

I am aware that there is a touch screen shield for the Arduino and I could possibly allow the user to interact what is going on. However, I'm just asking if it's possible to change the if (moisture <= 350) while the code is running and perhaps allowing the user to change that value. If so, then how? Could I use a touch screen shield or another shield that allows a user to change the condition?

  if (moisture <= 350) //if moisture is less than reading 350 {

That is wrong, because you have the brace { after the comment. You need it before the comment, eg.

 if (moisture <= 350) { //if moisture is less than reading 350

Or preferably in my opinion:

if (moisture <= 350) //if moisture is less than reading 350

I'm just asking if it's possible to change the if (moisture <= 350)

Yes, of course. That's what variables are for. You could read a potentiometer, and the value from that could replace 350 in the code. You could use a simple LCD or LED display to show what the current reading is.

delay(600000) //water for ten minutes

Your problem here is that the sketch will be totally unresponsive (it would not react to your changing the moisture level) during the time you are watering. You might want to read How to do multiple things at once which shows how you can keep doing other things, while waiting for time to elapse.

  • Thanks for catching that small { fluke. Now, back to the the change in the condition. You mentioned a potentiometer. Where exactly in the circuit would that go? – user23244 Feb 27 '17 at 23:14
  • Actually, come to think of it, the potentiometer would probably be series with the moisture sensor. Please correct me if I'm wrong. – user23244 Feb 27 '17 at 23:25
  • That could be fiddly to get right. Normally you have a potentiometer with the 2 end pins connected to Gnd and +5V and the middle (wiper) to an analog input. It acts as a voltage divider and then you can read a number in the range 0 to 1023 which indicates how far you have turned the knob. – Nick Gammon Feb 28 '17 at 1:54
  • There's another "fluke" here: delay(600000) //water for ten minutes - there should be a semicolon after the right parenthesis. – Nick Gammon Feb 28 '17 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy