Escaperoom Puzzle - Code MEGA
20/02/2021 | Views: 494 | Robotics | by: ELECTRONOOBS      


We have 2 codes for this project, one main code for the Arduino Mega and another code for the small NANO that controls the LEDs. We also need multiple libraries for the DFplayer, the fingerprint sensor and LED strip. Get the main code from below and see the links to downlaod each library. To install the libraries, just go to sketch, include library, add zip file and open the downlaoded .zip file for each one. Compile the code and upload it to the Arduino MEGA. In my case I had to use Arduino 1.6.5 version, otherwise I had compilation errors. So maybe go here on the Arduino previous versions page, and get that vesion in .zip file.







//Libraries
#include <Adafruit_Fingerprint.h>
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial1);

#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
RH_ASK driver;



#include <DFMiniMp3.h>
class Mp3Notify
{
  public:
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }
  static void OnPlayFinished(uint16_t globalTrack)
  {
    Serial.println();
    Serial.print("Play finished for #");
    Serial.println(globalTrack);   
  }
  static void OnCardOnline(uint16_t code)
  {
    Serial.println();
    Serial.print("Card online ");
    Serial.println(code);     
  }
  static void OnCardInserted(uint16_t code)
  {
    Serial.println();
    Serial.print("Card inserted ");
    Serial.println(code); 
  }
  static void OnCardRemoved(uint16_t code)
  {
    Serial.println();
    Serial.print("Card removed ");
    Serial.println(code);  
  }
};
DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial2);

int USB_drive_voltage;
int STEP = 1;
String Received = "";
int user_ID = 1;
bool code_ok = false;
int USB_value_min = 700;
int USB_value_max = 900;
int analog_counter = 0;
bool push_state = false;

//Inouts
#define drive_in A0
#define key_switch 8
#define push_button 9
#define buzzer 7
#define drive_out 6
#define fingerprint_LEDs 5

void setup() {
  Serial.begin(115200);
  Serial3.begin(115200);
  pinMode(drive_in, INPUT_PULLUP);
  pinMode(key_switch, INPUT_PULLUP);
  pinMode(push_button, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer,LOW);
  pinMode(drive_out, OUTPUT);
  digitalWrite(drive_out,LOW);
  pinMode(fingerprint_LEDs, OUTPUT);
  digitalWrite(fingerprint_LEDs,LOW);
  
  // set the data rate for the sensor serial port
  finger.begin(57600);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) { delay(1); }
  }

  mp3.begin();
  digitalWrite(buzzer,HIGH);
  delay(100);
  digitalWrite(buzzer,LOW);
  mp3.setVolume(28);
  delay(1000);
  mp3.playMp3FolderTrack(1);    //Master key required

  delay(100);
  
  //Start radio up
  #ifdef RH_HAVE_SERIAL
      Serial.begin(9600);    // Debugging only
  #endif
      if (!driver.init())
  #ifdef RH_HAVE_SERIAL
           Serial.println("init failed");
  #else
    ;
  #endif
  delay(10);
}

void loop() {
  ////////////STEP 1 USB DRIVE READ///////////
  if(STEP == 1){
    USB_drive_voltage = analogRead(A0);
    if(USB_drive_voltage > USB_value_min && USB_drive_voltage < USB_value_max && STEP == 1)
    {
      if(analog_counter > 300){
        digitalWrite(drive_out,HIGH);
        digitalWrite(buzzer,HIGH);
        Serial3.print("scr.val=2");
        Serial3.write(0xff);
        Serial3.write(0xff);
        Serial3.write(0xff);
        STEP = 2;
        mp3.playMp3FolderTrack(2);  //Master key accepted
        delay(100);
        digitalWrite(buzzer,LOW);
      }
      analog_counter ++;
      
    }
    else{
      analog_counter = 0;
    }
  }//end step 1

  ////////////STEP 2 RECEIVE CODE OK///////////
  if(STEP == 2){
    if(Serial3.available()){
      Received = Serial3.readString();
    }
    if(Received == "ok" && !code_ok){
      code_ok = true;
      mp3.playMp3FolderTrack(4);  //Inserted code OK. You can now continue
    }
    if(code_ok){
      if(!digitalRead(key_switch)){
        digitalWrite(fingerprint_LEDs,HIGH);
        Serial3.print("page0.scr.val=3");
        Serial3.write(0xff);
        Serial3.write(0xff);
        Serial3.write(0xff);
        STEP = 3;
        mp3.playMp3FolderTrack(5);  //Please scan your fingerpring
        delay(100);
      }
    }

    if(Received == "wrg"){      
      mp3.playMp3FolderTrack(3);  //Wrong inserted code. Please try again. 
      delay(100);
      Received = "";
    }
  }//end step 2

  ////////////STEP 3 FINGERPRINT READ///////////
  if(STEP == 3){
    while(STEP == 3){
      getFingerprintID();
      Serial.println(finger.fingerID);
      if(finger.fingerID == user_ID){
        digitalWrite(fingerprint_LEDs,LOW);
        Serial3.print("page0.scr.val=4");
        Serial3.write(0xff);
        Serial3.write(0xff);
        Serial3.write(0xff);
        STEP = 4;
        mp3.playMp3FolderTrack(6);  //Correct fingerprint. Press the button
      }
    }
  }//end step 3

  ////////////STEP 3 FINGERPRINT READ///////////
  if(STEP == 4){
    if(!digitalRead(push_button)){
      const char *msg = "k";
      driver.send((uint8_t *)msg, strlen(msg));
      driver.waitPacketSent();
      delay(10);
      Serial3.print("page0.scr.val=5");
      Serial3.write(0xff);
      Serial3.write(0xff);
      Serial3.write(0xff);
      STEP = 5;
      mp3.playMp3FolderTrack(7);  //System inicializated
    }
  }

  ////////////STEP 5 All OK, sending radio///////////
  if(STEP == 5){
    if(!digitalRead(push_button) && push_state){
      push_state = false;
      const char *msg = "k";
      driver.send((uint8_t *)msg, strlen(msg));
      driver.waitPacketSent();
      delay(10);
    }

    if(digitalRead(push_button) && !push_state){
      push_state = true;
    }
  }


    
  
    
  

}//end void loop







uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } 
  else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } 
  else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } 
  else {
    Serial.println("Unknown error");
    return p;
  }   
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 

  return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID; 
}













Last tutorials

Escaperoom Puzzle
Raspberry Pi PICO PWM example
Raspberry Pi PICO i2c OLED example
Raspberry Pi PICO ADC + Temperature Example
Upload permanent code to Raspberry Pi PICO

ADVERTISERS



ADVERTISERS

PCBWAY PCB service

PCBWAY PCB service





Arduino curso en español desde cero