// https://forum.arduino.cc/t/two-slide-switches-to-sequence-leds/1012696
//# include <Arduino.h>
//# include "RadioButtons.h"
// RadioButtons.h
# define PRESST LOW
# define ON HIGH
# define OFF LOW
class RadioButtons {
private:
int *buttonPins; // Array of button pin numbers to do: provide function hooks
int *ledPins; // Array of LED pin numbers to do: NUL/
int numButtons; // Number of radio buttons
bool lastButtonState[16]; // Array of last button states (fix size as needed)
int currentSelection; // Index of the current selected button
bool selectionChanged; // Flag to indicate if the selection has changed
unsigned long lastUpdateTime;
public:
// Constructor that initializes the button and LED pins and the number of buttons
RadioButtons(int *buttons, int *leds, int num);
void RadioButtons::begin() {
Serial.println("write radio button begin");
}
// Method to get the current selection
int read();
// Method to update the button and LED states
void update();
// Method to check if the selection has changed
bool changed();
private:
// to check if a button is pressed
bool isButtonPressed(int pin);
};
RadioButtons::RadioButtons(int *buttons, int *leds, int num) : buttonPins(buttons), ledPins(leds), numButtons(num), currentSelection(-1), selectionChanged(false) {
for (int ii = 0; ii < num; ++ii) {
// GPIO write(ledPins[i], LOW);
}
}
int RadioButtons::read() {
return currentSelection;
}
void RadioButtons::update() {
selectionChanged = false;
if (millis() - lastUpdateTime < 20) return;
lastUpdateTime = millis();
for (int ii = 0; ii < numButtons; ++ii) {
bool currentButtonState = isButtonPressed(buttonPins[ii]);
if (currentButtonState == lastButtonState[ii]) continue;
lastButtonState[ii] = currentButtonState;
if (!currentButtonState) continue;
if (currentSelection != ii) {
if (currentSelection >= 0) {
digitalWrite(ledPins[currentSelection], OFF);
}
currentSelection = ii;
digitalWrite(ledPins[ii], ON);
}
else {
currentSelection = -1;
digitalWrite(ledPins[ii], OFF);
}
selectionChanged = true;
}
}
bool RadioButtons::changed() {
return selectionChanged;
}
bool RadioButtons::isButtonPressed(int pin) {
return digitalRead(pin) == PRESST;
}
# 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;
*/
}