Maze Lock created with MAX7219 LED Matrix
Open the magnetic lock by solving the simple maze puzzle
Components and supplies
1
Relay Module (Generic)
1
MAXREFDES99# MAX7219 Display Driver Shield
1
Arduino Nano R3
Apps and platforms
1
Arduino IDE
Project description
Code
Maze Lock
arduino
1//Mario's Ideas 2//Maze Lock 3 4#include <LedControl.h> 5int cursor_row= 2; 6int cursor_col = 0; 7int Direction =1; 8 9int UP_LED = 6; 10int DOWN_LED = 8; 11int RIGHT_LED = 4; 12int LEFT_LED = 12; 13 14int DIN = 11; 15int CS = 7; 16int CLK = 13; 17 18long Timestamp_Button_Pressed; 19 20 21LedControl lc=LedControl(DIN, CLK, CS,0); 22 23int CurrentState [8] [8] { 24 {0,0,0,0,0,0,0,0}, 25 {0,0,0,0,0,0,0,0}, 26 {1,0,0,0,0,0,0,0}, 27 {0,0,0,0,0,0,0,0}, 28 {0,0,0,0,0,0,0,0}, 29 {0,0,0,0,0,0,0,0}, 30 {0,0,0,0,0,0,0,0}, 31 {0,0,0,0,0,0,0,0} 32}; 33 34int Move_up [8] [8] { 35 {0,0,0,0,0,0,0,0}, 36 {1,1,1,0,1,0,1,1}, 37 {1,1,1,1,0,0,0,1}, 38 {0,1,1,1,0,0,0,0}, 39 {1,0,1,1,0,1,1,1}, 40 {1,1,0,0,0,1,1,1}, 41 {1,0,0,0,0,1,0,1}, 42 {0,0,1,0,0,0,0,1} 43}; 44 45int Move_down [8] [8] { 46 {1,1,1,0,1,0,1,1}, 47 {1,1,1,1,0,0,0,1}, 48 {0,1,1,1,0,0,0,0}, 49 {1,0,1,1,0,1,1,1}, 50 {1,1,0,0,0,1,1,1}, 51 {1,0,0,0,0,1,1,1}, 52 {0,0,1,0,0,0,0,1}, 53 {0,0,0,0,0,0,0,0} 54}; 55 56int Move_left [8] [8] { 57 {0,1,0,0,1,1,0,1}, 58 {0,0,0,0,1,1,1,0}, 59 {0,0,1,0,0,1,1,1}, 60 {0,1,0,0,1,1,0,1}, 61 {0,0,1,0,1,0,0,0}, 62 {0,0,1,1,1,0,0,0}, 63 {0,1,1,1,1,1,1,1}, 64 {0,1,1,1,1,1,1,0} 65}; 66 67int Move_right [8] [8] { 68 {1,0,0,1,1,0,1,0}, 69 {0,0,0,1,1,1,0,1}, 70 {0,1,0,0,1,1,1,0}, 71 {1,0,0,1,1,0,1,0}, 72 {0,1,0,1,0,0,0,0}, 73 {0,1,1,1,0,0,0,0}, 74 {1,1,1,1,1,1,1,0}, 75 {1,1,1,1,1,1,0,0} 76}; 77 78void setup() { 79 80 attachInterrupt(0,Change_direction, RISING); 81 attachInterrupt(1,Move_in_the_maze, RISING); 82 83 pinMode(UP_LED, OUTPUT); 84 pinMode(DOWN_LED, OUTPUT); 85 pinMode(RIGHT_LED, OUTPUT); 86 pinMode(LEFT_LED, OUTPUT); 87 pinMode(A0, OUTPUT); 88 pinMode(A1, OUTPUT); 89 90 lc.shutdown(0,false); 91 lc.setIntensity(0,3); 92 lc.clearDisplay(0); 93 94 digitalWrite(UP_LED, HIGH); 95 digitalWrite(DOWN_LED, LOW); 96 digitalWrite(RIGHT_LED, LOW); 97 digitalWrite(LEFT_LED, LOW); 98 digitalWrite(A0, LOW); 99 digitalWrite(A1, LOW); 100 101 Serial.begin(9600); 102 Timestamp_Button_Pressed=millis(); 103} 104 105 106 107void Change_direction() { 108if (millis() - Timestamp_Button_Pressed>200){ 109 Direction= Direction+1; 110 if (Direction==5) Direction=1; 111 if (Direction==1) { 112 digitalWrite(UP_LED, HIGH); 113 digitalWrite(DOWN_LED, LOW); 114 digitalWrite(RIGHT_LED, LOW); 115 digitalWrite(LEFT_LED, LOW); 116 } 117 if (Direction==2) { 118 digitalWrite(UP_LED, LOW); 119 digitalWrite(DOWN_LED, LOW); 120 digitalWrite(RIGHT_LED, HIGH); 121 digitalWrite(LEFT_LED, LOW); 122 } 123 if (Direction==3) { 124 digitalWrite(UP_LED, LOW); 125 digitalWrite(DOWN_LED, HIGH); 126 digitalWrite(RIGHT_LED, LOW); 127 digitalWrite(LEFT_LED, LOW); 128 } 129 if (Direction==4) { 130 digitalWrite(UP_LED, LOW); 131 digitalWrite(DOWN_LED, LOW); 132 digitalWrite(RIGHT_LED, LOW); 133 digitalWrite(LEFT_LED, HIGH); 134 } 135 } 136} 137 138 139void Move_in_the_maze(){ 140if (millis() - Timestamp_Button_Pressed>200){ 141 // Attempt to move up 142 if (Direction==1 ){ 143 if(Move_up[cursor_row][cursor_col]==1){ // Can we move up? 144 if(CurrentState[cursor_row-1][cursor_col]==1){ 145 CurrentState[cursor_row][cursor_col]=0; 146 } 147 cursor_row=cursor_row-1; 148 } 149 } 150 151 // Attempt to move right 152 if (Direction==2 ){ 153 if(Move_right[cursor_row][cursor_col]==1){ // Can we move right? 154 if(CurrentState[cursor_row][cursor_col+1]==1 and cursor_col!=7) 155 CurrentState[cursor_row][cursor_col]=0; 156 cursor_col=cursor_col+1; 157 } 158 } 159 // Attempt to move down 160 if (Direction==3 ){ 161 if(Move_down[cursor_row][cursor_col]==1){ // Can we move down? 162 if(CurrentState[cursor_row+1][cursor_col]==1) 163 CurrentState[cursor_row][cursor_col]=0; 164 cursor_row=cursor_row+1; 165 } 166 } 167 // Attempt to move left 168 if (Direction==4 ){ 169 if(Move_left[cursor_row][cursor_col]==1){ // Can we move left? 170 if(CurrentState[cursor_row][cursor_col-1]==1) 171 CurrentState[cursor_row][cursor_col]=0; 172 cursor_col=cursor_col-1; 173 } 174 } 175 CurrentState[cursor_row][cursor_col]=1; 176 Serial.println(cursor_col); 177} 178} 179void loop() { 180 for(int i=0;i<8;i++){ 181 lc.setRow(0,i,CurrentState[i][7]+ 182 CurrentState[i][6]*2+ 183 CurrentState[i][5]*4+ 184 CurrentState[i][4]*8+ 185 CurrentState[i][3]*16+ 186 CurrentState[i][2]*32+ 187 CurrentState[i][1]*64+ 188 CurrentState[i][0]*128); 189 } 190 if (cursor_col==8){ 191 digitalWrite(A0,HIGH); 192 digitalWrite(A1,HIGH); 193 } 194 195}
Downloadable files
Diagram
Diagram

Diagram
Diagram

Comments
Only logged in users can leave comments