I would not attempt to check them both in the loop due to the fact that your loop may be busy and not see the button being pressed.
Here is a working example of how I multi-task using your button example code. This should just plug right in.
In your exact case the loop process may be quick .. but that is bound to change so a process like this will help you be able to expand your application later.
Overview:You can use a timer for the button check.
If you do use a timer, you will want to remove the delay or your code will stall due to not being able to complete the task in the small timer window.
You should not need the delay or need to debounce if you put your button check code in a timer routine.
Example Code://Find timer at http://www.arduino.cc/playground/Main/MsTimer2
#include <MsTimer2.h>
//--- Button Pin
#define BUTTON 7
//--- used to store old value
int old_val=0;
unsigned long time=0;
boolean buttonPressNew = false;
void checkButton(){
//IMPORTANT: NO DELAY OR LONG ACTION HERE .. just change button status and take action in the loop
//-- no need for val to be global .. just takes up 2 bytes for nothing being global.
int val = digitalRead(BUTTON);
//-- If they are not the same then ...
if( val != old_val ){
//-- Reset last value
old_val = val;
//-- If value is turned on .. then we need to tell the system it was hit once ..
// but not do it over and over until released
if( val ){
buttonPressNew = true;
}
}
}
void setup() {
//--- Starndard Setup
pinMode(BUTTON,INPUT);
Serial.begin(9600);
//--- Initialize timer2 .. using 20ms for this example
MsTimer2::set(20, checkButton);
MsTimer2::start();
}
void loop() {
//Do any normal processing here ... it can run as long as you like and not effect the reading of the button.
//Simple Example ... a delay .. this will only process a single button click.
// so in this example .. if the button is pressed twice in this delay period .. it picks up only one time.
// if the button check was in the loop .. it would not see the button at all.
// if multiple button presses being excepted while in this delay .. and the action is quick .. it can be run in the loop
// or a counter added
delay(5000);
// This flag is set when a button is pressed .. take action in the loop and reset
if( buttonPressNew ){
// Take action based on the button being pressed...
// then reset
buttonPressNew = false;
//IMPORTANT: Notice serial printing done here .. not in timer process
Serial.println("Button Was Pressed");
}
}
Final Notes: You could add a counter to track the number of times the button was pressed while the loop was busy if desired.
Also .. if the action you are taking is really fast .. then you can go ahead and just take the action right in the checkButton() function .. you may need to expand beyond 20ms to fit in your process. Expanding too far will effect your button press responsiveness.
Hope this helps.