const int key1 = 4; // const int key2 = 5; // const int key3 = 6; // const int key4 = 7; // const int key5 = 8; // const int key6 = 9; // const int key7 = 10; // const int lock = 12; // int lastKey = 0; // // void openLock() { digitalWrite(lock, LOW); delay(10000); closeLock(); } // void closeLock() { digitalWrite(lock, HIGH); } // key boolean isKeyPressed(int key) { int what = -1; while(true) { int countPressed = 0; if(digitalRead(key1) == HIGH) { if(lastKey != key1) { countPressed++; what = key1; lastKey = key1; } } if(digitalRead(key2) == HIGH) { if(lastKey != key2) { countPressed++; what = key2; lastKey = key2; } } if(digitalRead(key3) == HIGH) { if(lastKey != key3) { countPressed++; what = key3; lastKey = key3; } } if(digitalRead(key4) == HIGH) { if(lastKey != key4) { countPressed++; what = key4; lastKey = key4; } } if(digitalRead(key5) == HIGH) { if(lastKey != key5) { countPressed++; what = key5; lastKey = key5; } } if(digitalRead(key6) == HIGH) { if(lastKey != key6) { countPressed++; what = key6; lastKey = key6; } } if(digitalRead(key7) == HIGH) { if(lastKey != key7) { countPressed++; what = key7; lastKey = key7; } } if(what != -1) { Serial.print("pressed "); Serial.println(what - 3); if((countPressed == 1) && (key == what)) { break; } if((key != what) || (countPressed >= 1)) { break; } } } if(what == key) { return true; } else { return false; } } void setup() { // pinMode(test,INPUT); Serial.begin(9600); pinMode(key1, INPUT); pinMode(key2, INPUT); pinMode(key3, INPUT); pinMode(key4, INPUT); pinMode(key5, INPUT); pinMode(key6, INPUT); pinMode(key7, INPUT); pinMode(lock, OUTPUT); closeLock(); } // void loop() { if(isKeyPressed(key2)) { if(isKeyPressed(key3)) { if(isKeyPressed(key6)) { if(isKeyPressed(key2)) { if(isKeyPressed(key4)) { if(isKeyPressed(key6)) { if(isKeyPressed(key1)) { if(isKeyPressed(key7)) { openLock(); } } } } } } } } }
Source: https://habr.com/ru/post/232025/
All Articles