Components and supplies
Buzzer, Piezo
Resistor 10k ohm
Jumper wires (generic)
Tactile Switch, Top Actuated
Alphanumeric LCD, 16 x 2
Arduino UNO
Resistor 221 ohm
Perma-Proto Breadboard Half Size
Apps and platforms
Arduino IDE
Project description
Code
Code
c_cpp
1// C++ code 2// 3 4#include <LiquidCrystal.h> 5LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 6 7const int BTN_1 = 8; 8const int BTN_2 = 9; 9const int BTN_3 = 10; 10const int buzzerPin = 7; 11int key = 0; //which button is pressed? 12int Li = 17; //variable for lcd left row scrolling 13int Lii = 1; //variable for lcd left row scrolling 14int Ri = -1; //variable for lcd right row scrolling 15int Rii = -1; //variable for lcd right row scrolling 16int numTones = 10; 17int tones[] = { 261, 277, 294, 311, 330, 349, 370, 392, 415, 440 }; 18// mid C C# D D# E F F# G G# A 19 20//arrays for custom chars. Black and white key code pegs 21byte black_peg[8] = { 22 B00000, 23 B01110, 24 B11111, 25 B11111, 26 B11111, 27 B01110, 28 B00000, 29}; 30byte white_peg[8] = { 31 B00000, 32 B01110, 33 B10001, 34 B10001, 35 B10001, 36 B01110, 37 B00000, 38}; 39 40void setup() { 41 pinMode(BTN_1, INPUT); //key code black pegs 42 pinMode(BTN_2, INPUT); //key code white pegs 43 pinMode(BTN_3, INPUT); //enter to confirm key code or start the game 44 pinMode(buzzerPin, OUTPUT); //to manage the sound effects 45 Serial.begin(9600); // use monitor for troubleshooting 46 lcd.createChar(1, black_peg); //create custom char for black pegs 47 lcd.createChar(2, white_peg); //create custom char for white pegs 48 lcd.begin(16, 2); //inizialize the LCD 49 50 51 for (int i = 0; i <= 16; i++) { 52 lcd.setCursor(0, 0); 53 lcd.print(Scroll_LCD_Right("MASTER MIND GAME")); 54 delay(200); 55 } 56 for (int i = 0; i <= 36; i++) { 57 lcd.setCursor(0, 1); 58 lcd.print(Scroll_LCD_Left(" Arduino will guess your secret code.")); 59 if (i == 0) { 60 delay(1100); 61 } else { 62 delay(300); 63 } 64 } 65 Clear_Scroll_LCD_Left(); 66 for (int i = 0; i <= 31; i++) { 67 lcd.setCursor(0, 1); 68 lcd.print(Scroll_LCD_Left("Write it down and press key 3")); 69 delay(300); 70 } 71} 72 73void loop() { 74 //wait until key 3 is pressed 75 key = 0; 76 while (key != 3) { 77 key = read_buttons(); 78 lcd.clear(); 79 if (key == 1) { lcd.noDisplay(); } 80 } 81 lcd.display(); 82 int lc = 0; 83 byte db_lc[252][4]; 84 byte game_board[10][4]; 85 byte game_board_k[10][2]; 86 byte code_played[4] = {}; 87 byte whites = 0; 88 byte blacks = 0; 89 for (int row = 0; row < 11; row++) { 90 lcd.clear(); 91 int digit; 92 // the first code played is "1234" type and it is generated by random function 93 if (row == 0) { 94 byte code_pattern[6] = { 1, 2, 3, 4, 5, 6 }; 95 randomSeed(analogRead(0)); 96 for (int i = 0; i < 4; i++) { 97 digit = random(6 - i); 98 code_played[i] = code_pattern[digit]; 99 for (int j = digit; j < 5; j++) { 100 code_pattern[j] = code_pattern[j + 1]; 101 } 102 } 103 //set fixed 1234 as code_played for debugging 104 //code_played[0]=1; code_played[1]=2; code_played[2]=3; code_played[3]=4; 105 } else { 106 int rndCode = random(lc); 107 for (int k = 0; k < 4; ++k) { code_played[k] = db_lc[rndCode][k]; } 108 } 109 // print row number + code played - example:" 1) 2453 " 110 lcd.print(row + 1); 111 lcd.print(") "); 112 for (int i = 0; i < 4; i++) { 113 lcd.print(code_played[i]); 114 } 115 Clear_Scroll_LCD_Left(); 116 for (int i = 0; i <= 26; i++) { 117 lcd.setCursor(0, 1); 118 lcd.print(Scroll_LCD_Left("1)blacks 2)whites 3)enter")); 119 if (i == 0 && row == 0) { 120 delay(1100); 121 } else { 122 delay(300); 123 } 124 } 125 if (lc == 1) { 126 whites = 0; 127 blacks = 4; 128 } else { 129 // input blacks and whites key code with buttons 1 and 2 130 lcd.setCursor(8, 0); 131 lcd.blink(); 132 whites = 0; 133 blacks = 0; 134 key = 0; 135 while (key != 3) { 136 key = read_buttons(); 137 if (key == 1) { blacks++; } 138 if (key == 2) { whites++; } 139 if (blacks + whites > 4) { 140 lcd.setCursor(8, 0); 141 lcd.print(" "); 142 lcd.setCursor(8, 0); 143 blacks = 0; 144 whites = 0; 145 } else { 146 lcd.write(byte(key)); 147 } 148 } 149 } 150 lcd.noBlink(); 151 //append code played to game board 152 for (int k = 0; k < 4; ++k) { game_board[row][k] = code_played[k]; } 153 //append blacks and whites to game_board_k 154 game_board_k[row][0] = blacks; 155 game_board_k[row][1] = whites; 156 if (blacks == 4) { 157 lcd.setCursor(12, 0); 158 lcd.print(" "); 159 lcd.setCursor(0, 1); 160 lcd.print("SecrCode Guessed"); 161 for (int i = 0; i < numTones; i++) { 162 tone(buzzerPin, tones[i], 100); 163 delay(300); 164 } 165 for (int i = numTones; i > 0; i--) { 166 tone(buzzerPin, tones[i], 100); 167 delay(300); 168 } 169 noTone(buzzerPin); 170 break; 171 } 172 lc = left_codes(game_board, game_board_k, row + 1, db_lc); 173 if (lc == 0) { 174 Clear_Scroll_LCD_Left(); 175 for (int i = 0; i <= 27; i++) { 176 lcd.setCursor(0, 1); 177 lcd.print(Scroll_LCD_Left("Almost a Key Code is wrong")); 178 if (i == 0) { 179 delay(1100); 180 } else { 181 delay(150); 182 } 183 } 184 break; 185 } 186 } 187} 188 189//---------------------------------- 190int read_buttons() { 191 while (true) { 192 int key1 = digitalRead(BTN_1); 193 int key2 = digitalRead(BTN_2); 194 int key3 = digitalRead(BTN_3); 195 if (key1 == HIGH) { 196 tone(buzzerPin, 100, 100); 197 delay(400); 198 return (1); 199 } 200 if (key2 == HIGH) { 201 tone(buzzerPin, 200, 100); 202 delay(400); 203 return (2); 204 } 205 if (key3 == HIGH) { 206 tone(buzzerPin, 300, 100); 207 delay(400); 208 return (3); 209 } 210 } 211} 212//---------------------------------- 213String Scroll_LCD_Left(String StrDisplay) { 214 String result; 215 String StrProcess = " " + StrDisplay + " "; 216 result = StrProcess.substring(Li, Lii); 217 //Serial.print ("\ 218"); 219 //Serial.print (StrProcess); 220 Li++; 221 Lii++; 222 if (Li > StrProcess.length()) { 223 Li = 16; 224 Lii = 0; 225 } 226 return result; 227} 228void Clear_Scroll_LCD_Left() { 229 Li = 16; 230 Lii = 0; 231} 232//---------------------------------- 233String Scroll_LCD_Right(String StrDisplay) { 234 String result; 235 String StrProcess = " " + StrDisplay + " "; 236 if (Rii < 1) { 237 Ri = StrProcess.length(); 238 Rii = Ri - 16; 239 } 240 result = StrProcess.substring(Rii, Ri); 241 Ri--; 242 Rii--; 243 return result; 244} 245void Clear_Scroll_LCD_Right() { 246 Ri = -1; 247 Rii = -1; 248} 249//------------------------------------- 250int left_codes(byte game_board[][4], byte game_borad_k[][2], byte len_board, byte db_lc[][4]) { 251 int colors = 6; 252 byte t[4] = { 0, 0, 0, 0 }; 253 byte board_row[4] = {}; 254 int j = 0; // used for db_lc code append 255 for (int a = 1; a < colors + 1; a++) { 256 for (int b = 1; b < colors + 1; b++) { 257 for (int c = 1; c < colors + 1; c++) { 258 for (int d = 1; d < colors + 1; d++) { 259 t[0] = a, t[1] = b, t[2] = c, t[3] = d; 260 for (int i = 0; i < len_board; i++) { 261 byte key_code[2] = {}; 262 // board_row = game_board[i] 263 for (int k = 0; k < 4; k++) { board_row[k] = game_board[i][k]; } 264 // key_code is found_keycode 265 find_keycode(t, board_row, key_code); 266 if (key_code[0] != game_borad_k[i][0] || key_code[1] != game_borad_k[i][1]) { 267 break; 268 } else { 269 if (i == len_board - 1) { 270 //db_lc.append(t) 271 for (int k = 0; k < 4; k++) { 272 db_lc[j][k] = t[k]; 273 } 274 j++; 275 // memory room limits the left code to 252 276 if (j == 252) { 277 int lc = j; 278 return (lc); 279 } 280 } 281 } 282 } 283 } 284 } 285 } 286 } 287 int lc = j; 288 return (lc); 289} 290 291//--------------------------------------------------------------- 292void find_keycode(byte code_a[4], byte code_b[4], byte key_code[2]) { 293 byte whites = 0; 294 byte blacks = 0; 295 byte sa[4] = { 1, 1, 1, 1 }; 296 byte sb[4] = { 1, 1, 1, 1 }; 297 for (int i = 0; i < 4; i++) { 298 if (code_a[i] == code_b[i]) { 299 sa[i] = 0; 300 sb[i] = 0; 301 blacks += 1; 302 } 303 } 304 for (int i = 0; i < 4; i++) { 305 for (int j = 0; j < 4; j++) { 306 if (sa[i] == 1 && sb[j] == 1 && code_a[i] == code_b[j]) { 307 sa[i] = 0; 308 sb[j] = 0; 309 whites += 1; 310 } 311 } 312 } 313 key_code[0] = blacks; 314 key_code[1] = whites; 315} 316
Code
c_cpp
1// C++ code 2// 3 4#include <LiquidCrystal.h> 5LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 6 7const int BTN_1 = 8; 8const int BTN_2 = 9; 9const int BTN_3 = 10; 10const int buzzerPin = 7; 11int key = 0; //which button is pressed? 12int Li = 17; //variable for lcd left row scrolling 13int Lii = 1; //variable for lcd left row scrolling 14int Ri = -1; //variable for lcd right row scrolling 15int Rii = -1; //variable for lcd right row scrolling 16int numTones = 10; 17int tones[] = { 261, 277, 294, 311, 330, 349, 370, 392, 415, 440 }; 18// mid C C# D D# E F F# G G# A 19 20//arrays for custom chars. Black and white key code pegs 21byte black_peg[8] = { 22 B00000, 23 B01110, 24 B11111, 25 B11111, 26 B11111, 27 B01110, 28 B00000, 29}; 30byte white_peg[8] = { 31 B00000, 32 B01110, 33 B10001, 34 B10001, 35 B10001, 36 B01110, 37 B00000, 38}; 39 40void setup() { 41 pinMode(BTN_1, INPUT); //key code black pegs 42 pinMode(BTN_2, INPUT); //key code white pegs 43 pinMode(BTN_3, INPUT); //enter to confirm key code or start the game 44 pinMode(buzzerPin, OUTPUT); //to manage the sound effects 45 Serial.begin(9600); // use monitor for troubleshooting 46 lcd.createChar(1, black_peg); //create custom char for black pegs 47 lcd.createChar(2, white_peg); //create custom char for white pegs 48 lcd.begin(16, 2); //inizialize the LCD 49 50 51 for (int i = 0; i <= 16; i++) { 52 lcd.setCursor(0, 0); 53 lcd.print(Scroll_LCD_Right("MASTER MIND GAME")); 54 delay(200); 55 } 56 for (int i = 0; i <= 36; i++) { 57 lcd.setCursor(0, 1); 58 lcd.print(Scroll_LCD_Left(" Arduino will guess your secret code.")); 59 if (i == 0) { 60 delay(1100); 61 } else { 62 delay(300); 63 } 64 } 65 Clear_Scroll_LCD_Left(); 66 for (int i = 0; i <= 31; i++) { 67 lcd.setCursor(0, 1); 68 lcd.print(Scroll_LCD_Left("Write it down and press key 3")); 69 delay(300); 70 } 71} 72 73void loop() { 74 //wait until key 3 is pressed 75 key = 0; 76 while (key != 3) { 77 key = read_buttons(); 78 lcd.clear(); 79 if (key == 1) { lcd.noDisplay(); } 80 } 81 lcd.display(); 82 int lc = 0; 83 byte db_lc[252][4]; 84 byte game_board[10][4]; 85 byte game_board_k[10][2]; 86 byte code_played[4] = {}; 87 byte whites = 0; 88 byte blacks = 0; 89 for (int row = 0; row < 11; row++) { 90 lcd.clear(); 91 int digit; 92 // the first code played is "1234" type and it is generated by random function 93 if (row == 0) { 94 byte code_pattern[6] = { 1, 2, 3, 4, 5, 6 }; 95 randomSeed(analogRead(0)); 96 for (int i = 0; i < 4; i++) { 97 digit = random(6 - i); 98 code_played[i] = code_pattern[digit]; 99 for (int j = digit; j < 5; j++) { 100 code_pattern[j] = code_pattern[j + 1]; 101 } 102 } 103 //set fixed 1234 as code_played for debugging 104 //code_played[0]=1; code_played[1]=2; code_played[2]=3; code_played[3]=4; 105 } else { 106 int rndCode = random(lc); 107 for (int k = 0; k < 4; ++k) { code_played[k] = db_lc[rndCode][k]; } 108 } 109 // print row number + code played - example:" 1) 2453 " 110 lcd.print(row + 1); 111 lcd.print(") "); 112 for (int i = 0; i < 4; i++) { 113 lcd.print(code_played[i]); 114 } 115 Clear_Scroll_LCD_Left(); 116 for (int i = 0; i <= 26; i++) { 117 lcd.setCursor(0, 1); 118 lcd.print(Scroll_LCD_Left("1)blacks 2)whites 3)enter")); 119 if (i == 0 && row == 0) { 120 delay(1100); 121 } else { 122 delay(300); 123 } 124 } 125 if (lc == 1) { 126 whites = 0; 127 blacks = 4; 128 } else { 129 // input blacks and whites key code with buttons 1 and 2 130 lcd.setCursor(8, 0); 131 lcd.blink(); 132 whites = 0; 133 blacks = 0; 134 key = 0; 135 while (key != 3) { 136 key = read_buttons(); 137 if (key == 1) { blacks++; } 138 if (key == 2) { whites++; } 139 if (blacks + whites > 4) { 140 lcd.setCursor(8, 0); 141 lcd.print(" "); 142 lcd.setCursor(8, 0); 143 blacks = 0; 144 whites = 0; 145 } else { 146 lcd.write(byte(key)); 147 } 148 } 149 } 150 lcd.noBlink(); 151 //append code played to game board 152 for (int k = 0; k < 4; ++k) { game_board[row][k] = code_played[k]; } 153 //append blacks and whites to game_board_k 154 game_board_k[row][0] = blacks; 155 game_board_k[row][1] = whites; 156 if (blacks == 4) { 157 lcd.setCursor(12, 0); 158 lcd.print(" "); 159 lcd.setCursor(0, 1); 160 lcd.print("SecrCode Guessed"); 161 for (int i = 0; i < numTones; i++) { 162 tone(buzzerPin, tones[i], 100); 163 delay(300); 164 } 165 for (int i = numTones; i > 0; i--) { 166 tone(buzzerPin, tones[i], 100); 167 delay(300); 168 } 169 noTone(buzzerPin); 170 break; 171 } 172 lc = left_codes(game_board, game_board_k, row + 1, db_lc); 173 if (lc == 0) { 174 Clear_Scroll_LCD_Left(); 175 for (int i = 0; i <= 27; i++) { 176 lcd.setCursor(0, 1); 177 lcd.print(Scroll_LCD_Left("Almost a Key Code is wrong")); 178 if (i == 0) { 179 delay(1100); 180 } else { 181 delay(150); 182 } 183 } 184 break; 185 } 186 } 187} 188 189//---------------------------------- 190int read_buttons() { 191 while (true) { 192 int key1 = digitalRead(BTN_1); 193 int key2 = digitalRead(BTN_2); 194 int key3 = digitalRead(BTN_3); 195 if (key1 == HIGH) { 196 tone(buzzerPin, 100, 100); 197 delay(400); 198 return (1); 199 } 200 if (key2 == HIGH) { 201 tone(buzzerPin, 200, 100); 202 delay(400); 203 return (2); 204 } 205 if (key3 == HIGH) { 206 tone(buzzerPin, 300, 100); 207 delay(400); 208 return (3); 209 } 210 } 211} 212//---------------------------------- 213String Scroll_LCD_Left(String StrDisplay) { 214 String result; 215 String StrProcess = " " + StrDisplay + " "; 216 result = StrProcess.substring(Li, Lii); 217 //Serial.print ("\ 218"); 219 //Serial.print (StrProcess); 220 Li++; 221 Lii++; 222 if (Li > StrProcess.length()) { 223 Li = 16; 224 Lii = 0; 225 } 226 return result; 227} 228void Clear_Scroll_LCD_Left() { 229 Li = 16; 230 Lii = 0; 231} 232//---------------------------------- 233String Scroll_LCD_Right(String StrDisplay) { 234 String result; 235 String StrProcess = " " + StrDisplay + " "; 236 if (Rii < 1) { 237 Ri = StrProcess.length(); 238 Rii = Ri - 16; 239 } 240 result = StrProcess.substring(Rii, Ri); 241 Ri--; 242 Rii--; 243 return result; 244} 245void Clear_Scroll_LCD_Right() { 246 Ri = -1; 247 Rii = -1; 248} 249//------------------------------------- 250int left_codes(byte game_board[][4], byte game_borad_k[][2], byte len_board, byte db_lc[][4]) { 251 int colors = 6; 252 byte t[4] = { 0, 0, 0, 0 }; 253 byte board_row[4] = {}; 254 int j = 0; // used for db_lc code append 255 for (int a = 1; a < colors + 1; a++) { 256 for (int b = 1; b < colors + 1; b++) { 257 for (int c = 1; c < colors + 1; c++) { 258 for (int d = 1; d < colors + 1; d++) { 259 t[0] = a, t[1] = b, t[2] = c, t[3] = d; 260 for (int i = 0; i < len_board; i++) { 261 byte key_code[2] = {}; 262 // board_row = game_board[i] 263 for (int k = 0; k < 4; k++) { board_row[k] = game_board[i][k]; } 264 // key_code is found_keycode 265 find_keycode(t, board_row, key_code); 266 if (key_code[0] != game_borad_k[i][0] || key_code[1] != game_borad_k[i][1]) { 267 break; 268 } else { 269 if (i == len_board - 1) { 270 //db_lc.append(t) 271 for (int k = 0; k < 4; k++) { 272 db_lc[j][k] = t[k]; 273 } 274 j++; 275 // memory room limits the left code to 252 276 if (j == 252) { 277 int lc = j; 278 return (lc); 279 } 280 } 281 } 282 } 283 } 284 } 285 } 286 } 287 int lc = j; 288 return (lc); 289} 290 291//--------------------------------------------------------------- 292void find_keycode(byte code_a[4], byte code_b[4], byte key_code[2]) { 293 byte whites = 0; 294 byte blacks = 0; 295 byte sa[4] = { 1, 1, 1, 1 }; 296 byte sb[4] = { 1, 1, 1, 1 }; 297 for (int i = 0; i < 4; i++) { 298 if (code_a[i] == code_b[i]) { 299 sa[i] = 0; 300 sb[i] = 0; 301 blacks += 1; 302 } 303 } 304 for (int i = 0; i < 4; i++) { 305 for (int j = 0; j < 4; j++) { 306 if (sa[i] == 1 && sb[j] == 1 && code_a[i] == code_b[j]) { 307 sa[i] = 0; 308 sb[j] = 0; 309 whites += 1; 310 } 311 } 312 } 313 key_code[0] = blacks; 314 key_code[1] = whites; 315} 316
Downloadable files
Circuit Diagram
Circuit Diagram
Circuit Diagram
Circuit Diagram
Circuit Image
Circuit Image
Comments
Only logged in users can leave comments
zaffaroby
0 Followers
•0 Projects
Table of contents
Intro
0
0