Automated watering system code help.

Hi all,

I am working on automated watering system, similar to this one (link below). But I would like to water 5 plants with 5 separate sensors and pumps. I have tried to modify the code below, but now the sensor readings are inaccurate and the pumps never turn on.

This is the original code for one plant, which worked great:

const int VAL_PROBE = 0; //Analog pin 0
const int MOISTURE_LEVEL = 450; // the value after the LED goes on

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);

}

void loop()
{
digitalWrite(13,HIGH);
delay(2000);
int moisture = analogRead(VAL_PROBE);

Serial.print("Moisture = ");
Serial.println(moisture);

if(moisture < MOISTURE_LEVEL)
{
digitalWrite(7,LOW);
digitalWrite(13,LOW);
delay(10000);
}
else
{
digitalWrite(7,HIGH);
}
}

and this is how I modified it for 2 plants:

const int VAL_PROBE = 0; //Analog pin 1
const int MOISTURE_LEVEL = 750; // the value after the LED goes on
const int VAL_PROBE2 = 0; //Analog pin 2
const int MOISTURE_LEVEL2 = 750; // the value after the LED goes on

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

}

void LedState(int state)
{
digitalWrite(13,state);
}

void loop()
{
int moisture = analogRead(VAL_PROBE);

Serial.print("Moisture = ");
Serial.println(moisture);

if(moisture > MOISTURE_LEVEL)
{
LedState(HIGH);
digitalWrite(2,LOW);
}
else
{
LedState(LOW);
digitalWrite(2,HIGH);
}

int moisture2 = analogRead(VAL_PROBE2);

Serial.print("Moisture2 = ");
Serial.println(moisture2);

if(moisture > MOISTURE_LEVEL2)
{
LedState(HIGH);
digitalWrite(3,LOW);
}
else
{
LedState(LOW);
digitalWrite(3,HIGH);
}

delay(10000);
}

My setup is this:
A1-5- analog sensor input
D2-7- digital output to relays
D13- power for the sensors

I appreciate your help. Thanks

What you mean by inaccurate? Are you not getting the correct reading?

Pump is not turning on because of inaccurate reading?

When you find yourself tempted to suffix variable names with numbers, it is time to learn about arrays. I would not even attempt this project (5 sensors and 5 valves) without arrays.

Also, see the Blink Without Delay example and get rid of all occurrences of delay(...).
Your Arduino cannot check other sensors or operate other valves while executing delay(...).

const int VAL_PROBE = 0; //Analog pin 1
const int VAL_PROBE2 = 0; //Analog pin 2

That's a mistake. You are using the same pin for both. And you really should use the names A0 through A5 when you are referencing analog input pins.

  int moisture = analogRead(VAL_PROBE);
  if(moisture > MOISTURE_LEVEL)
 
  int moisture2 = analogRead(VAL_PROBE2);
  if(moisture > MOISTURE_LEVEL2)

And another mistake. You are reading a new value and using the old value! You should definitely use arrays for your inputs and outputs.

  delay(10000);

This will make your sketch very unresponsive. Everything it does will take at least ten seconds.

By inaccurate, I mean, code with one sensor would read between 200's to 1000's, but one I changed reads around 250's wet or dry.

I am new to arduino, haven't read much, I'll look into arrays and check back. Thanks

JohnWasser gave you explanation on why it fails. Use his suggestion to adapt your code

And you really should use the names A0 through A5 when you are referencing analog input pins.

The names A0, etc. are the names to use when using the analog pins as digital pins. They are not intended to be used when using the analog pins as analog pins.

PaulS:
The names A0, etc. are the names to use when using the analog pins as digital pins. They are not intended to be used when using the analog pins as analog pins.

The names can be used in both cases and I find it much easier to understand a sketch if the pin names are used whenever the analog input pin is meant. I did some Google searches and it looks like about half the time when analogRead() is called with a pin number people use the number and half the time people use the name.

Besides, if the intent was to always use the number for analogRead() the code would not provide a handy translation:

int analogRead(uint8_t pin)
{
	uint8_t low, high;

#if defined(analogPinToChannel)
#if defined(__AVR_ATmega32U4__)
	if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#endif
	pin = analogPinToChannel(pin);
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	if (pin >= 54) pin -= 54; // allow for channel or pin numbers
#elif defined(__AVR_ATmega32U4__)
	if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
	if (pin >= 24) pin -= 24; // allow for channel or pin numbers
#else
	if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif