first Arduino program - works but could use tinkering

The script verifies and compiles just fine. I just wanted to post my code here incase of a better way to write this.

future plans: (indoor plants) add ventilation controllers, air temp. humidity sensors etc.

I would prefer to use a switch...case statement for the valve controller and a separate function to turn on pump if a valve is open

this is just a quick draft really. I have not acquired all the hardware together yet.

hardware:
arduino uno R3
4 12v DC solenoid valves
12v DC pump
relays
tubing
watering rings
55 gallon barrel.

  //pump resources
const int pumpPin = 4;


  //solenoid valve resources 0-3
const int slndPin0 = 0;
const int slndPin1 = 1;
const int slndPin2 = 2;
const int slndPin3 = 3;


  //sensory input
const int analog0 = A0;
const int analog1 = A1;
const int analog2 = A2;
const int analog3 = A3; 


  //Values
#define trigger 250 //moisture level to start watering()
  //moisture level
int humidity0 = 0;
int humidity1 = 0;
int humidity2 = 0;
int humidity3 = 0;
  //sensor sample collector
long int moistureSum0 = 0;
long int moistureSum1 = 0;
long int moistureSum2 = 0;
long int moistureSum3 = 0;

  
void setup() {
  Serial.begin(9600);
  pinMode(pumpPin, OUTPUT);
  pinMode(slndPin0, OUTPUT);
  pinMode(slndPin1, OUTPUT);
  pinMode(slndPin2, OUTPUT);
  pinMode(slndPin3, OUTPUT);
  
}

void loop() {
  delay(3000);
  waterTest();
  //feed plant 0
  if (humidity0 <= trigger){         
    digitalWrite(slndPin0, HIGH);
    delay(250);
    digitalWrite(pumpPin, HIGH);
    delay(1500);
    digitalWrite(pumpPin, LOW);
    digitalWrite(slndPin0, LOW);
  }
  //feed plant 1
  if (humidity1 <= trigger){
    digitalWrite(slndPin1, HIGH);
    delay(250);                                     
    digitalWrite(pumpPin, HIGH);
    delay(1500);
    digitalWrite(pumpPin, LOW);
    digitalWrite(slndPin1, LOW);
  }
  //feed plant 2
  if (humidity2 <= trigger){
    digitalWrite(slndPin2, HIGH);
    delay(250);
    digitalWrite(pumpPin, HIGH);        
    delay(1500);
    digitalWrite(pumpPin, LOW);
    digitalWrite(slndPin2, LOW);
  }
  //feed plant 3
  if (humidity3 <= trigger){
    digitalWrite(slndPin3, HIGH);
    delay(250);                               
    digitalWrite(pumpPin, HIGH);
    delay(1500);
    digitalWrite(pumpPin, LOW);
    digitalWrite(slndPin3, LOW);
  }
  if(pumpPin, HIGH){
    digitalWrite(pumpPin, LOW);
  }
}

void waterTest() {
  for (int i = 0; i < 30; i++) {
    moistureSum0 = moistureSum0 + analogRead(analog0);
    moistureSum1 = moistureSum1 + analogRead(analog1);
    moistureSum2 = moistureSum2 + analogRead(analog2);
    moistureSum3 = moistureSum3 + analogRead(analog3);
  }
  humidity0 = moistureSum0 / 30;         //get the average soil humidity
  humidity1 = moistureSum1 / 30;
  humidity2 = moistureSum2 / 30;
  humidity3 = moistureSum3 / 30;

  // print the results to the serial monitor:
  Serial.print("Humidity0 = " );
  Serial.print(humidity0);
  Serial.print("\t Humidity1 = ");
  Serial.print(humidity1);
  Serial.print("\t Humidity2 = ");
  Serial.print(humidity2);
  Serial.print("\t Humidity3 = ");
  Serial.println(humidity3);
  Serial.println();
  
  moistureSum0 = 0;       //reset the readings
  moistureSum1 = 0;
  moistureSum2 = 0;
  moistureSum3 = 0;
  delay(3000); //3 second delay
}

WateringProgram-C.ino (2.68 KB)

You haven't gotten any responses, probably because people do not like to upload unknown files. Read the How to Post sticky at the top of the Forum by Nick Gammon on how to post source code here using code tags. You will likely get a better response.

thank you econjack I have embedded my code into the original post.

Some first reactions

Do not use pins 0 and 1 as inputs or outputs as on most Arduinos they are used by the hardware serial interface which is useful for debugging.

Don't declare variables as int when their value will never exceed 255. Pin numbers are a prime example.

Whilst it is normally a good ide to give pins names, the names you give to the analogue inputs add nothing in this case.

Whilst I have not examined your code in detail, the use of variable names with a numeric suffix often means that the code could be written using arrays to avoid writing repetitive code.

 if(pumpPin, HIGH){

This is wrong. Check the syntax of the if statement. Also, will pumpPin ever have a value other than 4 ?

 Serial.print("Humidity0 = " );

Consider using

 Serial.print(F("Humidity0 = ") );

when printing string constants in order to save memory

thank you thats a bit of help. I'm just going off of the way i saw other scripts for the same function.

pumpPin will change. don't want the pump constantly running. this is NOT drip irrigation.

basically this system is meant to detect need for water and then pump and port the water to the corresponding output(s)

I'll build an array so i can setup the switch statement so it can water multiple flowers at once. rather than fixing the if statement which breaks up my program and would take 4x as long for a complete watering.

I figured i would have incorrect pins selected. i just chose a pin to get started

I'll clean this up when i get time and post an update Thank you UKHeliBob

pumpPin will change.

Really? While the Arduino is running, you intend to unplug the pump and plug it into a different pin?

The VALUE read from the pin will change. The pin number will not.