Загрузка данных


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const byte ROWS = 4, COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys),
                       rowPins, colPins, ROWS, COLS);

const int BUZZER = 10;

String password;
String entered = "";

void setup() {
  pinMode(BUZZER, OUTPUT);
  randomSeed(analogRead(A0));

  password = String(random(1000, 10000));

  lcd.init();
  lcd.backlight();
  showInput();
}

void loop() {
  char key = keypad.getKey();

  if (key >= '0' && key <= '9' &&
      entered.length() < 4) {
    entered += key;
    showInput();
  }

  if (key == '*') {
    entered = "";
    showInput();
  }

  if (key == '#' && entered.length() == 4) {
    lcd.clear();
    lcd.setCursor(0, 0);

    if (entered == password) {
      lcd.print("Correct!");
      tone(BUZZER, 1000, 500);
    } else {
      lcd.print("Wrong password");
      tone(BUZZER, 200, 1000);
    }

    lcd.setCursor(0, 1);
    lcd.print("Code: ");
    lcd.print(entered);

    delay(2000);
    entered = "";
    showInput();
  }
}

void showInput() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Guess password");
  lcd.setCursor(0, 1);
  lcd.print("Enter: ");
  lcd.print(entered);
}