I've used enough products with embedded microcontrollers and unresponsive UIs to know that it can make or break a product. Even a slight delay or lag between the button presses and the display updating, or button presses being ignored or double counted, can be extremely frustrating.
What design patterns can be used to deal with user input (from buttons) and updating displays without causing these issues?
I currently use the following kind of pattern (this is simplified down to the bare minimum):
#include <Bounce.h>
#define GREEN_LED 6 // Pin for green LED
#define BUTTON_PIN 15 // Pin for button - uses external pull down so active high
#define DISPLAY_REFRESH_INT 100 // How many MS between display updates
Bounce button = Bounce(BUTTON_PIN, 5);
// To signal between the button read and the display update
bool ledState = false;
// Used to keep track of last display update
long displayUpdate = 0;
void setup()
{
pinMode(GREEN_LED, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop()
{
// Read the button status
if (button.update())
{
if (button.risingEdge())
{
ledState = !ledState;
}
}
// Update the display periodically
if (millis() - displayUpdate > DISPLAY_REFRESH_INT)
{
displayUpdate = millis();
digitalWrite(GREEN_LED, ledState);
}
}
What other options are there? Is it ever worth using pin change interrupts to read buttons (if we are not considering battery life!).