Here is an easy way to check buttons while waitng for a delay to timeout:
int pin = 2;
void loop() {
// your loop code here
myDelay(2000); // call a new delay function that both delays and checks the button states
}
void myDelay(unsigned long duration)
{
unsigned long start = millis();
while (millis() - start <= duration) {
checkButtons(); // check the buttons
}
}
void checkButtons()
{
int buttonstate = digitalRead(pin);
if(buttonstate == HIGH)
{
// your code to set vari = newvalue;
}
}
void setup(){
}
//handle timing of action()
TimedAction timedAction = TimedAction(2000,action);
//handle button presses on pin 10 wired from pin 10 to switch to GND
Button button = Button(10,PULLUP); //change values to suit your needs
//add your variables here
void setup(){
//add your setup here
}
void loop(){
timedAction.check();
if (button.uniquePress()){ //this only evaluates true one time per press
//button is pressed, do something
//add your 'button pressed' code here
}
}
void action(){
//this function gets called approx. every 2000 millisecond
//add your 'every 2 seconds' actions here
}