Hi guys,
I really like keyboards and bit processing, maybe my example can help ? voila:
#define KeyOne B00000100 // PortD (pin 2) to the Ground (0V) via button
#define KeysAll B00000100 // All Keys (on an input Port, 8 keys possible)
byte PPImuSta; // (Keys state)
byte PPImuMem; //
byte PPImuFro; // (Keys state)
int LedDurationOne; // timer Led 1
int LedDurationTwo = 100; // timer Led 2
boolean toggleLeds; // select Led One or Two
unsigned long previousmillis; // debounce and led duration timer
void setup(void) { //
PORTD |= KeysAll; // internal pullups on pins "KeysAll"
DDRB |= B00110000; // pins 12 and 13 (PB4 PB5) as output
} //
void loop(void) { //
KeysLeds(); //
} //
void KeysLeds(void) { // 1..8 Keys (input Port)
if (millis() - previousmillis > 10) { // 10 ms for debounce and Leds duration
previousmillis = millis(); //
//
byte iii = ~PIND & KeysAll; // reading (inverted) PORTD (pin 2, pin...)
byte jjj = PPImuSta; //
PPImuSta = iii & PPImuMem; // Keys state : pressed (maintained)
PPImuMem = iii; //
PPImuFro = (jjj ^ PPImuSta) & PPImuSta; // Keys state : first press (just changed)
// here you can play and adjust the code : ***********************************//// when a key is pressed :
if (PPImuFro & KeyOne) { //// first press (front) on KeyOne
toggleLeds = !toggleLeds; //// swap Led1 <-> Led2
if (toggleLeds) LedDurationOne=100; else LedDurationTwo=100; //// 100 * 10 ms = 1 s
} ////
if (LedDurationOne) {LedDurationOne--; digitalWrite(12, (LedDurationOne>0));} //// Leds management : ON / OFF
if (LedDurationTwo) {LedDurationTwo--; digitalWrite(13, (LedDurationTwo>0));} //// Leds management : ON / OFF
} ////
} ////
////