Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <Keypad.h>
  3.  
  4. const byte LINHAS = 4;
  5. const byte COLUNAS = 4;
  6.  
  7. char MATRIZ_TECLAS[LINHAS][COLUNAS] =
  8. {
  9. {'1', '2', '3', 'A'},
  10. {'4', '5', '6', 'B'},
  11. {'7', '8', '9', 'C'},
  12. {'*', '0', '#', 'D'}
  13. };
  14.  
  15. byte PINOS_LINHAS[LINHAS] = {7, 6, 5, 4};
  16. byte PINOS_COLUNAS[COLUNAS] = {11, 10, 9, 8}; // D4..D7 LCD / C1..C4 TEC
  17.  
  18. Keypad teclado = Keypad(makeKeymap(MATRIZ_TECLAS), PINOS_LINHAS, PINOS_COLUNAS, LINHAS, COLUNAS);
  19.  
  20. LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
  21.  
  22. byte SMILEY[8] = {
  23. B00000,
  24. B10001,
  25. B00000,
  26. B00000,
  27. B10001,
  28. B01110,
  29. B00000,
  30. };
  31.  
  32. byte PACMAN_LOOKING_RIGHT[8] = {
  33. B00000,
  34. B01110,
  35. B11101,
  36. B11111,
  37. B11000,
  38. B11111,
  39. B01110,
  40. B00000
  41. };
  42.  
  43. byte PACMAN_LOOKING_LEFT[8] = {
  44. B00000,
  45. B01110,
  46. B10111,
  47. B11111,
  48. B00011,
  49. B11111,
  50. B01110,
  51. B00000
  52. };
  53.  
  54. byte PACMAN_LOOKING_BOTTOM[8] = {
  55. B00000,
  56. B01110,
  57. B11111,
  58. B10111,
  59. B10111,
  60. B10101,
  61. B00110,
  62. B00000
  63. };
  64.  
  65. byte PACMAN_LOOKING_TOP[8] = {
  66. B00000,
  67. B00110,
  68. B10101,
  69. B10111,
  70. B10111,
  71. B11111,
  72. B01110,
  73. B00000
  74. };
  75.  
  76. byte PACMAN_FOOD[8] = {
  77. B00000,
  78. B00010,
  79. B00100,
  80. B01110,
  81. B01110,
  82. B01110,
  83. B00000,
  84. B00000
  85. };
  86.  
  87. byte PACMAN_BLOCK[8] = {
  88. B11111,
  89. B11111,
  90. B11111,
  91. B11111,
  92. B11111,
  93. B11111,
  94. B11111,
  95. B11111
  96. };
  97.  
  98. byte PACMAN_EMPTY_CODE = byte(0);
  99. byte PACMAN_LOOKING_RIGHT_CODE = byte(1);
  100. byte PACMAN_LOOKING_LEFT_CODE = byte(2);
  101. byte PACMAN_LOOKING_BOTTOM_CODE = byte(3);
  102. byte PACMAN_LOOKING_TOP_CODE = byte(4);
  103. byte PACMAN_FOOD_CODE = byte(5);
  104. byte PACMAN_BLOCK_CODE = byte(6);
  105.  
  106. bool PACMAN_LOOSE = false;
  107. bool PACMAN_WINS = false;
  108.  
  109. byte pacman_lookingNow = PACMAN_LOOKING_RIGHT_CODE;
  110. byte pacman_lcdMatrix[16][2];
  111. int pacman_positionCol = 0;
  112. int pacman_positionRow = 0;
  113. int pacman_foodEaten = 0;
  114.  
  115. unsigned long previousMillisMove, previousMillisFood;
  116. unsigned long SECOND = 50;
  117. long intervalFood = SECOND * 3;
  118. int currentQuantityFood = 0;
  119. int currentQuantityBlocks = 0;
  120.  
  121. void pacman_start() {
  122. lcd.createChar(PACMAN_LOOKING_RIGHT_CODE, PACMAN_LOOKING_RIGHT);
  123. lcd.createChar(PACMAN_LOOKING_LEFT_CODE, PACMAN_LOOKING_LEFT);
  124. lcd.createChar(PACMAN_LOOKING_BOTTOM_CODE, PACMAN_LOOKING_BOTTOM);
  125. lcd.createChar(PACMAN_LOOKING_TOP_CODE, PACMAN_LOOKING_TOP);
  126. lcd.createChar(PACMAN_FOOD_CODE, PACMAN_FOOD);
  127. lcd.createChar(PACMAN_BLOCK_CODE, PACMAN_BLOCK);
  128.  
  129. lcd.begin(16, 2);
  130. lcd.write(PACMAN_LOOKING_RIGHT_CODE);
  131. pacman_lcdMatrix[0][0] = PACMAN_LOOKING_RIGHT_CODE;
  132.  
  133. pacman_createBlocks();
  134. }
  135.  
  136. void pacman_createBlocks() {
  137. int col, row;
  138.  
  139. pacman_randomPosition(&col, &row);
  140. pacman_createBlock(col, 0);
  141.  
  142. pacman_randomPosition(&col, &row);
  143. pacman_createBlock(col, 1);
  144. }
  145.  
  146. void pacman_createBlock(int col, int row) {
  147. currentQuantityBlocks += 1;
  148. lcd.setCursor(col, row);
  149. lcd.write(PACMAN_BLOCK_CODE);
  150. pacman_lcdMatrix[col][row] = PACMAN_BLOCK_CODE;
  151. }
  152.  
  153. void pacman_move() {
  154. if (PACMAN_LOOSE || PACMAN_WINS) return;
  155.  
  156. lcd.setCursor(pacman_positionCol, pacman_positionRow);
  157. lcd.write(' ');
  158. pacman_lcdMatrix[pacman_positionCol][pacman_positionRow] = PACMAN_EMPTY_CODE;
  159.  
  160. if (pacman_lookingNow == PACMAN_LOOKING_RIGHT_CODE) {
  161. pacman_positionCol = (pacman_positionCol + 1) % 16;
  162. } else if (pacman_lookingNow == PACMAN_LOOKING_LEFT_CODE) {
  163. pacman_positionCol = pacman_positionCol - 1;
  164. if (pacman_positionCol < 0) pacman_positionCol = 15;
  165. } else if (pacman_lookingNow == PACMAN_LOOKING_BOTTOM_CODE) {
  166. pacman_positionRow = (pacman_positionRow + 1) % 2;
  167. } else if (pacman_lookingNow == PACMAN_LOOKING_TOP_CODE) {
  168. pacman_positionRow = pacman_positionRow - 1;
  169. if (pacman_positionRow < 0) pacman_positionRow = 1;
  170. }
  171.  
  172. byte newObjectCode = pacman_lcdMatrix[pacman_positionCol][pacman_positionRow];
  173.  
  174. if (newObjectCode == PACMAN_BLOCK_CODE) {
  175. PACMAN_LOOSE = true;
  176. pacman_showLooseMessage();
  177. return;
  178. }
  179.  
  180. if (newObjectCode == PACMAN_FOOD_CODE) {
  181. intervalFood -= (SECOND / 10);
  182. currentQuantityFood -= 1;
  183.  
  184. if (intervalFood <= 0) {
  185. PACMAN_WINS = true;
  186. pacman_showWinsMessage();
  187. return;
  188. }
  189. }
  190.  
  191. lcd.setCursor(pacman_positionCol, pacman_positionRow);
  192. lcd.write(pacman_lookingNow);
  193. pacman_lcdMatrix[pacman_positionCol][pacman_positionRow] = pacman_lookingNow;
  194. }
  195.  
  196. void pacman_createFood() {
  197. if (PACMAN_LOOSE || PACMAN_WINS) return;
  198.  
  199. int col, row;
  200. pacman_randomPosition(&col, &row);
  201.  
  202. if (pacman_lcdMatrix[col][row] != PACMAN_EMPTY_CODE) {
  203. pacman_createFood();
  204. return;
  205. }
  206.  
  207. currentQuantityFood += 1;
  208. lcd.setCursor(col, row);
  209. lcd.write(PACMAN_FOOD_CODE);
  210. pacman_lcdMatrix[col][row] = PACMAN_FOOD_CODE;
  211.  
  212. int pacmanLcdSize = 1;
  213.  
  214. if ((currentQuantityFood + currentQuantityBlocks + pacmanLcdSize) == (16 * 2)) {
  215. delay(500);
  216. PACMAN_LOOSE = true;
  217. pacman_showLooseMessage();
  218. }
  219. }
  220.  
  221. void pacman_randomPosition(int *col, int *row) {
  222. int randCol = random(0, 16);
  223. int randRow = random(0, 2);
  224.  
  225. *col = randCol;
  226. *row = randRow;
  227. }
  228.  
  229. void pacman_showLooseMessage() {
  230. lcd.clear();
  231. lcd.write("You loose! Sorry");
  232. }
  233.  
  234. void pacman_showWinsMessage() {
  235. lcd.clear();
  236. lcd.write("YOU WIN! YEAH");
  237.  
  238. lcd.setCursor(0, 1);
  239. lcd.write("POINTS: ");
  240.  
  241. char foodEatenAsChar[3];
  242. sprintf(foodEatenAsChar, "%d", pacman_foodEaten);
  243.  
  244. lcd.write(foodEatenAsChar);
  245. }
  246.  
  247. void setup() {
  248. Serial.begin(9600);
  249. Serial.println("Teclado 4x3 - Exemplo biblioteca Keypad");
  250. Serial.println("Aguardando aconamento das teclas...");
  251. Serial.println();
  252.  
  253. randomSeed(analogRead(0));
  254.  
  255. pacman_start();
  256. }
  257.  
  258. void loop() {
  259. unsigned long currentMillisMove = millis();
  260. unsigned long currentMillisFood = millis();
  261. char teclaPressionada = teclado.getKey();
  262.  
  263. switch (teclaPressionada) {
  264. case '2':
  265. pacman_lookingNow = PACMAN_LOOKING_TOP_CODE;
  266. break;
  267. case '6':
  268. pacman_lookingNow = PACMAN_LOOKING_RIGHT_CODE;
  269. break;
  270. case '8':
  271. pacman_lookingNow = PACMAN_LOOKING_BOTTOM_CODE;
  272. break;
  273. case '4':
  274. pacman_lookingNow = PACMAN_LOOKING_LEFT_CODE;
  275. break;
  276. }
  277.  
  278. if ((currentMillisMove - previousMillisMove) > SECOND) {
  279. previousMillisMove = currentMillisMove;
  280. pacman_move();
  281. }
  282.  
  283. if ((currentMillisFood - previousMillisFood) > intervalFood) {
  284. previousMillisFood = currentMillisFood;
  285. pacman_createFood();
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement