Arduino Button state

I use the handy Adafruit LCD shield 

it’s a good product

gives you 5 buttons to read and a 2×20 LCD – all using the TWI/I2C interface. It uses a Microchip MCP23017 (datasheet) I2C port extender – reallllly cool chip that I have used in other applications.

The basic example that comes with the Adafruit library is ok – but I often want to know:

  •  when the button is pressed – and do something
  •  do something while the button is pressed
  •  do something when the button is released

so this is the code I use – it also has a nice timer thingy to do suff at regular intervals – and included a blink’thing that should blink at 1ms intervals.

Use the code as you like – and please let me know if you know how to do this in a better way

73, Benni TF3CY

#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();


uint16_t IntervalTimer1,IntervalTimer2,IntervalTimer3;

#define PORT_SHUTTER 51
#define PORT_FOCUS 53

#define WHITE 0x7

uint16_t BUTTSTATUS ; // status of the BUTTons

#define P_UP (1<<1)
#define P_DOWN (1<<2)
#define P_RIGHT (1<<3)
#define P_LEFT (1<<4)
#define P_SELECT (1<<5)

uint8_t STATUS;

#define BLINK (1<<0)

void buttonscan()
{
 uint8_t buttons = lcd.readButtons();

// UP BUTTON CHECKS
 if ( (buttons & BUTTON_UP) && (!(BUTTSTATUS & P_UP)) ) // we pushed it
 {
 BUTTSTATUS |= P_UP; // we pushed it
 }
 else if ((buttons & BUTTON_UP) && (BUTTSTATUS & P_UP)) // we are pushing it
 {

 }

 else if ((!(buttons & BUTTON_UP)) && (BUTTSTATUS & P_UP)) // we released it
 {
 BUTTSTATUS &= ~P_UP;

 }

// DOWN

 if ( (buttons & BUTTON_DOWN) && (!(BUTTSTATUS & P_DOWN)) ) // we pushed it
 {
 BUTTSTATUS |= P_DOWN; // we pushed it
 }
 else if ((buttons & BUTTON_DOWN) && (BUTTSTATUS & P_DOWN)) // we are pushing it
 {

 }
 else if ((!(buttons & BUTTON_DOWN)) && (BUTTSTATUS & P_DOWN)) // we released it
 {
 BUTTSTATUS &= ~P_DOWN;
 }

// LEFT

 if ( (buttons & BUTTON_LEFT) && (!(BUTTSTATUS & P_LEFT)) ) // we pushed it
 {
 BUTTSTATUS |= P_LEFT; // we pushed it 

 }
 else if ((buttons & BUTTON_LEFT) && (BUTTSTATUS & P_LEFT)) // we are pushing it
 {

 STATUS |= P_LEFT;
 }

 else if ((!(buttons & BUTTON_LEFT)) && (BUTTSTATUS & P_LEFT)) // we released it
 {
 BUTTSTATUS &= ~P_LEFT; 

 }

 // RIGHT

 if ( (buttons & BUTTON_RIGHT) && (!(BUTTSTATUS & P_RIGHT)) ) // we pushed it
 {
 BUTTSTATUS |= P_RIGHT; // we pushed it
 }
 else if ((buttons & BUTTON_RIGHT) && (BUTTSTATUS & P_RIGHT)) // we are pushing it
 {

 }

 else if ((!(buttons & BUTTON_RIGHT)) && (BUTTSTATUS & P_RIGHT)) // we released it
 {
 BUTTSTATUS &= ~P_RIGHT; 

 }

// SELECT

 if ( (buttons & BUTTON_SELECT) && (!(BUTTSTATUS & P_SELECT)) ) // we pushed it
 {
 BUTTSTATUS |= P_SELECT; // we pushed it
 }
 else if ((buttons & BUTTON_SELECT) && (BUTTSTATUS & P_SELECT)) // we are pushing it
 {
 // STATUS |= SELECTPUSHED;
 }

 else if ((!(buttons & BUTTON_SELECT)) && (BUTTSTATUS & P_SELECT)) // we released it
 {
 BUTTSTATUS &= ~P_SELECT; 

 }
}

void setup() {
 // Debugging output
 Serial.begin(9600);
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);

 lcd.print("Hello there");
 lcd.setBacklight(WHITE);

}

void loop() {

 // Here we do stuff we want to do as fast as possible
 buttonscan();
 unsigned long currentMillis = millis();

 if(currentMillis - IntervalTimer1 > 1000) // 1 second interval
 {
 IntervalTimer1 = currentMillis; 

 // just some example blink code
 STATUS ^= BLINK;

 if (STATUS & BLINK)
 {
 lcd.setCursor(1,1);
 lcd.print("*");
 }
 else
 {
 lcd.setCursor(1,1);
 lcd.print(".");
 }

 }

 if(currentMillis - IntervalTimer2 > 100) // 1/100 second interval
 {
 IntervalTimer2 = currentMillis;
 // stuff we want to run slow
 }

 if(currentMillis - IntervalTimer3 > 10) // 1/10 second interval
 {
 IntervalTimer3 = currentMillis;
 // suff we want to run quite fast
 }

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *