View Single Post
Old 04-28-2008, 11:18 PM   #40 (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
button interrupts have been sorted out.

Phew, finally got the buttons to be read reliably with interrupts in arduino IDE. No polling required, though we might want to elaborate on the debouncing at some point. This also means there are a lot more pins for doing external interrupts with than arduino allows "out of the box", though only pins 2&3 do RISING/FALLING, the rest are state change, not a big deal.


Code:
#include <avr/interrupt.h>    
#include <avr/io.h>  

int lbuttonPin = 17; // Left Button, on analog 3 
int mbuttonPin = 18; // Middle Button, on analog 4 
int rbuttonPin = 19; // Right Button, on analog 5 

//button signals are inverted (internal pullup resistor), so true is not pressed
boolean  lbutton = true;
boolean  mbutton = true;
boolean  rbutton = true;
 
//attach the interrupt 
ISR( PCINT1_vect ){ 
  PCICR &= !(1 << PCIE1);//disable interrupts in the interrupt
  if (!digitalRead(lbuttonPin)) lbutton = false;
  if (!digitalRead(mbuttonPin)) mbutton = false;
  if (!digitalRead(rbuttonPin)) rbutton = false;
  PCICR |= (1 << PCIE1);
} 
 
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 stuff
  PCICR |= (1 << PCIE1); 
  PCMSK1 |= (1 << PCINT11); 
  PCMSK1 |= (1 << PCINT12); 
  PCMSK1 |= (1 << PCINT13);   
} 
 
void loop() {    
  //print out what buttons have been pressed
  if(!lbutton) Serial.print("lbutton");
  Serial.print(",");
  if(!mbutton) Serial.print("mbutton");
  Serial.print(",");
  if(!rbutton) Serial.print("rbutton");
  Serial.println("");
  
  //reset the buttons
  lbutton = true;
  mbutton = true;
  rbutton = true;
  
  delay(500);
}
__________________
WINDMILLS DO NOT WORK THAT WAY!!!
  Reply With Quote