PROJECT Nº 2: Safe

Learn to build and program a safe using the components of the Maker Control Kit and Maker Kit 3.

Use the servomotor and two buttons to build a safe that will only open when a secret combination of buttons is entered.

DIFFICULTY LEVEL: Intermediate.

DURATION OF THE EXERCISE: 60 min.

MATERIALS:

  • 1 Servomotor
  • 2 Buttons
  • 1 Cardboard box
  • 1 4in1 Build&Code board
  • 1 USB – Micro USB cable
  • Computer
  • Material to create the lock
  • Adhesive or tape 

What is a button?

A button or switch is an electric operator that, when pushed, allows electric current to flow, and when it is no longer pressed, interrupts it.

BUILDING THE STRUCTURE:

You will use poster board to make the lock and a cardboard box to make the safe. To join different parts, you will use tape and screws.

To build the structure, download the lock stencil and the quick assembly guide. Follow the steps provided:

  1. Glue the lock stencil onto the poster board and use the lines as guides to cut out the cardboard.
  2. Make a hole in the box to insert the servomotor and another hole to insert the USB cable. Fasten the lock and the rest of the electronic components to the box.

Once the structure is assembled, put the 4in1 Build&Code board inside the box and connect all the electronic components following the indications in the section on connections found below.

 CONNECTIONS:

  1. Connect button 1 to digital port 4 on the 4in1 Build&Code board.
  2. Connect button 2 to digital port 5 on the 4in1 Build&Code board.
  3. Connect the servomotor to digital port 9 on the 4in1 Build&Code board.

Look at the colors of the cables and the colors of the terminals on the 4in1 Build&Code board to guide you. Each cable should be connected to its color.

PROGRAMMING CODE:

The program is comprised of two parts:

  1. Reading and counting the pulses of each button.
  2. Comparing the number of pulses of each button to the secret code entered in the program.

To make the system more secure, there is one way to enter the secret code. First, the first number must be entered with the button connected to digital port 4, and then the second number must be entered with the button connected to digital port 5. If they are entered backwards, the lock will not open and the account will have to be reset.

If the code entered is correct, the servomotor will move 90º, opening the lock.

To reset the whole system and close the lock, you will push the two buttons at the same time and the servomotor will position itself at 15º, closing the lock. The system of counting the button pushes will be set to zero. This way, the system will be ready for the code to be entered again.

You can do this activity by using Arduino and Bitbloq software, as well as other compatible block programming software. Below you will find the programming code needed for each software.

Arduino Code

  1. Download the Arduino software and go through the installation process.
  2. Open the Arduino program, and once in, copy the following program:
    #include <Servo.h> // Servomotor library
    Servo motor1; // Name of the servomotor is motor1
    int VButton1, VButton2; // Variables for button 1 and button 2
    int Button1 = 4, Button2 = 5; // Connection ports of the buttons, digital ports 4 and 5
    int CODE1 = 2; // 1st number of the secret code is 2
    int CODE2 = 5; // 2nd number of the secret code is 5
    int X1 = 0; // Variables to save the number of button pushes
    int X2 = 0;
    int X3 = 0;
    
    void setup() {
      // Put your setup code here, to run once:
      motor1.attach (9); // Servomotor connected to digital port 9
      motor1.write (15); // Servomotor at 15º
      pinMode (Button1, INPUT); // Configuration of the buttons as input signal
      pinMode (Button2, INPUT);
      pinMode (13, OUTPUT); // Configuration of port 13 as output
      Serial.begin(9600); // Configuration of communication by serial port of the buttons
      Serial.print ("Button1");
      Serial.print ("\t");
      Serial.println ("Button2");
    }
    
    void loop() {
      // Put your main code here, to run repeatedly:
      VButton1 = digitalRead (Button1); // Status reading of button 1, pushed/not pushed
      VButton2 = digitalRead (Button2); // Status reading of button 2, pushed/not pushed
      if(VButton1 == 1) // If button 1 is pushed 
      {
        X1 = X1+1; // X1 adds +1
        delay(300); // Wait 300 ms
      }
      if(VButton2 == 1) // If button 2 is pushed
      {
        X2 = X2+1; // X2 adds +1
        delay(300); // Wait 300 ms
      }
      if((X1 == CODE1) && (X2 == 0) && (X3 == 0)) // If X1 is equal to CODE1 and X2 = 0 and X3 = 0
      {
        X3 = 1; // X3 = 1
      }
      if((X2 == CODE2) && (X3 == 1) // If X2 is equal to CODE2 and X3 = 1
      {
        digitalWrite (13, HIGH); // Board LED on 
        motor1.write (90); // Servomotor at 90º
      }
      if ((VButton1 == 1) && (VButton2 == 1)) // If button 1 and button 2 are pushed at the same time
      {
        X1 = 0; // X1, X2 and X3 to 0
        X2 = 0;
        X3 = 0;
        digitalWrite (13, LOW); // Board LED off
        motor1.write (15); // Servomotor at 15º
      }  
      Serial.print (X1); // Show the value of X1 onscreen
      Serial.print ("\t");
      Serial.printIn (X2); // Show the value of X2 onscreen
    }
    
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

Code for compatible block programming software

  1. Download the software and go through the installation process.
  2. Open the program, and once in, copy the following code:
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

BitBloq Code

  1. Get the BitBloq software.
  2. Open the BitBloq program, and once in, copy the following code:
    • Hardware
    • Software
  3. Configure and load the code, following the instructions given in the document First Steps for the 4in1 Build&Code board.

RESULT OF THE EXERCISE:

The lock works with the servomotor, and entering the secret number is done by using the buttons. When the correct combination of button pushes is entered, the servomotor will move 90º, opening the lock. By pushing both buttons at once, the servomotor will be positioned at 15º, closing the lock. You have made your own safe!

1 0

0