Components and supplies
LED
12V DC Power Supply
Key Switch
5V Voltage Regulator
OON/OFF Switch
Wires
LCD 16X2
Resistor 10k
Press Switch
Arduino Pro Mini 328 - 5V/16MHz
NPN Transistor
Project description
Code
The Code - Arduino
c_cpp
This is the main code. Was made very quickly so the are a lot of things that can be factorized... but it works well..
1//Includes 2#include <EEPROM.h> 3#include <LiquidCrystal.h> 4#include <EEPROM.h> 5 6 7//LCD pins definition and instantiation 8const int rs = 12, en = 11, d4 = 13, d5 = 10, d6 = 9, d7 = 8; 9LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 10 11 12//Software parameters 13long shootingTime = 5000; 14long configDelayTime = 3000; //must be under 5000 15int giftsBurned; 16 17bool burnedMsgFlag = true; 18bool showMsgFlag = true; 19 20//Led Matrix definition 21//Array pos: 0->Order, 1->xPos, 2->yPos, 3->AlreadyBurned 22int LEDMatrix[20][4]={ 23 24 {1,A2,2,false}, {2,A4,2,false}, {3,A5,2,false}, {4,A0,2,false}, 25 {5,A1,3,false},{6,A2,3,false}, {7,A4,3,false}, {8,A5,3,false}, {9,A0,3,false}, {10,A3,3,false}, 26 {11,A1,4,false},{12,A2,4,false},{13,A4,4,false},{14,A5,4,false},{15,A0,4,false},{16,A3,4,false}, 27 {17,A2,5,false},{18,A4,5,false},{19,A5,5,false},{20,A0,5,false}, 28}; 29 30 31int fullBurnedAddr = 20; //Address to store fullBurned value 32 33//Special Chars 34byte smile[8]={ 35 B00000, 36 B10001, 37 B00000, 38 B00000, 39 B10001, 40 B01110, 41 B00000, 42}; 43 44byte bar[8]={ 45 B11111, 46 B11111, 47 B11111, 48 B11111, 49 B11111, 50 B11111, 51 B11111, 52}; 53 54 55//Messages 56String msg[]= 57 { 58 " CHAVE ATIVADA! ", 59 " VAMOS JOGAR ", 60 "DESLIGUE A CHAVE", 61 " PARA CONTIN. ", 62 " PRESSIONE O ", 63 " GATILHO! ", 64 " BOA SORTE! ", 65 "GANHOU UM REGINA", 66 " EXPERIMENTE A ", 67 " SUA SORTE " 68 }; 69 70//List of Gifts 71String gifts[20]= 72 { 73 " FRUTAS ANANAS ", 74 " FLOC-CHOC ", 75 " SOMBRINHA ", 76 "CHOCOLATE LEITE", 77 " CLASSIC ", 78 "EXTRA NOIRE 70%", 79 " FLOC-CHOC ", 80 "EXTRA NOIRE 70%", 81 " SOMBRINHA ", 82 " CLASSIC ", 83 "CHOCOLATE LEITE", 84 " FRUTAS MORANGO ", 85 "EXTRA NOIRE 70%", 86 " FLOC-CHOC ", 87 " CLASSIC ", 88 " SOMBRINHA ", 89 "FRUTAS SILVESTRE", 90 "CHOCOLATE LEITE", 91 " SOMBRINHA ", 92 " FRUTAS LARANJA ", 93 }; 94 95 96 97 98void setup() { 99 100 long initTimmer = millis(); 101 102 //Serial communication just for debug (coment in the END) 103 Serial.begin(9600); 104 105 lcd.createChar(0, smile); 106 lcd.createChar(1, bar); 107 108 lcd.begin(16, 2); 109 lcd.clear(); 110 111 //Inicialize LED matrix pins 112 pinMode(A1, OUTPUT); 113 pinMode(A2, OUTPUT); 114 pinMode(A4, OUTPUT); 115 pinMode(A5, OUTPUT); 116 pinMode(A0, OUTPUT); 117 pinMode(A3, OUTPUT); 118 119 pinMode(2, OUTPUT); 120 pinMode(3, OUTPUT); 121 pinMode(4, OUTPUT); 122 pinMode(5, OUTPUT); 123 124 //Inicialize common pin of LED matrix PWM 125 pinMode(6, OUTPUT); 126 127 128 //LED brightness 129 analogWrite(6, 255); 130 131 132 //Input Btn 133 pinMode(A6, INPUT); //pressure BT 134 pinMode(A7, INPUT); //key BT 135 136 137 checkConfig(); 138 139 intializeArray(); 140} 141 142 143void loop() { 144 145 randomSeed(millis()); 146 147 if(isFullBurned()){ 148 showBurnedLEDs(); 149 if (burnedMsgFlag){ 150 showBurnedMsg(); 151 } 152 } 153 else{ 154 showBurnedLEDs(); 155 ckeckKeyActivation(); 156 if(showMsgFlag){ 157 showbaseMsg(); 158 } 159 } 160} 161 162 163 164 165 166void showbaseMsg(){ 167 showMsgFlag = !showMsgFlag; 168 lcd.clear(); 169 lcd.setCursor(0,0); 170 lcd.print(" EXPERIMENTE "); 171 lcd.setCursor(0,1); 172 lcd.print(" A SUA SORTE "); 173} 174 175void checkConfig(){ 176 if(analogRead(A6) >= 50 & analogRead(A7) <= 50){ 177 lcd.clear(); 178 lcd.setCursor(0,0); 179 lcd.print(" TO RESET TURN "); 180 lcd.setCursor(0,1); 181 lcd.print("THE KEY, HAVE 2s"); 182 delay(2000); 183 if(analogRead(A7) >= 50){ 184 resetArray(); 185 lcd.clear(); 186 lcd.setCursor(0,0); 187 lcd.print(" RESETTING.. "); 188 lcd.setCursor(0,1); 189 lcd.print(" TURN THE KEY "); 190 while(analogRead(A7) >= 50); 191 lcd.clear(); 192 lcd.setCursor(0,0); 193 lcd.print(" GAME RESETED "); 194 lcd.setCursor(0,1); 195 lcd.print(" LET's PLAY "); 196 delay(2000); 197 lcd.clear(); 198 } 199 else 200 { 201 lcd.clear(); 202 lcd.setCursor(0,0); 203 lcd.print(" TIME OUT! "); 204 delay(2000); 205 } 206 } 207 208 showMsgFlag=true; 209 210} 211 212void showBurnedMsg(){ 213 lcd.setCursor(0,0); 214 lcd.print(" JOGO COMPLETO "); 215 lcd.setCursor(0,1); 216 lcd.print(" OBRIGADO! "); 217 burnedMsgFlag = !burnedMsgFlag; 218 showMsgFlag=true; 219} 220 221 222void showBurnedLEDs(){ 223 for(int n=0; n<=19; n++){ 224 if(LEDMatrix[n][3]){ 225 digitalWrite(LEDMatrix[n][1],HIGH);digitalWrite(LEDMatrix[n][2],HIGH); 226 digitalWrite(LEDMatrix[n][1],LOW);digitalWrite(LEDMatrix[n][2],LOW); 227 delay(0); 228 } 229 } 230} 231 232 233void ckeckKeyActivation() 234{ 235 if(analogRead(A7) >= 50){ 236 letsPlay(); 237 } 238} 239 240 241void letsPlay(){ 242 lcd.clear(); 243 lcd.setCursor(0,0); 244 lcd.print(msg[0]); 245 lcd.setCursor(0,1); 246 lcd.print(msg[1]); 247 delay(3000); 248 lcd.setCursor(0,0); 249 lcd.print(msg[2]); 250 lcd.setCursor(0,1); 251 lcd.print(msg[3]); 252 while(analogRead(A7) >=50){} 253 lcd.setCursor(0,0); 254 lcd.print(msg[4]); 255 lcd.setCursor(0,1); 256 lcd.print(msg[5]); 257 while(analogRead(A6) <=50){} 258 lcd.clear(); 259 lcd.setCursor(0,0); 260 lcd.print(msg[6]); 261 randomShoot(shootingTime); 262 263 showMsgFlag=true; 264} 265 266 267void randomShoot(long timmer){ 268 269 long initTime = millis(); 270 long barTCtrl = millis(); 271 long barLenghtRatio = timmer/16; 272 int barPos = 0; 273 274 int xPos, yPos, randNumber; 275 276 while(initTime >= millis()-timmer){ 277 randNumber = int(random(0,20)); 278 279 digitalWrite(LEDMatrix[randNumber][1],HIGH);digitalWrite(LEDMatrix[randNumber][2],HIGH); 280 delay(20); 281 digitalWrite(LEDMatrix[randNumber][1],LOW);digitalWrite(LEDMatrix[randNumber][2],LOW); 282 lcd.setCursor(barPos,1); 283 lcd.print(char(1)); 284 if (millis() - (barTCtrl+barLenghtRatio) <= barLenghtRatio) 285 { 286 barPos++; 287 barTCtrl = millis(); 288 } 289 } 290 291 int gift = nextUnBurned(randNumber); 292 293 LEDMatrix[gift][3] = true; 294 writeToEEPROM(gift, true); 295 296 digitalWrite(LEDMatrix[gift][1],HIGH);digitalWrite(LEDMatrix[gift][2],HIGH); 297 298 lcd.clear(); 299 lcd.setCursor(0,0); 300 lcd.print(msg[7]); 301 lcd.setCursor(0,1); 302 lcd.print(gifts[gift]); 303 while(analogRead(A6) <=50){} 304 lcd.clear(); 305 306 showMsgFlag=true; 307} 308 309 310//Recursive function to give the next available Gift 311int nextUnBurned(int rnd){ 312 if(LEDMatrix[rnd][3]){ 313 if (rnd >19) rnd = 0; 314 rnd++; 315 nextUnBurned(rnd); 316 } 317 else{ 318 return rnd; 319 } 320} 321 322 323//Write to EEPROM function 324void writeToEEPROM(int addr, bool val){ 325 EEPROM.write(addr, val); 326} 327 328 329//Inicialize LEDMatrix from values stored in EEPROM 330void intializeArray(){ 331 for (int n=0; n<=19; n++){ 332 if (EEPROM.read(n)==1){ 333 LEDMatrix[n][3] = true; 334 }else{ 335 LEDMatrix[n][3] = false; 336 } 337 } 338} 339 340 341bool isFullBurned(){ 342 343 giftsBurned = 0; 344 345 for (int n=0; n<=19; n++){ 346 if (LEDMatrix[n][3]==true){ 347 giftsBurned++; 348 } 349 } 350 351 if (giftsBurned >= 20){ 352 return true; 353 }else{ 354 return false; 355 } 356} 357 358 359void resetArray(){ 360 for (int n=0; n<=19; n++){ 361 EEPROM.write(n,false); 362 } 363} 364 365 366 367 368 369 370
Downloadable files
Diagram
this is the electronic schematic
Diagram
Diagram
this is the electronic schematic
Diagram
Comments
Only logged in users can leave comments
pmrleite
0 Followers
•0 Projects
Table of contents
Intro
0
0