Hello Members,
I am using pin change interrupts on an Arduino Nano to detect encoder pulses -
got help on this from Nick Gammon's site as well as others. I am currently using the Adafruit LCD shield with buttons to control a motor and select menu items. Using the library associated with reading the buttons produces multiple button clicks, etc. I found this post from Nick Gammon, http://www.gammon.com.au/forum/?id=10945, which has a nice solution for detecting key presses.
I am wondering if I can add the interrupt routine for digital pin 2 to my current program, which is using pin change interrupts on pins 11 and 13 (arduino digital pins).
This would give me two ISRs - for pin changes - here is my current code for pins 11 and 13:
I have configured the pins as inputs, etc
PCICR |= (1 << PCIE0); // enable group interrupts on PORTB PCINT[7:0]
PCMSK0 |= (1 << PCINT5); // enable interrupt pin 13-bit 5
PCMSK0 |= (1 << PCINT3); // enable interrupt pin 11-bit 3, added 21 Jan
and the ISR:
ISR (PCINT0_vect)//handle pin change interrupt for Port B (D8 to D13), triggered by pins 11 and 13
{
byte MSB = bitRead(PINB,3);
byte LSB = bitRead(PINB,5);
byte encoded = (MSB << 1) | LSB; //converting the 2 pin values to single value
//more compact is byte encoded = bitRead(PINB3)<<1 | bitRead(PINB5);
//sum = adding encoded to the previous encoded value, Nilton61 uses decimal representation of sum as an array index
byte sum = (lastEncoded << 2) | encoded;
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderCount ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderCount --;
lastEncoded = encoded; //store this value for next time
}
I would like to add a similar structure to my program using the code from Nick's post - I can put the code into the current project and attempt to run it. I created a test program using Nick's example, but with an ISR pin change:
in setup():
// pin 19 of MCP23017 is plugged into D2 of the Arduino which is interrupt 0
//*** attachInterrupt(0, keypress, FALLING); add register code here
DDRD |= (0 << 2); // Set direction for Arduino pin 2 as Input
PORTD |= (1 << 2); // Set digial pin 2 HIGH
// set the mask bits, etc
PCMSK2 |= (1 << PCINT18);
PCIFR |= bit (PCIF1); // clear any outstanding interrupts
PCICR |= (1 << PCIE2);
sei(); // enable interrupts
and the ISR:
//ISR for pin change
ISR (PCINT2_vect)
{
// handle pin change interrupt for A0 to A5 here
keyPressed = true;
// toggle LED
//digitalWrite (LEDWAKE, !digitalRead (LEDWAKE));
} // end of PCINT2_vect
This works as a standalone program loaded into the Nano. I would like to place the key/button reading code into my main program that currently reads the Adafruit LCD buttons (replacing the Adafruit button reading code). I will still use the LCD for display, but not use the buttons or the associated library (for button reading).
Does this make sense? or should I convert Nick's I2C keypress example to a "library" and use a .h file, etc.
Any suggestions are welcome.
thanks,
ewholz