I would like to execute an instruction only while a button is held in. I need to do this for several buttons in my program but I am stuck as to the most efficient way to do it. At present, I debounce the button and write its state to a variable. I am stuck as to how I can manage these buttons in my code?
int LHTopLimit;
int LHObsLimit;
int LHBotLimit;
int RHTopLimit;
int RHObsLimit;
int RHBotLimit;
int UPButton;
int DWNButton;
int LEDPIN;
byte buttons[] = {2,3,4,5,6,7,8,9}; // pin numbers of the buttons that we'll use
#define NUMBUTTONS sizeof(buttons)
#define LEDPIN 13
int buttonState[NUMBUTTONS];
int lastButtonState[NUMBUTTONS];
boolean buttonIsPressed[NUMBUTTONS];
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int ledState = HIGH;
int names[] = {LHTopLimit, LHObsLimit, LHBotLimit, RHTopLimit, RHObsLimit, RHBotLimit, UPButton, DWNButton};
int *ptr = names;
#define NAMBUTTONS sizeof(names)
int currentNames;
int j;
void setup() {
Serial.begin(9600);
// define pins:
for (int i=0; i<(NUMBUTTONS-1); i++) {
pinMode(i, INPUT);
lastButtonState[i]=LOW;
buttonIsPressed[i]=false;
}
}
void loop()
{
LHTopLimit=false;
check_buttons();
action();
action2();
}
void check_buttons() {
for (int currentButton=0; currentButton<NUMBUTTONS; currentButton++) {
// read the state of the switch into a local variable:
int reading = digitalRead(buttons[currentButton]); // check to see if you just pressed the button and you've waited long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing then reset the debouncing timer
if (reading != lastButtonState[currentButton]) { lastDebounceTime = millis(); }
// whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
if ((millis() - lastDebounceTime) > debounceDelay) {
// if the button state has changed:
if (reading != buttonState[currentButton]) {
buttonState[currentButton] = reading;
if (buttonState[currentButton]==HIGH) { //pushing down on the button
buttonIsPressed[currentButton]=true; // set your flag for the adjustment function
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState[currentButton] = reading;
}
}
void action() {
for (int currentButton=0; currentButton<NUMBUTTONS; currentButton++) {
if (buttonIsPressed[currentButton]) {
// Serial.print("button "); Serial.println(buttons[currentButton]);
switch (buttons[currentButton]) {
case 2:
LHTopLimit=1;
break;
case 3:
LHObsLimit=1;
break;
case 4:
LHBotLimit=1;
break;
case 5:
RHTopLimit=1;
break;
case 6:
RHObsLimit=1;
break;
case 7:
RHBotLimit=1;
break;
case 8:
UPButton=1;
break;
case 9:
DWNButton=1;
break;
}
buttonIsPressed[currentButton]=false; //reset the button
}
}
}
void action2() {
Serial.println(LHTopLimit);
}