So it sounds like you don't really need Edit Mode as it doens't do anything aside from give you access to Loop Mode and CC Mode. So it could be better to use the long presses on the two buttons to get access to them like in my example here...
Basically the default is Normal mode (on/offs)...
... and then long-pressing button 0 toggles between Normal/Loop Mode, whereby in Loop Mode you activate the relays.
... and long-pressing button 1 toggles between Normal/CC Mode, whereby you enter the Menu or increment the counter.
This is getting a little hard to follow now though with the nested switches and if's etc. If you added any more to this I think it would be better to rethink the approach and refactor.
// Version 0.0
#define NUM_RELAYS 2
#define RELAY_PIN1 22
#define RELAY_PIN2 23
int relays[2] = { RELAY_PIN1, RELAY_PIN2 };
#define NUM_BUTTONS 2
#include "KTS_Button.h"
KTS_Button buttons[NUM_BUTTONS] = { 7, 6 };
enum Modes {
NORMAL = 0,
LOOP,
CC,
NUM_MODES
}; Modes mode = NORMAL;
char* modeNames[NUM_MODES] = { "Normal Mode", "Loop Mode", "CC Mode" };
void toggleState() {
static bool state = false;
state = !state;
Serial.print(F("State: "));
Serial.println(state ? "On" : "Off");
}
void changeModes(Modes newMode) {
mode = (mode == newMode ? NORMAL : newMode);
Serial.print(F("Mode Change: "));
Serial.println(modeNames[mode]);
}
void runMenu() {
Serial.println(F("Menu Active"));
}
void incrementCounter() {
static int counter = 0;
counter = (counter == 100 ? 0 : counter+1);
Serial.print(F("Counter is: "));
Serial.println(counter);
}
void activateRelay(int choice) {
digitalWrite(relays[choice], HIGH);
delay(100); // Or whatever you need
digitalWrite(relays[choice], LOW);
Serial.print(F("Relay: "));
Serial.print(choice);
Serial.println(F(" Activated"));
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_RELAYS; i++)
pinMode(relays[i], OUTPUT);
}
void loop() {
for (int i = 0; i < NUM_BUTTONS; i++) {
switch (buttons[i].read()) {
case SINGLE_PRESS:
switch(mode) {
case NORMAL:
toggleState();
break;
case LOOP:
if (i == 0) activateRelay(0);
else if (i == 1) activateRelay(1);
break;
case CC:
if (i == 0) runMenu();
else if (i == 1) incrementCounter();
break;
}
break;
case LONG_PRESS:
if (i == 0) changeModes(LOOP);
else if (i == 1) changeModes(CC);
break;
}
}
}
Also if you don't need all the serial printing (like if it's only for debugging etc) then you can lose a whole bunch of the above code).
I have a sneaking suspicion that you've not fully explained what this code is for and what it should be doing in the 'real project' or 'real code'. If you can explain that in detail and what each button should actually be doing there might be better ways to do the same thing.
See it in action here: EditMode.ino - Wokwi Arduino and ESP32 Simulator