 	if (state == UNLOCKED) {
 		change(LOCKED);
 	}
+	else if (state == INCORRECT) {
+		change(INCORRECT);
+
+		if (code_idx == MAX_CODE)
+			change(LOCKED);
+	}
 	else {
-		if (bitRead(code, size - 1 - code_idx) != bit) {
-			change(LOCKED);
+		if (code[code_size - 1 - code_idx] == bit) {
+			change(PARTIAL);
+
+			if (code_idx == code_size) {
+				change(UNLOCKED);
+			}
 		}
 		else {
-			change(PARTIAL);
-		}
-
-		if (code_idx == size) {
-			change(UNLOCKED);
+			change(INCORRECT);
 		}
 	}
 }

#define ONE_BUTTON P1_1
#define ZERO_BUTTON P4_5

const uint16_t code = 0b10100111001;
const byte size = 11;

const unsigned int debounce_time = 40; // ms

byte code_idx = 0;

enum State { LOCKED, PARTIAL, UNLOCKED };

State state = LOCKED;


	code_idx = 0;
}

void incorrect() {
	code_idx++;
}

void partial() {
	code_idx++;
}
