mdraber
Created December 20, 2020 © GPL3+

Maze Lock created with MAX7219 LED Matrix

Open the magnetic lock by solving the simple maze puzzle

IntermediateFull instructions provided213
Maze Lock created with MAX7219 LED Matrix

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
MAXREFDES99# MAX7219 Display Driver Shield
Maxim Integrated MAXREFDES99# MAX7219 Display Driver Shield
×1
Relay Module (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Diagram

Code

Maze Lock

Arduino
//Mario's Ideas
//Maze Lock

#include <LedControl.h>
int cursor_row= 2;
int cursor_col = 0;
int Direction =1;

int UP_LED = 6;
int DOWN_LED = 8;
int RIGHT_LED = 4;
int LEFT_LED = 12;

int DIN = 11;
int CS = 7;
int CLK = 13;

long Timestamp_Button_Pressed;


LedControl lc=LedControl(DIN, CLK, CS,0);

int CurrentState [8] [8] {
  {0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0},
  {1,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0},
  {0,0,0,0,0,0,0,0}
};

int Move_up [8] [8] {
  {0,0,0,0,0,0,0,0},
  {1,1,1,0,1,0,1,1},
  {1,1,1,1,0,0,0,1},
  {0,1,1,1,0,0,0,0},
  {1,0,1,1,0,1,1,1},
  {1,1,0,0,0,1,1,1},
  {1,0,0,0,0,1,0,1},
  {0,0,1,0,0,0,0,1}
};

int Move_down [8] [8] {
  {1,1,1,0,1,0,1,1},
  {1,1,1,1,0,0,0,1},
  {0,1,1,1,0,0,0,0},
  {1,0,1,1,0,1,1,1},
  {1,1,0,0,0,1,1,1},
  {1,0,0,0,0,1,1,1},
  {0,0,1,0,0,0,0,1},
  {0,0,0,0,0,0,0,0}
};

int Move_left [8] [8] {
  {0,1,0,0,1,1,0,1},
  {0,0,0,0,1,1,1,0},
  {0,0,1,0,0,1,1,1},
  {0,1,0,0,1,1,0,1},
  {0,0,1,0,1,0,0,0},
  {0,0,1,1,1,0,0,0},
  {0,1,1,1,1,1,1,1},
  {0,1,1,1,1,1,1,0}
};

int Move_right [8] [8] {
  {1,0,0,1,1,0,1,0},
  {0,0,0,1,1,1,0,1},
  {0,1,0,0,1,1,1,0},
  {1,0,0,1,1,0,1,0},
  {0,1,0,1,0,0,0,0},
  {0,1,1,1,0,0,0,0},
  {1,1,1,1,1,1,1,0},
  {1,1,1,1,1,1,0,0}
};

void setup() {
 
   attachInterrupt(0,Change_direction, RISING);
   attachInterrupt(1,Move_in_the_maze, RISING);
   
   pinMode(UP_LED, OUTPUT);
   pinMode(DOWN_LED, OUTPUT);
   pinMode(RIGHT_LED, OUTPUT);
   pinMode(LEFT_LED, OUTPUT);
   pinMode(A0, OUTPUT);
   pinMode(A1, OUTPUT);
   
   lc.shutdown(0,false);
   lc.setIntensity(0,3);
   lc.clearDisplay(0);
   
   digitalWrite(UP_LED, HIGH);
   digitalWrite(DOWN_LED, LOW);
   digitalWrite(RIGHT_LED, LOW);
   digitalWrite(LEFT_LED, LOW);
   digitalWrite(A0, LOW);
   digitalWrite(A1, LOW);
   
   Serial.begin(9600);
   Timestamp_Button_Pressed=millis();
}



void Change_direction() {
if (millis() - Timestamp_Button_Pressed>200){
  Direction= Direction+1;
  if (Direction==5) Direction=1;
    if (Direction==1) {
      digitalWrite(UP_LED, HIGH);
      digitalWrite(DOWN_LED, LOW);
      digitalWrite(RIGHT_LED, LOW);
      digitalWrite(LEFT_LED, LOW);
    }
     if (Direction==2) {
      digitalWrite(UP_LED, LOW);
      digitalWrite(DOWN_LED, LOW);
      digitalWrite(RIGHT_LED, HIGH);
      digitalWrite(LEFT_LED, LOW);
    }
    if (Direction==3) {
      digitalWrite(UP_LED, LOW);
      digitalWrite(DOWN_LED, HIGH);
      digitalWrite(RIGHT_LED, LOW);
      digitalWrite(LEFT_LED, LOW);
    }
    if (Direction==4) {
      digitalWrite(UP_LED, LOW);
      digitalWrite(DOWN_LED, LOW);
      digitalWrite(RIGHT_LED, LOW);
      digitalWrite(LEFT_LED, HIGH);
    }
  }
}


void Move_in_the_maze(){
if (millis() - Timestamp_Button_Pressed>200){
  // Attempt to move up
  if (Direction==1 ){ 
    if(Move_up[cursor_row][cursor_col]==1){ // Can we move up?
      if(CurrentState[cursor_row-1][cursor_col]==1){ 
        CurrentState[cursor_row][cursor_col]=0;
      }
      cursor_row=cursor_row-1;
    }  
  }
 
  // Attempt to move right
  if (Direction==2 ){ 
    if(Move_right[cursor_row][cursor_col]==1){ // Can we move right?
      if(CurrentState[cursor_row][cursor_col+1]==1 and cursor_col!=7)
        CurrentState[cursor_row][cursor_col]=0;
      cursor_col=cursor_col+1;
    }
 }
   // Attempt to move down
  if (Direction==3 ){ 
    if(Move_down[cursor_row][cursor_col]==1){ // Can we move down?
      if(CurrentState[cursor_row+1][cursor_col]==1)
        CurrentState[cursor_row][cursor_col]=0;
      cursor_row=cursor_row+1;
    }
  }
 // Attempt to move left
  if (Direction==4 ){ 
    if(Move_left[cursor_row][cursor_col]==1){ // Can we move left?
      if(CurrentState[cursor_row][cursor_col-1]==1)
        CurrentState[cursor_row][cursor_col]=0;
      cursor_col=cursor_col-1;
    }
  }
  CurrentState[cursor_row][cursor_col]=1;
  Serial.println(cursor_col);
}  
}
void loop() {
 for(int i=0;i<8;i++){ 
  lc.setRow(0,i,CurrentState[i][7]+
                CurrentState[i][6]*2+
                CurrentState[i][5]*4+
                CurrentState[i][4]*8+
                CurrentState[i][3]*16+
                CurrentState[i][2]*32+
                CurrentState[i][1]*64+
                CurrentState[i][0]*128);
 }
 if (cursor_col==8){
    digitalWrite(A0,HIGH);
    digitalWrite(A1,HIGH);
  }

}

Credits

mdraber
50 projects • 74 followers
Contact

Comments

Please log in or sign up to comment.