#include <JC_Button.h>
const uint8_t code_unlock[] = { 6, 5, 5, 4, 3, 2 };
const uint8_t code_unlock_length = sizeof( code_unlock ) / sizeof( code_unlock[0] );
const uint8_t code_lock[] = { 6, 4, 2 };
const uint8_t code_lock_length = sizeof( code_lock ) / sizeof( code_lock[0] );
Button buttons[] = { {12}, {11}, {10}, {9}, {8}, {7} };
const uint8_t buttons_count = sizeof( buttons ) / sizeof( buttons[0] );
void setup()
{
Serial.begin( 115200 );
pinMode( LED_BUILTIN, OUTPUT );
for ( uint8_t i = 0; i < buttons_count; ++i )
{
buttons[i].begin();
}
}
void loop()
{
static bool lock_state = true;
static bool lock_state_prev = !lock_state;
static uint8_t * code = nullptr;
static uint8_t code_length = 0;
if ( lock_state_prev != lock_state )
{
lock_state_prev = lock_state;
if ( lock_state == true )
{
Serial.println( F( "Locked" ) );
digitalWrite( LED_BUILTIN, LOW );
code = code_unlock;
code_length = code_unlock_length;
}
else
{
Serial.println( F( "Unlocked" ) );
digitalWrite( LED_BUILTIN, HIGH );
code = code_lock;
code_length = code_lock_length;
}
}
for ( uint8_t i = 0; i < buttons_count; ++i )
{
buttons[i].read();
if ( buttons[i].wasReleased() )
{
static uint8_t position = 0;
if ( i == code[position] - 1 )
{
Serial.println( code[position] );
if ( ++position == code_length )
{
position = 0;
lock_state = !lock_state;
}
}
else
{
Serial.println( F( "Try again!" ) );
position = 0;
}
break;
}
}
}