Michael Engel
Published © GPL3+

Safe-deposit box based on Arduino and made of cardboard

Safe-deposit box for children made of 12mm cardboard and reinforced with chipboard. 4 digits code required to open and illuminated with LEDs

IntermediateShowcase (no instructions)Over 1 day1,508
Safe-deposit box based on Arduino and made of cardboard

Things used in this project

Hardware components

ELEGOO Mega 2560 R3 Board Black ATmega2560 ATMEGA16U2
ELEGOO Mega 2560 R3 Board Black ATmega2560 ATMEGA16U2
×1
ILS - 6V DC 1.5A Electric Lock Assembly
×1
Buzzer
Buzzer
×1
AZDelivery 4 Bit Digital Tube LED Display Modul I2C
×1
AZDelivery 4x4 Matrix Array Keypad
×1
Relay (generic)
×1
12mm cardboard
i bought the special cardboard at my local store. didn't found this special 12mm /3 layer cardboard online
×1
MissBirdler Arduino Terminal Adapter
I didn't consider the Terminal Adapter in the schematic.
×1
Resistor 220 ohm
Resistor 220 ohm
×2
LED Stick, NeoPixel Stick
LED Stick, NeoPixel Stick
I used these ones: Sertronics NeoPixel Stick mit 8 WS2812 5050 RGB LEDs
×2
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1

Story

Read more

Schematics

Fritzing Schematics

Code

Safe-deposit-box.ino

Arduino
Code can be copied 1:1. Can be uploaded to Arduino Mega.
//----------------
//Code fuer die finale Version von Antons Tresor
//Spring 2020
//latest update: March 18th 2020
//all functions are working
//----------------

//digitale Pins als VCC5V und GND festelegen - da nicht genuegend GND und 5V/Pins zur Verfuegung stehen
#define VCC5V2    50 //define Pin 50 as VCC5V for RGB strap 1
#define VCC5V3    51  //define Pin 51 as VCC5V for RGB strap 2
#define GND2      48 //define Pin 48 as Ground for RGB strap 1
#define GND3      49 //define Pin 49 as Ground for RGB strap 2
#define VCC5VR1   46  //define Pin 46 as VCC5V for Relay 1
#define GND4      47 //define Pin 47 as Ground for Relay 1

#include <Keypad.h>  //Keypad Bibliothek
#include <Arduino.h> //Einbinden der Arduino Bibliothekt
#include <TM1637Display.h> //Einbinden der Bibliothekt zum ansteuern des Displays

//Define Code for Lock
#define Code_Lenght 4
const byte PINLENGTH = 4;
char pinCode[PINLENGTH+1] = {'3','0','0','9'}; //Antons Code
char pinMaster[PINLENGTH+1] = {'2','0','1','0'}; //MasterCode

// temp buffer for code input 
char keyBuffer[PINLENGTH+1] = {'0','0','0','0'};
 
//counter for counting digits of code
int i=0; 

//Relay digital Output to switch 
const int Lock_Relais_Pin = 12;

//Define digital ports for 7 Segment display
#define CLK 24 //PIN 2 fr den CLK PIN
#define DIO 26 //PIN3 fr den DIO 

//Define size of KeyPad
const byte COLS = 4; //4 col
const byte ROWS = 4; //4 rows

//Die Ziffern und Zeichen des Keypads werden eingegeben:
char hexaKeys[ROWS][COLS]={
{'D','#','0','*'},
{'C','9','8','7'},
{'B','6','5','4'},
{'A','3','2','1'}
};

byte colPins[COLS] = {2,3,4,5}; //Definition der Pins fr die 4 Spalten
byte rowPins[ROWS] = {6,7,8,9};//Definition der Pins fr die 4 Zeilen
char Taste; //Taste ist die Variable fr die jeweils gedrckte Taste.

Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden

const uint8_t done[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
};

const uint8_t offen[] = {
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,    // O
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,            // P
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,            // E
  SEG_C | SEG_E | SEG_G                            // n
};
  
const uint8_t code[] = {
  SEG_A | SEG_D | SEG_E | SEG_F,                   // C
  SEG_C | SEG_D | SEG_E | SEG_G,                   // O
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,            // d
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
};

const uint8_t fail[] = {
  SEG_A | SEG_E | SEG_F | SEG_G,                   // F
  SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
  SEG_E | SEG_F,                                    // I
  SEG_D | SEG_E | SEG_F                             // L
};


//--- display 7-segment initialize
TM1637Display display(CLK, DIO); 
int delayVal = 500;
int pause = 1250;

//-----
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library

//modified for usage on the tresor

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which digital pin on the Arduino is connected to the NeoPixels?
#define PIN_LED_1        10 
#define PIN_LED_2        11

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 8 // Popular NeoPixel ring size

//Status ob RGB-LED an oder aus -> mit Taste * kann Licht an oder aus geschaltet werden
int light_status = 0;

// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// straps you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels_1(NUMPIXELS, PIN_LED_1, NEO_GRB + NEO_KHZ800); //LED strap #1
Adafruit_NeoPixel pixels_2(NUMPIXELS, PIN_LED_2, NEO_GRB + NEO_KHZ800); //LED strap #2

#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels

//-----
//initialize Parameter for Starwars Piezo Song 
//------

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

//-----

/* 
  Nokia Tune
  Connect a piezo buzzer or speaker to pin 11 or select a new pin.
  More songs available at https://github.com/robsoncouto/arduino-songs                                            
                                              
                                              Robson Couto, 2019
*/
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0


// change this to make the song slower or faster
int tempo = 180;

// change this to whichever pin you want to use
int buzzer = 52;

// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int melody[] = {

  // Nokia Ringtone 
  // Score available at https://musescore.com/user/29944637/scores/5266155
  
  NOTE_E5, 8, NOTE_D5, 8, NOTE_FS4, 4, NOTE_GS4, 4, 
  NOTE_CS5, 8, NOTE_B4, 8, NOTE_D4, 4, NOTE_E4, 4, 
  NOTE_B4, 8, NOTE_A4, 8, NOTE_CS4, 4, NOTE_E4, 4,
  NOTE_A4, 2, 
};

// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;

// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

//Difinition vom digitalen Output Buzzer/Piezo 
const int buzzerPin = 52;
 
int counter = 0;
 
void setup() 
{

  //init LED strap #1
  pixels_1.begin(); // INITIALIZE NeoPixel strap object (REQUIRED)
  pixels_1.setBrightness(150);
  
  //init LED strap #2
  pixels_2.begin(); // INITIALIZE NeoPixel strap object (REQUIRED)
  pixels_2.setBrightness(150);

  //Setup pin modes
  pinMode(buzzerPin, OUTPUT);

  //-------
  //making 5V and GND to Digital Pins
  //-------
  pinMode(VCC5V2, OUTPUT);
  digitalWrite(VCC5V2, HIGH);

  pinMode(VCC5V3, OUTPUT);
  digitalWrite(VCC5V3, HIGH);

  pinMode(GND2, OUTPUT);
  digitalWrite(GND2, LOW);

  pinMode(GND3, OUTPUT);
  digitalWrite(GND3, LOW);

  pinMode(VCC5VR1, OUTPUT);
  digitalWrite(VCC5VR1, HIGH);

  pinMode(GND4, OUTPUT);
  digitalWrite(GND4, LOW);


  //define Pin-Mode for relay
  pinMode(Lock_Relais_Pin, OUTPUT);

  //define brightness 0 (low) - 7(high)
  display.setBrightness(7);
 
  uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
  display.setSegments(data);
  delay(delayVal);
   
   // showNumberDec(number, leading_zeros, length, position)
   display.setSegments(code);
    
   Serial.begin(9600);
}

void loop() 
{
 
Taste = Tastenfeld.getKey(); //Mit Unter der Variablen pressedKey entspricht der gedrckten Taste
if (Taste) { //Wenn eine Taste gedrckt wurde
  tone(52, 1000); // Im Hauptteil wird nun mit dem Befehl "tone ( x , y )" ein Ton abgegeben.
  delay(100); // mit einer Dauer von 1 Sekunde
  noTone(52); //
  Serial.print("Die Taste ");
  Serial.print(Taste);
  Serial.print(" wurde gedrueckt");
  Serial.println(); //Teile uns am Serial Monitor die gedrckte Taste mit
   if ((int(Taste) >= 48) && (int(Taste) <= 57)){ 
     addToKeyBuffer(Taste); 
    }
  // oder Code berprfen, falls Raute gedrckt wurde 
    else if (Taste == '#') { 
      checkKey(); 
    } 
    //switch light ON/OFF with pressing "*"
    else if (Taste == '*') {  
      
      if (light_status == 0){
        light_status = 1;
        RGB_strap_one_ON();
        RGB_strap_two_ON();
        Serial.println("RGB light ON");
      }
      else {
        light_status = 0;
        RGB_strap_one_OFF();
        RGB_strap_two_OFF();
        Serial.println("RGB light OFF");
      }
    }
  }
}

void addToKeyBuffer(char inkey) { 
  Serial.print(inkey); 
  Serial.print(" > ");
  // Von links nach rechts Zeichen um eins weiterkopieren = ltestes Zeichen vergessen
  for (int i=1; i<PINLENGTH; i++) {
    keyBuffer[i-1] = keyBuffer[i];
  }
  // in ganz rechter Stelle die letzte Ziffer speichern
  keyBuffer[PINLENGTH-1] = inkey;
  
  display.showNumberDec(atoi(keyBuffer)); 
  Serial.println("keyBuffer");
  Serial.println(keyBuffer);
  Serial.println("Code:");
  Serial.println(atoi(keyBuffer));
}
 
void checkKey() {
  // Eingabe mit festgelegtem Pincode vergleichen
  if (strcmp(keyBuffer, pinCode) == 0) { //mit dem Anton's-Code vergleichen
    Serial.println("CORRECT");
    // action if PIN Code OK
    pinCorrect();
  }
   else if (strcmp(keyBuffer, pinMaster) == 0) { //mit dem Master-Code vergleichen
    Serial.println("CORRECT");
    // action if PIN Code OK
    pinCorrect();
  }
  else {
    Serial.println("WRONG!");
    // action if PIN Code WRONG
    pinWrong();
  }
 
  // delete INPUT
  for (int i=0; i < PINLENGTH; i++) {
    keyBuffer[i] = '-'; 
    display.setSegments(code);
  }
}
 
  // Procedure for PIN code OK
  void pinCorrect() {
  //print "OPEN" on the 7 segment
  display.setSegments(offen);

  //open magnet via relay
  digitalWrite(Lock_Relais_Pin, HIGH);
  //delay(5000);
 
  
  //Play first section
  firstSection();
  
  //close magnet
  digitalWrite(Lock_Relais_Pin, LOW); 
  
   //RGB-LEDs anschalten
  RGB_strap_one_ON();
  RGB_strap_two_ON();
 
  //Play second section
   secondSection();
 
  //Variant 1
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 350);  
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);  
  beep(cH, 125);
  beep(eH, 650);
 
  delay(500);
 
  //Repeat second section
 secondSection();
 
  //Variant 2
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 650);  
 
  delay(650);

   //Licht ausschalten 
    //hab auf die Schnelle keine bessere Loesung gefunden einen Trigger zum Ausschalten des Lichts zu nehmen
    RGB_strap_one_OFF();
    RGB_strap_two_OFF();
}
 
// Aktion fr falschen Pincode
void pinWrong() {

  //Anzeigen "OPEN" auf der 7
  display.setSegments(fail);
  
  //Abspielen Nokia Klingelton
  // iterate over the notes of the melody.
  // Remember, the array is twice the number of notes (notes + durations)
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    // calculates the duration of each note
    divider = melody[thisNote + 1];
    if (divider > 0) {
      // regular note, just proceed
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      // dotted notes are represented with negative durations!!
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes
    }

    // we only play the note for 90% of the duration, leaving 10% as a pause
    tone(buzzerPin, melody[thisNote], noteDuration * 0.9);

    // Wait for the specief duration before playing the next note.
    delay(noteDuration);

    // stop the waveform generation before the next note.
    noTone(buzzerPin);
  }
}

//Funktion schaltet den RGB-Riegel 1 (oberes Fach) ON
void RGB_strap_one_ON()
{
  pixels_1.clear(); // Set all pixel colors to 'off'
  for(int i=0; i<NUMPIXELS; i++) {  
     pixels_1.setPixelColor(i, pixels_1.Color(0, 200,0)); //GREEN
  }
  pixels_1.show();   // Send the updated pixel colors to the hardware.  
}

//Funktion schaltet den RGB-Riegel 2 (unteres Fach) ON
void RGB_strap_two_ON()
{
  pixels_2.clear(); // Set all pixel colors to 'off'
  for(int i=0; i<NUMPIXELS; i++) {  
     pixels_2.setPixelColor(i, pixels_2.Color(200, 0,0)); //GREEN
  }
  pixels_2.show();   // Send the updated pixel colors to the hardware.     
}

//Funktion schaltet den RGB-Riegel 1 (oberes Fach) OFF
void RGB_strap_one_OFF()
{
  for(int i=0; i<NUMPIXELS; i++) {  
     pixels_1.setPixelColor(i, pixels_1.Color(0, 0,0)); //OFF
  }
  pixels_1.show();
  pixels_1.clear(); // Set all pixel colors to 'off'  
}

//Funktion schaltet den RGB-Riegel 2 (unteres Fach) OFF
void RGB_strap_two_OFF()
{
  for(int i=0; i<NUMPIXELS; i++) {  
     pixels_2.setPixelColor(i, pixels_2.Color(0, 0,0)); //OFF
  }
  pixels_2.show();
  pixels_2.clear(); // Set all pixel colors to 'off'  
}


void beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
      delay(duration); 
  }
  else
  {
      delay(duration);
  }
 
  //Stop tone on buzzerPin
  noTone(buzzerPin);
 
  delay(50);
 
  //Increment counter
  counter++;
}
 
void firstSection()
{
  beep(a, 500);
  beep(a, 500);    
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);  
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
 
  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);  
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
}
 
void secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);    
  beep(fSH, 250);
 
  delay(325);
 
  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);  
  beep(cSH, 175);  
  beep(cH, 125);  
  beep(b, 125);  
  beep(cH, 250);  
 
  delay(350);
}

Credits

Michael Engel

Michael Engel

3 projects • 1 follower
originally studied computer science; started programming games on U880; programming iOS apps; since a couple of weeks sticked into Arduino

Comments