View Single Post
Old 04-29-2008, 08:28 AM   #41 (permalink)
dcb
needs more cowbell
 
dcb's Avatar
 
Join Date: Feb 2008
Location: ÿ
Posts: 5,038

pimp mobile - '81 suzuki gs 250 t
90 day: 96.29 mpg (US)

schnitzel - '01 Volkswagen Golf TDI
90 day: 53.56 mpg (US)
Thanks: 158
Thanked 269 Times in 212 Posts
Same thing, only tighten up the interrupt handler

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);
}

__________________
WINDMILLS DO NOT WORK THAT WAY!!!
  Reply With Quote