Security Gaming Box (If This Then That)

Published

Introduction: Security Gaming Box (If This Then That)

Intro


For this project I choose something that I wanted to have for some time, but decided to create it myself and trying to mix it with another hobby of mine. This tool functions as a security box to store your phone or any toy that you want to hide to get less addicted. As lock a simple Run and Jump game was added. So I added security (former eduation) and gaming (hobby) into one tool! By adding simple buttons, a LCD screen on a box and some paint the Security gaming box was created.

Credits for the code goes to Iron_Salsa(https://create.arduino.cc/projecthub/iron_salsastudio/lcd-game-2e69ea) After this I made some alterations.

Step 1: Shopping List

Step 1: The Materials

These are the materials that were used. Be creative, these are the one I used and be customized to your own needs.

- An Arduino Uno

- LCD scherm (20x4) (this is the one I used)

- L2C shield for the LCD

- 3 Push Buttons

- 11 wires (Male-Female)

- Empty shield (used to solder with)

- usb cord

Extra

- The case (fit to your own needs)

- Any kind of box (in this case a carton box was used)

- Screws

- Paint/brushes (any size and type of paint)

- Glue (I used super glue, bad choice)

- Utility Knife and/or Scissors

- Rubber Push buttons (I used a gamepad and removed them)

Step 2: Setting Up the Arduino

Step 2: The Circuit

The Scheme will show where to connect the wires. There are a few to connect and several have to be soldered to the push buttons.

  • To reset game = GND + Reset
  • To Choose Difficulty = GND + pin 4
  • To jump = GND + pin 2

You can use any breadboard or (soldered) shield that is connected to the Arduino

For the LCD screen

You can choose a LCD that comes with a build on L2C shield or separately. For this part we’ll using male- female wires. With this you can immediatly connect the wires from the shield to the Arduino. There will be some cluttering with the wires as they’re very close to each other.

If these are put in different pins, the lighting of the LCD screen could be less bright.

  • SCL = A5
  • SDA = A4
  • GND = GND
  • VCC = 5V

Once you connected and tested every wire, you can start to solder the wires to the shield.

A small tip is to use solder on the other side of the shield to avoid collision of the wires with the buttons.

Step 3: The Case

The casing, box or chest
chest or any kind of object can be used to mount the Arduino and LCD on.

This can be made any way you prefer. I simply take a shoe like box and use the lit to set the base for the shield and LCD screen. Some holes are cut out and the inside of the box is used to glue and screw everything together. Some paint to hide glued/screwed on carton pieces that were used to hold the shield and Arduino.

Step 4: Setting Up the Code

For the code additional libraries have to be downloaded. Two are needed to use the LCD screen and shield. These will be delivered alongside the code used for this Arduino.

The text is easily changed and can be adjust to own needs.

#include <wire.h><br>
#include <lcd.h>
#include <liquidcrystal_i2c.h>


volatile int write;
volatile int inscription;
volatile int submenu;
#define PIN_BUTTON 2
#define PIN_AUTOPLAY 1
#define PIN_READWRITE 10
#define PIN_CONTRAST 12
#define SPRITE_RUN1 1
#define SPRITE_RUN2 2
#define SPRITE_JUMP 3
#define SPRITE_JUMP_UPPER ' '         
#define SPRITE_JUMP_LOWER 4
#define SPRITE_TERRAIN_EMPTY ' '      
#define SPRITE_TERRAIN_SOLID 5
#define SPRITE_TERRAIN_SOLID_RIGHT 6
#define SPRITE_TERRAIN_SOLID_LEFT 7
#define HERO_HORIZONTAL_POSITION 1    
#define TERRAIN_WIDTH 20
#define TERRAIN_EMPTY 0
#define TERRAIN_LOWER_BLOCK 1
#define TERRAIN_UPPER_BLOCK 2
#define HERO_POSITION_OFF 0          
#define HERO_POSITION_RUN_LOWER_1 1  
#define HERO_POSITION_RUN_LOWER_2 2  
#define HERO_POSITION_JUMP_1 3       
#define HERO_POSITION_JUMP_2 4       
#define HERO_POSITION_JUMP_3 5       
#define HERO_POSITION_JUMP_4 6     
#define HERO_POSITION_JUMP_5 7    
#define HERO_POSITION_JUMP_6 8       
#define HERO_POSITION_JUMP_7 9       
#define HERO_POSITION_JUMP_8 10      
#define HERO_POSITION_RUN_UPPER_1 11 
#define HERO_POSITION_RUN_UPPER_2 12 </liquidcrystal_i2c.h></lcd.h></wire.h></p><p>LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
static char terrainUpper[TERRAIN_WIDTH + 1];
static char terrainLower[TERRAIN_WIDTH + 1];
static bool buttonPushed = false;
char selection;
char item_selection;</p><p>//How the character and enviroment will look like in bytes
void initializeGraphics(){
  static byte graphics[] = {
    // Run position 1
    B00000,
    B10000,
    B10010,
    B10011,
    B11111,
    B01110,
    B11010,
    B10011,
    // Run position 2
    B00000,
    B10000,
    B10001,
    B10011,
    B11111,
    B01110,
    B01100,
    B01110,
    // Jump
    B11000,
    B10001,
    B10011,
    B10111,
    B11111,
    B11011,
    B10001,
    B00000,
    // Jump lower
    B10111,
    B11111,
    B11011,
    B10001,
    B00000,
    B00000,
    B00000,
    B00000,
    // Ground
    B00100,
    B00100,
    B00100,
    B00100,
    B00100,
    B11111,
    B00100,
    B00100,
    // Ground right
    B00000,
    B00000,
    B00000,
    B00000,
    B00000,
    B00001,
    B00000,
    B00000,
    // Ground left
    B00000,
    B00000,
    B00000,
    B00000,
    B00000,
    B10000,
    B00000,
    B00000,
  };
  int i;
  
 
  for (i = 0; i < 7; ++i) {
    lcd.createChar(i + 1, &graphics[i * 8]);
  }
  for (i = 0; i < TERRAIN_WIDTH; ++i) {
    terrainUpper[i] = SPRITE_TERRAIN_EMPTY;
    terrainLower[i] = SPRITE_TERRAIN_EMPTY;
  }
}</p><p>void advanceTerrain(char* terrain, byte newTerrain){
  for (int i = 0; i < TERRAIN_WIDTH; ++i) {
    char current = terrain[i];
    char next = (i == TERRAIN_WIDTH-1) ? newTerrain : terrain[i+1];
    switch (current){
      case SPRITE_TERRAIN_EMPTY:
        terrain[i] = (next == SPRITE_TERRAIN_SOLID) ? SPRITE_TERRAIN_SOLID_RIGHT : SPRITE_TERRAIN_EMPTY;
        break;
      case SPRITE_TERRAIN_SOLID:
        terrain[i] = (next == SPRITE_TERRAIN_EMPTY) ? SPRITE_TERRAIN_SOLID_LEFT : SPRITE_TERRAIN_SOLID;
        break;
      case SPRITE_TERRAIN_SOLID_RIGHT:
        terrain[i] = SPRITE_TERRAIN_SOLID;
        break;
      case SPRITE_TERRAIN_SOLID_LEFT:
        terrain[i] = SPRITE_TERRAIN_EMPTY;
        break;
    }
  }
}
void gl (int time, int glos){</p><p>}</p><p>bool drawHero(byte position, char* terrainUpper, char* terrainLower, unsigned int score) {
  bool collide = false;
  char upperSave = terrainUpper[HERO_HORIZONTAL_POSITION];
  char lowerSave = terrainLower[HERO_HORIZONTAL_POSITION];
  byte upper, lower;
  switch (position) {
    case HERO_POSITION_OFF:
      upper = lower = SPRITE_TERRAIN_EMPTY;
      break;
    case HERO_POSITION_RUN_LOWER_1:
      upper = SPRITE_TERRAIN_EMPTY;
      lower = SPRITE_RUN1;
      break;
    case HERO_POSITION_RUN_LOWER_2:
      upper = SPRITE_TERRAIN_EMPTY;
      lower = SPRITE_RUN2;
      break;
    case HERO_POSITION_JUMP_1:
    case HERO_POSITION_JUMP_8:
      upper = SPRITE_TERRAIN_EMPTY;
      lower = SPRITE_JUMP;
      break;
    case HERO_POSITION_JUMP_2:
    case HERO_POSITION_JUMP_7:
      upper = SPRITE_JUMP_UPPER;
      lower = SPRITE_JUMP_LOWER;
      break;
    case HERO_POSITION_JUMP_3:
    case HERO_POSITION_JUMP_4:
    case HERO_POSITION_JUMP_5:
    case HERO_POSITION_JUMP_6:
      upper = SPRITE_JUMP;
      lower = SPRITE_TERRAIN_EMPTY;
      break;
    case HERO_POSITION_RUN_UPPER_1:
      upper = SPRITE_RUN1;
      lower = SPRITE_TERRAIN_EMPTY;
      break;
    case HERO_POSITION_RUN_UPPER_2:
      upper = SPRITE_RUN2;
      lower = SPRITE_TERRAIN_EMPTY;
      break;
  }
  if (upper != ' ') {
    terrainUpper[HERO_HORIZONTAL_POSITION] = upper;
    collide = (upperSave == SPRITE_TERRAIN_EMPTY) ? false : true;
    
  }
  if (lower != ' ') {
    terrainLower[HERO_HORIZONTAL_POSITION] = lower;
    collide |= (lowerSave == SPRITE_TERRAIN_EMPTY) ? false : true;
  }
  
  byte digits = (score > 9999) ? 5 : (score > 999) ? 4 : (score > 99) ? 3 : (score > 9) ? 2 : 1;
  
  // Draw the scene
  terrainUpper[TERRAIN_WIDTH] = '\0';
  terrainLower[TERRAIN_WIDTH] = '\0';
  char temp = terrainUpper[20-digits];
  terrainUpper[20-digits] = '\0';
  lcd.setCursor(0,1);
  lcd.print(terrainUpper);
  terrainUpper[20-digits] = temp;  
  lcd.setCursor(0,3);
  lcd.print(terrainLower);
  
  lcd.setCursor(20 - digits,0);
  lcd.print(score);</p><p>  terrainUpper[HERO_HORIZONTAL_POSITION] = upperSave;
  terrainLower[HERO_HORIZONTAL_POSITION] = lowerSave;
  return collide;
}</p><p>// hanteerd de knop als een drukknop en onderbreker
void buttonPush() {
  buttonPushed = true;
  
}
/////////////////////////////////////Intro text screen////////////////////////////
void beginning(){
  lcd.setCursor(0,0);
  lcd.print("--------------------");
  lcd.setCursor(5,1);
  lcd.print("Run and jump");
  lcd.setCursor(0,3);
  lcd.print("--------------------");
  
  gl(100,400);
  gl(100,300);
  gl(100,200);
  gl(100,300);
  gl(100,400);
  gl(100,600);
  delay(3000);
  lcd.clear();</p><p>  lcd.setCursor(0,0);
  lcd.print("--------------------");
  lcd.setCursor(8,1);
  lcd.print("Made by");
  lcd.setCursor(2,2);
  lcd.print("Frans van Swam");
  lcd.setCursor(0,3);
  lcd.print("--------------------");
  
  gl(100,400);
  gl(100,300);
  gl(100,200);
  gl(100,300);
  gl(100,400);
  gl(100,600);
  delay(3000);
  lcd.clear();</p><p>  lcd.setCursor(0,0);
  lcd.print("--------------------");
  lcd.setCursor(4,1);
  lcd.print("Try and get");
  lcd.setCursor(4,2);
  lcd.print("150 points!");
  lcd.setCursor(0,3);
  lcd.print("--------------------");
  
  gl(100,400);
  gl(100,300);
  gl(100,200);
  gl(100,300);
  gl(100,400);
  gl(100,600);
  delay(3000);
  lcd.clear();</p><p>  lcd.setCursor(0,0);
  lcd.print("--------------------");
  lcd.setCursor(3,1);
  lcd.print("Once you have");
  lcd.setCursor(3,2);
  lcd.print("enough points");
  lcd.setCursor(0,3);
  lcd.print("--------------------");</p><p>  gl(100,400);
  gl(100,300);
  gl(100,200);
  gl(100,300);
  gl(100,400);
  gl(100,600);
  delay(3000);
  lcd.clear();</p><p>  lcd.setCursor(0,0);
  lcd.print("--------------------");
  lcd.setCursor(1,1);
  lcd.print("You collect your");
  lcd.setCursor(2,2);
  lcd.print("toys in the chest");
  lcd.setCursor(0,3);
  lcd.print("--------------------");</p><p>  gl(100,400);
  gl(100,300);
  gl(100,200);
  gl(100,300);
  gl(100,400);
  gl(100,600);
  delay(3000);
  lcd.clear();
}  </p><p>void game(int level){
  static byte heroPos = HERO_POSITION_RUN_LOWER_1;
  static byte newTerrainType = TERRAIN_EMPTY;
  static byte newTerrainDuration = 1;
  static bool playing = false;
  static bool blink = false;
  static unsigned int distance = 0;
  if (!playing) {
    drawHero((blink) ? HERO_POSITION_OFF : heroPos, terrainUpper, terrainLower, distance >> 3);
    if (blink) {
      lcd.clear();
      lcd.setCursor(3,1);
      lcd.print("Try again! ");
    }
    delay(500);
    blink = !blink;
    if (buttonPushed) {
      gl(100,400);
      gl(100,600);
      gl(100,400);
      initializeGraphics();
      heroPos = HERO_POSITION_RUN_LOWER_1;
      playing = true;
      buttonPushed = false;
      distance = 0;
      
    }
    return;
  }</p><p>  // Shift the terrain to the left
  advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
  advanceTerrain(terrainUpper, newTerrainType == TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
  
  // Make new terrain to enter on the right
  if (--newTerrainDuration == 0) {
    if (newTerrainType == TERRAIN_EMPTY) {
      newTerrainType = (random(3) == 0) ? TERRAIN_UPPER_BLOCK : TERRAIN_LOWER_BLOCK;
      newTerrainDuration = 2 + random(10);
    } else {
      newTerrainType = TERRAIN_EMPTY;
      newTerrainDuration = 10 + random(10);
    }
  }
    
  if (buttonPushed) {
    
    if (heroPos <= HERO_POSITION_RUN_LOWER_2)
    gl(100,300);
    heroPos = HERO_POSITION_JUMP_1;
    buttonPushed = false;
    
  }  </p><p>  if (drawHero(heroPos, terrainUpper, terrainLower, distance >> 3)) {
    playing = false; // Speler collide
    gl(100,500);
    gl(100,400);
    gl(120,300);
    gl(160,400);
    lcd.clear();
  } else {
    if (heroPos == HERO_POSITION_RUN_LOWER_2 || heroPos == HERO_POSITION_JUMP_8) {
      heroPos = HERO_POSITION_RUN_LOWER_1;
    } else if ((heroPos >= HERO_POSITION_JUMP_3 && heroPos <= HERO_POSITION_JUMP_5) && terrainLower[HERO_HORIZONTAL_POSITION] != SPRITE_TERRAIN_EMPTY) {
      heroPos = HERO_POSITION_RUN_UPPER_1;
    } else if (heroPos >= HERO_POSITION_RUN_UPPER_1 && terrainLower[HERO_HORIZONTAL_POSITION] == SPRITE_TERRAIN_EMPTY) {
      heroPos = HERO_POSITION_JUMP_5;
    } else if (heroPos == HERO_POSITION_RUN_UPPER_2) {
      heroPos = HERO_POSITION_RUN_UPPER_1;
    } else {
      ++heroPos;
    }
    ++distance;
    
    digitalWrite(PIN_AUTOPLAY, terrainLower[HERO_HORIZONTAL_POSITION + 2] == SPRITE_TERRAIN_EMPTY ? HIGH : LOW);
  }
  delay(level);
  }
  
void setup(){
  pinMode(4, INPUT_PULLUP);
 
  pinMode(PIN_BUTTON, INPUT);
  digitalWrite(PIN_BUTTON, HIGH);
  inscription = 1;</p><p>  attachInterrupt(0/*PIN_BUTTON*/, buttonPush, FALLING);
  
  initializeGraphics();
  
  lcd.begin(20, 4);
   beginning();
   do{menu();}while(digitalRead(2) == HIGH);</p><p>   
}</p><p>void loop() { 
switch (write) {
case 1:
game(70);//increases speed 70x
break;
case 2:
game(50);//increases speed 50x
break;
case 3:
game(15);//increases speed 15x
break;
case 4:
game(1);//speed is normal
break;
    }
  }</p><p>// Menu too chose dificulty
void menu() {
  
if (inscription > 4 || inscription < 0) {inscription == 0;}
if (digitalRead(4) == LOW) { inscription++;}
  switch (inscription) {</p><p>    case 1:
    lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(6,2);
  lcd.print("<easy>");
  delay(500);
  lcd.clear();
  lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(6,2);
  lcd.print("<     >");
  delay(500);
  if(digitalRead(2) == LOW) {
    write = 1;
    write;}
  
  break;
    case 2:
lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(5,2);
  lcd.print("<normal>");
  delay(500);
  lcd.clear();
  lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(2,2);
  lcd.print("<          >");
  delay(500);
if(digitalRead(2) == LOW) {
    write = 2;
    write;}
    break;</normal></easy></p><p>    case 3:
lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(6,2);
  lcd.print("<hard> ");
  delay(500);
  lcd.clear();
  lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(6,2);
  lcd.print("<     > ");
  delay(500);
  if(digitalRead(2) == LOW) {
    write = 3;
    write;}</hard></p><p>    break;
    case 4:
lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(5,2);
  lcd.print("<very hard="">");
  delay(500);
  lcd.clear();
  lcd.setCursor(5,1);
  lcd.print("Dificulty:");
  lcd.setCursor(5,2);
  lcd.print("<         >");
  delay(500);
  if(digitalRead(2) == LOW) {
    write = 4;
    write;}
    break;
    
  default:
inscription = 0;
  break;
    }
  }</very></p>

Step 5: Video and Neck Exercise

Share

    Recommendations

    • Make it Move Contest

      Make it Move Contest
    • Microcontroller Contest

      Microcontroller Contest
    • Woodworking Contest

      Woodworking Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments