// https://forum.arduino.cc/t/two-slide-switches-to-sequence-leds/1012696
# include "ezButton.h"
ezButton test(3);
ezButton add(5), stop(4);
void setup() {
Serial.begin(112500);
Serial.println("test NPUMPS world!\n");
test.setDebounceTime(12);
add.setDebounceTime(12);
stop.setDebounceTime(12);
setupPumps();
}
void loop() {
test.loop();
add.loop();
stop.loop();
if (test.isPressed()) {
Serial.println("I saw that!");
}
if (add.isPressed()) {
Serial.print("I see that add... ");
if (addAPump()) Serial.println("and cannot");
else Serial.println("and did.");
}
if (stop.isPressed()) {
Serial.println("I saw that stop!");
stopAll();
}
}
# define NPUMPS 4
const unsigned char pumpPin[] = {A0, A1, A2, A3};
unsigned char nOn;
void setupPumps()
{
for (unsigned char tt = 0; tt < NPUMPS; tt++)
pinMode(pumpPin[tt], OUTPUT);
nOn = 0;
stopAll();
}
void stopAll()
{
nOn = 0;
for (unsigned char tt = 0; tt < NPUMPS; tt++)
digitalWrite(pumpPin[tt], LOW);
}
unsigned char addAPump()
{
/* load sharing */
static unsigned char nFirst;
static unsigned char myNext;
if (nOn == 0) {
myNext = nFirst;
nFirst++; if (nFirst >= NPUMPS) nFirst = 0;
}
if (nOn == NPUMPS) return 1; // FAIL no more pumps
nOn++;
/* here is where a pump might really be turned on, or the process for doing initiated
Serial.print("pump ");
Serial.print(nOn); // number in the work force 1, 2 &c.
Serial.print(" is ");
Serial.print(myNext); // pump ID number
Serial.println("");
*/
digitalWrite(pumpPin[myNext], HIGH);
myNext++; if (myNext >= NPUMPS) myNext = 0;
return 0; // SUCCEED
/* non load sharing first
if (nOn < NPUMPS) nOn++;
else return 1;
return 0;
*/
}