hi
i am trying to make a zigbee remote so i copied a code from Nick Gammon's website which is for a power efficient keypad. i uploaded the code,
although the code is working but it is not very responsive as after pressing key for many times , key is printed on the serial monitor only once and whent i press two keys one after another only one key is printed.
the code is
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
#include <avr/sleep.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};
byte rowPins[ROWS] = {12, A1, A2, A4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A5, A3, A0, 13}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))
// turn off interrupts until we are ready
ISR (PCINT0_vect)
{
PCICR = 0; // cancel pin change interrupts
} // end of ISR (PCINT0_vect)
ISR (PCINT1_vect)
{
PCICR = 0; // cancel pin change interrupts
} // end of ISR (PCINT1_vect)
ISR (PCINT2_vect)
{
PCICR = 0; // cancel pin change interrupts
} // end of ISR (PCINT2_vect)
void setup(){
Serial.begin(9600);
PCMSK0 |= _BV (PCINT4); // pin D12
PCMSK1 |= _BV (PCINT9); // pin A1
PCMSK1 |= _BV (PCINT10); // pin A2
PCMSK1 |= _BV (PCINT12); // pin A4
}
void reconfigurePins ()
{
byte i;
// go back to all pins as per the keypad library
for (i = 0; i < NUMITEMS (colPins); i++)
{
pinMode (colPins [i], OUTPUT);
digitalWrite (colPins [i], HIGH);
} // end of for each column
for (i = 0; i < NUMITEMS (rowPins); i++)
{
pinMode (rowPins [i], INPUT);
digitalWrite (rowPins [i], HIGH);
} // end of for each row
} // end of reconfigurePins
void goToSleep ()
{
byte i;
// set up to detect a keypress
for (i = 0; i < NUMITEMS (colPins); i++)
{
pinMode (colPins [i], OUTPUT);
digitalWrite (colPins [i], LOW); // columns low
} // end of for each column
for (i = 0; i < NUMITEMS (rowPins); i++)
{
pinMode (rowPins [i], INPUT);
digitalWrite (rowPins [i], HIGH); // rows high (pull-up)
} // end of for each row
// now check no pins pressed (otherwise we wake on a key release)
for (i = 0; i < NUMITEMS (rowPins); i++)
{
if (digitalRead (rowPins [i]) == LOW)
{
reconfigurePins ();
return;
} // end of a pin pressed
} // end of for each row
// overcome any debounce delays built into the keypad library
delay (50);
// at this point, pressing a key should connect the high in the row to the
// to the low in the column and trigger a pin change
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
byte old_ADCSRA = ADCSRA;
// disable ADC to save power
ADCSRA = 0;
PRR = 0xFF; // turn off various modules
PCIFR |= _BV (PCIF0) | _BV (PCIF1) | _BV (PCIF2); // clear any outstanding interrupts
PCICR |= _BV (PCIE0) | _BV (PCIE1) | _BV (PCIE2); // enable pin change interrupts
// turn off brown-out enable in software
MCUCR = _BV (BODS) | _BV (BODSE);
MCUCR = _BV (BODS);
sleep_cpu ();
// cancel sleep as a precaution
sleep_disable();
PRR = 0; // enable modules again
ADCSRA = old_ADCSRA; // re-enable ADC conversion
// put keypad pins back how they are expected to be
reconfigurePins ();
} // end of goToSleep
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
}if (!customKey)
{
// no key pressed? go to sleep
goToSleep ();
return;
}
}