Hello,
I designed an 8 channel self watering system using AT2560mega. There are 8 capacitive sensors connected to Analog A0 thru A7, and 8 digitalWrites controlling solenoids. I want the pump feeding the solenoids to be on anytime any of the solenoids are on, however the only time the pump energizes is when the last solenoid in the list is energized. I thought maybe setting up the pump loop separately but operating from the same capacitive analog signals, but it seems to be a lot of repetitious code. Any suggestions are welcome.
const int pumpSolenoid[8] = { 28, 30, 32, 34, 36, 44, 46, 48 };
const int moistureSensor[8] = { A0, A1, A2, A3, A4, A5, A6, A7 };
const int pumpRelay[1] = { 50 };
const int ON = LOW;
const int OFF = HIGH;
// Lower = wetter, 220 = completely wet, 513 = completely dry
const int moistureThreshold[8] = { 400, 400, 400, 400, 400, 400, 400, 400 }; // Adjust to each plant's needs
void setup() {
Serial.begin(9600);
for(int i = 0; i < 8; i++) {
pinMode(pumpSolenoid[i], OUTPUT);
digitalWrite(pumpSolenoid[i], OFF);
pinMode(pumpRelay[i], OUTPUT);
digitalWrite(pumpRelay[i], OFF);
pinMode(moistureSensor[i], INPUT);
}
delay(500);
}
void loop() {
int moistureLevel;
for(int i = 0; i < 8; i++) {
moistureLevel = analogRead(moistureSensor[i]);
Serial.print("MOISTURE LEVEL ");
Serial.print(i);
Serial.print(": ");
Serial.println(moistureLevel);
// If the soil is too dry, turn the pump on.
// Otherwise, turn the pump off
if(moistureLevel > moistureThreshold[i]) {
digitalWrite(pumpSolenoid[i], ON), digitalWrite(pumpRelay[i], ON);
} else {
digitalWrite(pumpSolenoid[i], OFF), digitalWrite(pumpRelay[i], OFF); }
}
Serial.println();
delay(10000);
}

