Project showcase
Grid Lock Puzzle

Grid Lock Puzzle

Unlock each of 16 (4x4) locks to crack the bigger lock by flipping the states either horzontally or vertically.

  • 810 views
  • 1 comment
  • 3 respects

Components and supplies

About this project

Unlock each of 16 (4x4) locks to crack the bigger lock. Associated 4x4 buttons can be used to flip the state either horzontally or vertically. Note either complete row or column of LEDs will flip its state not individual.

Code

Grid Lock PuzzleArduino
#include <Keypad.h>                                                           //keypad library
#include <LedControl.h>

const byte ROWS = 4; //four rows         ;                                    //declare rows
const byte COLS = 4; //four columns                                           //declare columns
char keys[ROWS][COLS] = {                                                     //declare 2 dimensional array to map keys
  {'M', 'N', 'O', 'P'},
  {'I', 'J', 'K', 'L'},
  {'E', 'F', 'G', 'H'},
  {'A', 'B', 'C', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};                                            //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9};                                            //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );     //create instance of Keypad called keypad


int DIN = 12;
int CS =  11;
int CLK = 10;
LedControl lc = LedControl(DIN, CLK, CS, 0);

int FIVER[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
int r, c, i, j;
int flag = 1;


void setup() {
  Serial.begin(9600);
  lc.shutdown(0, false);      //The MAX72XX is in power-saving mode on startup
  lc.setIntensity(0, 1);      // Set the brightness to maximum value
  lc.clearDisplay(0);         // and clear the display
  randomSeed(analogRead(0));
}

void loop() {
  if (flag == 1) {
    for (i = 0; i < 4; i++) {
      for (j = 0; j < 4; j++) {
        FIVER[i][j] = random(0, 2);
      }
    }

    flag = 0;

    for (i = 0; i < 4; i++) {
      for (j = 0; j < 4; j++) {
        if (FIVER[i][j] == 1) {
          lc.setLed(0, 2 * i, 2 * j , true);
          lc.setLed(0, 2 * i + 1, 2 * j , true);
          lc.setLed(0, 2 * i, 2 * j + 1 , true);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , true);
        }
        if (FIVER[i][j] == 0) {
          lc.setLed(0, 2 * i, 2 * j , false);
          lc.setLed(0, 2 * i + 1, 2 * j , false);
          lc.setLed(0, 2 * i, 2 * j + 1 , false);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , false);
        }
      }
    }
  }

  char key = keypad.getKey();                                                 //use keypad.getKey function to
  //to define the value of Char variable key

  if (key == 'A') {
    r = 0;
    c = 0;
  }
  if (key == 'B') {
    r = 0;
    c = 1;
  }
  if (key == 'C') {
    r = 0;
    c = 2;
  }
  if (key == 'D') {
    r = 0;
    c = 3;
  }
  if (key == 'E') {
    r = 1;
    c = 0;
  }
  if (key == 'F') {
    r = 1;
    c = 1;
  }
  if (key == 'G') {
    r = 1;
    c = 2;
  }
  if (key == 'H') {
    r = 1;
    c = 3;
  }
  if (key == 'I') {
    r = 2;
    c = 0;
  }
  if (key == 'J') {
    r = 2;
    c = 1;
  }
  if (key == 'K') {
    r = 2;
    c = 2;
  }
  if (key == 'L') {
    r = 2;
    c = 3;
  }
  if (key == 'M') {
    r = 3;
    c = 0;
  }
  if (key == 'N') {
    r = 3;
    c = 1;
  }
  if (key == 'O') {
    r = 3;
    c = 2;
  }
  if (key == 'P') {
    r = 3;
    c = 3;
  }

  if (key) {
    for (i = 0; i < 4; i++) {
      FIVER[i][c] = 1 - FIVER[i][c];
    }
    for (i = 0; i < 4; i++) {
      FIVER[r][i] = 1 - FIVER[r][i];
    }
    FIVER[r][c] = 1 - FIVER[r][c];

    for (i = 0; i < 4; i++) {
      for (j = 0; j < 4; j++) {
        if (FIVER[i][j] == 1) {
          lc.setLed(0, 2 * i, 2 * j , true);
          lc.setLed(0, 2 * i + 1, 2 * j , true);
          lc.setLed(0, 2 * i, 2 * j + 1 , true);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , true);
        }
        if (FIVER[i][j] == 0) {
          lc.setLed(0, 2 * i, 2 * j , false);
          lc.setLed(0, 2 * i + 1, 2 * j , false);
          lc.setLed(0, 2 * i, 2 * j + 1 , false);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , false);
        }
      }
    }
  }
}

Schematics

uploads2ftmp2f1b78bf1f-36c3-42e6-80aa-3d8b395131c92fimg_20190317_004724_itT3gHjd1g.jpg
Uploads2ftmp2f1b78bf1f 36c3 42e6 80aa 3d8b395131c92fimg 20190317 004724 itt3ghjd1g

Comments

Similar projects you might like

Turning Lock Puzzle

Project showcase by Krishna Lalith

  • 910 views
  • 0 comments
  • 0 respects

Arduino Keyless Door Lock System with Keypad and LCD

Project tutorial by DIY Hacking

  • 32,760 views
  • 19 comments
  • 59 respects

Ultrasonic Lock

by IoannisD

  • 5,078 views
  • 2 comments
  • 8 respects

Intelligent Door Lock

Project in progress by Md. Khairul Alam

  • 24,553 views
  • 24 comments
  • 117 respects

Password Based Door Lock

Project tutorial by Rishabh

  • 16,597 views
  • 6 comments
  • 30 respects

Smart BT Door Lock (Simple Arduino)

Project showcase by Arun

  • 12,762 views
  • 1 comment
  • 23 respects
Add projectSign up / Login