By the way, I had written a small class named AutomaticPump
to handle such a simple automatic pump which was making the sketch super easy to write.
Each pump has a name, a relay pin and a sensor pin (and optionally a threshold which defaults to 550). you build an array of such pumps and the only thing you need to do is begin all the pumps in the setup and update all the pumps in the loop.
something like this:
/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
===============================================
*/
#include "AutomaticPump.h"
AutomaticPump pumps[] = {
// name, relay, sensor
{"Green bean", 2, A0},
{"strawberry", 3, A1},
{"Old Tomato", 4, A2},
{"Zucchini", 5, A3}
};
void setup() {
Serial.begin(115200);
AutomaticPump::beginAll();
}
void loop() {
AutomaticPump::updateAll();
delay(5000);
}
the output would be something like
Configuring the Green bean pump.
Configuring the strawberry pump.
Configuring the Old Tomato pump.
Configuring the Zucchini pump.
--------------------------------
Green bean 1023 ON
strawberry 1023 ON
Old Tomato 456 OFF
Zucchini 398 OFF
--------------------------------
Green bean 1023 ON
strawberry 1023 ON
Old Tomato 451 OFF
Zucchini 452 OFF
--------------------------------
Green bean 1023 ON
strawberry 1023 ON
Old Tomato 454 OFF
Zucchini 451 OFF
--------------------------------
...
click to see the source files for the class, to add in the same sketch folder
AutomaticPump.h
/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#ifndef AUTOMATICPUMP_H
#define AUTOMATICPUMP_H
#include <Arduino.h>
class AutomaticPump {
public:
AutomaticPump(const char * name, const byte sensorPin, const byte relayPin, const float moistureThreshold = 550);
~AutomaticPump();
void begin();
void update();
static void updateAll();
static void beginAll();
private:
const char * name;
byte sensorPin;
byte relayPin;
float moistureThreshold;
AutomaticPump* nextPump;
static AutomaticPump* headPump;
};
#endif
AutomaticPump.cpp
/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "AutomaticPump.h"
AutomaticPump* AutomaticPump::headPump = nullptr;
AutomaticPump::AutomaticPump(const char * name, const byte sensorPin, const byte relayPin, const float moistureThreshold) :
name(name), sensorPin(sensorPin), relayPin(relayPin), moistureThreshold(moistureThreshold), nextPump(nullptr)
{
if (headPump == nullptr) headPump = this;
else {
AutomaticPump* current = headPump;
while (current->nextPump) current = current->nextPump;
current->nextPump = this;
}
}
AutomaticPump::~AutomaticPump() {
if (headPump == this) headPump = nextPump;
else {
AutomaticPump* current = headPump;
while (current->nextPump && current->nextPump != this) current = current->nextPump;
if (current->nextPump == this) current->nextPump = nextPump;
}
}
void AutomaticPump::begin() {
Serial.print(F("Configuring the "));
Serial.print(name);
Serial.println(F(" pump."));
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
}
void AutomaticPump::update() {
int v = analogRead(sensorPin);
v = analogRead(sensorPin);
if (v <= moistureThreshold - 10) {
digitalWrite(relayPin, HIGH);
} else if (v >= moistureThreshold) {
digitalWrite(relayPin, LOW);
}
Serial.print(name); Serial.write('\t'); Serial.print(v);
Serial.write('\t'); Serial.println(digitalRead(relayPin) == LOW ? F("ON") : F("OFF"));
}
void AutomaticPump::updateAll() {
AutomaticPump* current = headPump;
while (current) {
current->update();
current = current->nextPump;
}
Serial.println(F("--------------------------------"));
}
void AutomaticPump::beginAll() {
AutomaticPump* current = headPump;
while (current) {
current->begin();
current = current->nextPump;
}
Serial.println(F("--------------------------------"));
}