Untitled

 avatar
unknown
plain_text
a year ago
4.3 kB
25
Indexable
#include <Keypad.h>
#include <NeoSWSerial.h>
#include <DFRobotDFPlayerMini.h>

#define PIN_MP3_RX 10
#define PIN_MP3_TX 11
#define PIN_COMBINE 12
#define PIN_VICTOIRE A0      // ✅ Utilise maintenant A0
#define PIN_MP3_BUSY 9

NeoSWSerial mp3Serial(PIN_MP3_RX, PIN_MP3_TX);
DFRobotDFPlayerMini mp3;

const char NUMERO_CORRECT[] = "0629272657";

const byte ROWS = 4;
const byte COLS = 3;
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

bool combineDecroche = false;
bool tonaliteJouee = false;
bool attenteRaccroche = false;
String saisie = "";
unsigned long dernierAppui = 0;

void setup() {
  Serial.begin(9600);
  mp3Serial.begin(9600);

  pinMode(PIN_COMBINE, INPUT_PULLUP);
  pinMode(PIN_VICTOIRE, OUTPUT);
  pinMode(PIN_MP3_BUSY, INPUT);
  digitalWrite(PIN_VICTOIRE, LOW);

  Serial.println(F("Initialisation DFPlayer..."));
  if (!mp3.begin(mp3Serial)) {
    Serial.println(F("Erreur DFPlayer"));
    while (1);
  }

  mp3.volume(25);
  Serial.println(F("Prêt. Décroche pour démarrer."));
}

void loop() {
  bool etatCombine = digitalRead(PIN_COMBINE) == LOW;

  // Détection décroché
  if (etatCombine && !combineDecroche) {
    combineDecroche = true;
    saisie = "";
    tonaliteJouee = false;
    attenteRaccroche = false;
    mp3.stop();
    delay(100);
    mp3.playMp3Folder(4);  // tonalité attente
    tonaliteJouee = true;
    dernierAppui = millis();
    Serial.println(F("Combiné décroché → MP3 0004.mp3 joué"));
  }

  // Détection raccroché
  if (!etatCombine && combineDecroche) {
    combineDecroche = false;
    saisie = "";
    tonaliteJouee = false;
    attenteRaccroche = false;
    mp3.stop();
    Serial.println(F("Combiné raccroché → reset"));
  }

  // Lecture clavier
  if (combineDecroche && !attenteRaccroche) {
    char key = keypad.getKey();
    if (key >= '0' && key <= '9') {
      if (tonaliteJouee) {
        mp3.stop();
        tonaliteJouee = false;
        delay(100);
      }

      if (digitalRead(PIN_MP3_BUSY) == HIGH) {
        saisie += key;

        mp3.playMp3Folder(2);  // bip touche
        delay(100);            // délai léger sans attendre la fin du son

        dernierAppui = millis();
        Serial.print(F("Touche : "));
        Serial.println(key);

        if (saisie.length() == 10) {
          Serial.print(F("Numéro saisi complet : "));
          Serial.println(saisie);

          mp3.stop();
          delay(100);
          mp3.playMp3Folder(1);  // tonalité appel
          Serial.println(F("MP3 : tonalité appel 0001.mp3"));
          delay(1500);

          if (saisie == NUMERO_CORRECT) {
            mp3.playMp3Folder(5);  // message victoire
            Serial.println(F("MP3 : message victoire 0005.mp3"));

            digitalWrite(PIN_VICTOIRE, HIGH);  // ✅ Signal 5V sur A0
            delay(3000);
            digitalWrite(PIN_VICTOIRE, LOW);
          } else {
            int r = random(2);
            if (r == 0) {
              mp3.playMp3Folder(3);  // occupé
              Serial.println(F("MP3 : numéro occupé 0003.mp3"));
            } else {
              mp3.playMp3Folder(7);  // erroné
              Serial.println(F("MP3 : numéro erroné 0007.mp3"));
            }
          }

          saisie = "";
          attenteRaccroche = true;
          Serial.println(F("Fin de séquence → en attente de raccroché"));
        }
      }
    }

    // Gestion inactivité 3s pour numéro incomplet
    if (saisie.length() > 0 && saisie.length() < 10) {
      if (millis() - dernierAppui > 3000) {
        Serial.println(F("Inactivité > 3s → MP3 : numéro erroné 0007.mp3"));
        mp3.stop();
        delay(100);
        mp3.playMp3Folder(7);
        saisie = "";
        attenteRaccroche = true;
        Serial.println(F("Fin de séquence → en attente de raccroché"));
      }
    }

    if (key == '*' || key == '#') {
      Serial.println(F("Touche * ou # ignorée"));
    }
  }
}
Editor is loading...
Leave a Comment