Coffre à code ESP32 — ESP32 simulation

Coffre-fort à code : clavier 4x4, écran LCD 2004 I2C, deux LED et un servo qui ouvre la porte puis la referme après 10 secondes.

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by yenihandresk.

Components

Sketch code

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

// ----- Clavier -----
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] = {13, 12, 14, 27};   
byte colPins[COLS] = {26, 25, 33, 32};   
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27, 20, 4);

const int stepsPerRevolution = 200;  
Stepper myStepper(stepsPerRevolution, 5, 23, 15, 2);

const int LED_VERTE = 18;
const int LED_ROUGE = 4;
const int BUZZER_PIN = 19;

const String CODE = "1234";
const String CODE_DEVEROUILLAGE = "0000";
String saisie = "";
int tentativesEchouees = 0;
bool enAlarme = false;
bool afficherEnClair = false; 

bool alerteGazEnCours = false;
bool porteOuverteParGaz = false;

void actualiserAffichageSaisie() {
  lcd.setCursor(0, 1);
  lcd.print("                    "); 
  lcd.setCursor(0, 1);
  if (saisie.length() == 0) {
    lcd.print("_");
  } else if (afficherEnClair) {
    lcd.print(saisie);
  } else {
    for (unsigned int i = 0; i < saisie.length(); i++) {
      lcd.print('*');
    }
  }
}

void setup() {
  Serial.begin(115200);

  pinMode(LED_VERTE, OUTPUT);
  pinMode(LED_ROUGE, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  
  digitalWrite(LED_VERTE, LOW);
  digitalWrite(LED_ROUGE, LOW);
  digitalWrite(BUZZER_PIN, LOW);

  myStepper.setSpeed(60); 

  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Entrez le code:");
  lcd.setCursor(0, 1);
  lcd.print("_");
  lcd.setCursor(0, 3);
  lcd.print("Systeme securise    ");
}

void alerteCourte() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CODE INCORRECT");
  unsigned long debut = millis();
  while (millis() - debut < 5000) {
    digitalWrite(LED_ROUGE, HIGH);
    tone(BUZZER_PIN, 1000);
    delay(250);
    digitalWrite(LED_ROUGE, LOW);
    noTone(BUZZER_PIN);
    delay(250);
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Entrez le code:");
  lcd.setCursor(0, 3);
  lcd.print("Systeme securise    ");
  saisie = "";
  actualiserAffichageSaisie();
}

void ouvrirPorte() {
  saisie = "";
  tentativesEchouees = 0; 
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CODE VALIDE");
  digitalWrite(LED_VERTE, HIGH);

  lcd.setCursor(0, 1);
  lcd.print("Ouverture...");
  
  myStepper.step(100);

  unsigned long debutCompteARebours = millis();
  int secRestantes = 10;
  bool fermetureForcee = false;

  while (secRestantes > 0) {
    char toucheInterruption = keypad.getKey();
    if (toucheInterruption == 'B') {
      fermetureForcee = true;
      break;
    }

    lcd.setCursor(0, 1);
    lcd.print("Porte ouverte: ");
    if (secRestantes < 10) lcd.print(" "); 
    lcd.print(secRestantes);
    lcd.print("s ");

    unsigned long debutSeconde = millis();
    while (millis() - debutSeconde < 1000) {
      char t = keypad.getKey();
      if (t == 'B') {
        fermetureForcee = true;
        br

More projects by yenihandresk