doing digital reads in an interrupt seems kind of frowned on so I made ISR( PCINT1_vect ) as small as I could in this version. It does exactly the same thing as the previous post, just takes a litle more effort to move the pins around.
Code:
int lbuttonPin = 17; // Left Button, on analog 3,
int mbuttonPin = 18; // Middle Button, on analog 4
int rbuttonPin = 19; // Right Button, on analog 5
int lbuttonBit = 8; // pin17 is a bitmask 8 on port C
int mbuttonBit = 16; // pin18 is a bitmask 16 on port C
int rbuttonBit = 32; // pin19 is a bitmask 32 on port C
//pretend we start with upressed buttons
int buttonpins = lbuttonBit + mbuttonBit + rbuttonBit;
//attach the interrupt
ISR( PCINT1_vect ){
buttonpins &= PINC;//bypassing digitalRead for interrupt performance
}
void setup() {
Serial.begin(9600);
pinMode( lbuttonPin, INPUT );
pinMode( mbuttonPin, INPUT );
pinMode( rbuttonPin, INPUT );
//"turn on" the internal pullup resistors
digitalWrite( lbuttonPin, HIGH);
digitalWrite( mbuttonPin, HIGH);
digitalWrite( rbuttonPin, HIGH);
//low level interrupt enable stuff
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT11);
PCMSK1 |= (1 << PCINT12);
PCMSK1 |= (1 << PCINT13);
}
void loop() {
//print out what buttons have been pressed
if(!(buttonpins&lbuttonBit)) Serial.print("lbutton");
Serial.print(",");
if(!(buttonpins&mbuttonBit)) Serial.print("mbutton");
Serial.print(",");
if(!(buttonpins&rbuttonBit)) Serial.print("rbutton");
Serial.println("");
//reset the buttons
buttonpins = lbuttonBit + mbuttonBit + rbuttonBit;
delay(1000);
}