I'm using my own button class here KTS_Button.h (3.3 KB) but you're welcome to write your own, use mine or just see how I'm doing it in my one:
I wouldn't claim that this is the best or the right way (just how I would approach it), but it does allow a set-up that's pretty easy to use and extend like this:
// Version 0.0
#define NUM_BUTTONS 2
#include "KTS_Button.h"
KTS_Button buttons[NUM_BUTTONS] = { 7, 6 };
enum Modes {
RUN = 0,
EDIT,
NUM_MODES
}; Modes mode = RUN;
char* modeNames[NUM_MODES] = { "Run Mode", "Edit Mode" };
void cycleModes() {
mode = mode + 1;
if(mode == NUM_MODES)
mode = 0;
Serial.print(F("Mode Change: "));
Serial.println(modeNames[mode]);
}
void printMessages() {
switch(mode) {
case RUN:
Serial.println(F("Run Mode Short Press"));
break;
case EDIT:
Serial.println(F("Edit Mode Short Press"));
break;
}
}
void setup() {
Serial.begin(9600);
buttons[0].setShortExecute(&printMessages);
buttons[1].setLongExecute(&cycleModes);
}
void loop() {
for (int i = 0; i < NUM_BUTTONS; i++) {
switch (buttons[i].read()) {
case SINGLE_PRESS:
buttons[i].shortExecute();
break;
case LONG_PRESS:
buttons[i].longExecute();
break;
}
}
}