word KeyDown, KeyStat, kkk; // keys, switches, toggles, reeds, bare wires
word LedGreen=80,LedRed=80; // blink duration (green on pin A0, red on A1)
byte imagePORTC= B00000001; // outputs A0,A1 (01 de-synchronise red-green)
byte GoodKey; // 1,2,3,4...10 1,2,3,4..10 and so on
word set [] = {4,8,16,32,64,128,256,512,1024,2048}; // easy game : pins 2,3,4,5,6,7,8,9,10,11
void setup() { // INITIALIZATION CODE
DDRC |= B00000011; // A0, A1 as output (pinA0...pinA1)
PORTD |= B11111100; // PC7..PC2 with pullups (pin7....pin2)
PORTB |= B00001111; // PB3..PB0 with pullups (pin11...pin8)
}
void loop() { // MAIN CODE
delay(01); //
word iii = ((~PIND & 0xFC) | ((~PINB & 0x0F) << 8)); //-------// PORTD AND B (input pins) READING BLOC
word jjj = KeyStat; // debounce, state, and front detections
KeyStat = iii & kkk; // while key is maintained pressed
KeyDown = (jjj ^ KeyStat) & KeyStat; // when key is just pressed down
kkk = iii; // key configuration stored for next use
if (KeyDown) { //---------------------------------------------// KEY PRESSED TREATMENT BLOC ----------
GoodKey++; // first/next key and value to compare
if (KeyDown == set[GoodKey-1]) LedGreen = 20; // good choice for this one...green led
else {LedRed = 60; GoodKey = 0;} // error ! red led and start a new game
if (GoodKey >= 10) {LedGreen=LedRed=80;GoodKey=0;} // green/red led blinks together, bingo!
}
if (LedGreen) LedGreen--; //----------------------------------// BLINKING LEDS TREATMENT BLOC --------
if (LedRed) LedRed--; // blinking in progress
if (1 == 1) imagePORTC ^= B00000011; // it blinks here, all the time
if (!LedGreen) imagePORTC &= B11111110; // Green OFF at the end of the term
if (!LedRed) imagePORTC &= B11111101; // Red OFF at the end of the term
if (!LedGreen && GoodKey) imagePORTC |= B00000001; // maintains Green ON while success
PORTC = imagePORTC & B00000011; // set pins A0 A1 (LED green & red)
}