Krishna Lalith
Published

Grid Lock Puzzle

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

BeginnerShowcase (no instructions)-60 minutes1,892
Grid Lock Puzzle

Things used in this project

Story

Read more

Schematics

uploads2ftmp2f1b78bf1f-36c3-42e6-80aa-3d8b395131c92fimg_20190317_004724_itT3gHjd1g.jpg

Code

Grid Lock Puzzle

Arduino
#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);
        }
      }
    }
  }
}

Credits

Krishna Lalith
11 projects • 2 followers

Comments