// CODE = GREEN, BLUE, RED
uint8_t index;
uint8_t code [3];
unsigned long inputTimer;
unsigned long resetTimer;
constexpr uint8_t BLUE = 15;
constexpr uint8_t GREEN = 23;
constexpr uint8_t RED = 27;
void setup()
{
DDRB = 7;
Serial.begin(9600);
}
void printColor(uint8_t button)
{
switch(button)
{
case 15:
Serial.println("BLUE");
break;
case 23:
Serial.println("GREEN");
break;
case 27:
Serial.println("RED");
break;
}
}
void loop()
{
unsigned long now = millis();
uint8_t button = PIND;
if (index < 3 && button != 31 && now - inputTimer > 500)
{
inputTimer = now;
resetTimer = now;
code[index] = button;
printColor(button);
index++;
}
if (index == 3)
{
PORTB = code[0] == 23 && code[1] == 15 && code[2] == 27 ? 6 : 1;
if (now - resetTimer > 3000)
{
PORTB = 0;
index = 0;
}
}
}