nimajneb

Arduino Maze

Mar 8th, 2021
1,053
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Game of Maze
  2. //Version 1.1
  3. //By Benjamin Osborne
  4. //March 8, 2021
  5. //Uses an Arduino Uno, LCD Display 20 char 4 lines, 8x8 LED display
  6. //LCD Display pins are SCL pin A5 and SDA pin A4
  7.  
  8. #include <LedControl.h>
  9. #include "LiquidCrystal_I2C.h"
  10. #include <Wire.h>
  11.  
  12. int DIN = 11;
  13. int CS = 7;
  14. int CLK = 13;
  15. const int onPin = 2;
  16. const int offPin = 3;
  17. const int upPin = 4;
  18. const int downPin = 5;
  19. const int restartPin = 8;
  20. int onState = 0;
  21. int offState = 0;
  22. int upState = 0;
  23. int downState = 0;
  24. byte RightorLeft = 0;
  25. byte UporDown = 1;
  26. boolean left = 0; // 1 can go left 0 can't
  27. boolean right = 1; // 1 can go right 0 can't
  28. boolean up = 1; // 1 can go up 0 can't
  29. boolean down = 0; // 1 can go down 0 can't
  30. boolean restart = 0; //reset program to initial conditions. Restart game
  31.  
  32. LedControl lc=LedControl(DIN, CLK, CS,0);
  33. LiquidCrystal_I2C lcd(0x27,20,4);
  34.  
  35. // This is the map of the maze.
  36. int room1 [8] ={B11111111,B10000001,B10000001,B10000001,B10000001,B10000001,B10000001,B11100111 };
  37. int room2 [8] ={B11111111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111 };
  38. int room3 [8] ={B11111111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11100111 };
  39. int room4 [8] ={B11100111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111 };
  40. int room5 [8] ={B11111111,B10000001,B10000001,B00000000,B00000000,B10000001,B10000001,B11100111 };
  41. int room6 [8]={B11100111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11100111};
  42. int room7 [8]={B11111111,B10000001,B10000001,B10000000,B10000000,B10000001,B10000001,B11111111};
  43. int room8 [8]={B11100111,B10000001,B10000001,B00000000,B00000000,B10000001,B10000001,B11111111};
  44. int room9 [8]={B11100111,B10000001,B10000001,B00000001,B00000001,B10000001,B10000001,B11111111};
  45. //winning animation
  46. int youwin1 [8]={B11111111,B10000001,B10000001,B10011001,B10011001,B10000001,B10000001,B11111111};
  47. int youwin2 [8]={B11111111,B10000001,B10100101,B10000001,B10000001,B10100101,B10000001,B11111111};
  48. int youwin3 [8]={B11111111,B11000011,B10000001,B10000001,B10000001,B10000001,B11000011,B11111111};
  49. int youwin4 [8]={B11111111,B11000011,B10000001,B10000001,B10000001,B10000001,B11000011,B11111111};
  50. int youwin5 [8]={B10000001,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10000001};
  51.  
  52. void setup() {
  53. lcd.init();
  54. lcd.backlight();
  55. lcd.setCursor(0,0); // go to start of 2nd line
  56. lcd.print("Game of Maze");
  57.  
  58. pinMode(onPin, INPUT);
  59. pinMode(offPin, INPUT);
  60.  
  61.   Serial.begin(9600);
  62.   lc.shutdown(0,false);
  63.   lc.setIntensity(0,0);
  64.   lc.clearDisplay(0);
  65.  
  66. }
  67.  
  68. void loop(){
  69. onState = digitalRead(onPin);
  70. offState = digitalRead(offPin);
  71. upState = digitalRead(upPin);
  72. downState = digitalRead(downPin);
  73. restart = digitalRead(restartPin);
  74.  
  75.  
  76. if(onState == HIGH) {
  77.   if(RightorLeft < 2 && right == 1) { //set maximum
  78.   RightorLeft = RightorLeft += 1; //increment value up
  79.   delay(1000);
  80.   }
  81. }
  82. if(offState == HIGH) {
  83.   if(RightorLeft > 0 && left == 1) { //set minimum
  84.   RightorLeft = RightorLeft -= 1; //decrement value down
  85.   delay(1000);
  86.   }
  87. }
  88. if(upState == HIGH) {
  89.   if(UporDown < 2 && down == 1) { //set maximum
  90.   UporDown = UporDown += 1; //increment value up
  91.   delay(1000);
  92.   }
  93. }
  94. if(downState == HIGH) {
  95.   if(UporDown > 0 && up == 1) { //set minimum
  96.   UporDown = UporDown -= 1; //decrement value down
  97.   delay(1000);
  98.   }
  99. }
  100. if(RightorLeft == 0 && UporDown == 0) {
  101.  for(int i=0;i<8;i++) lc.setRow(0,i,room1[i]);
  102. left = 0; // 1 can go left 0 can't
  103. right = 0; // 1 can go right 0 can't
  104. up = 0; // 1 can go up 0 can't
  105. down = 1; // 1 can go down 0 can't
  106. }
  107. if(RightorLeft == 1 && UporDown == 0) {
  108.   left = 0; // 1 can go left 0 can't
  109. right = 0; // 1 can go right 0 can't
  110. up = 0; // 1 can go up 0 can't
  111. down = 0; // 1 can go down 0 can't
  112.     lcd.setCursor(0,2); // this is the winning square
  113. lcd.print(restart);
  114.     lcd.setCursor(0,3); // this is the winning square
  115. lcd.print("     You Win!!!    ");
  116.  //for(int i=0;i<8;i++) lc.setRow(0,i,room2[i]);
  117.   for(int i=0;i<8;i++) lc.setRow(0,i,youwin1[i]);
  118.  delay(250);
  119.   for(int i=0;i<8;i++) lc.setRow(0,i,youwin2[i]);
  120.  delay(250);
  121.   for(int i=0;i<8;i++) lc.setRow(0,i,youwin3[i]);
  122.   delay(250);
  123.  for(int i=0;i<8;i++) lc.setRow(0,i,youwin4[i]);
  124.   delay(250);
  125.  for(int i=0;i<8;i++) lc.setRow(0,i,youwin5[i]);
  126.  delay(250);
  127. }
  128. if(RightorLeft == 2 && UporDown == 0) {
  129.  for(int i=0;i<8;i++) lc.setRow(0,i,room3[i]);
  130.  left = 1; // 1 can go left 0 can't
  131. right = 0; // 1 can go right 0 can't
  132. up = 0; // 1 can go up 0 can't
  133. down = 1; // 1 can go down 0 can't
  134. }
  135. if(RightorLeft == 0 && UporDown == 1) {
  136. for(int i=0;i<8;i++) lc.setRow(0,i,room4[i]);
  137. left = 0; // 1 can go left 0 can't
  138. right = 1; // 1 can go right 0 can't
  139. up = 1; // 1 can go up 0 can't
  140. down = 0; // 1 can go down 0 can't
  141. }
  142. if(RightorLeft == 1 && UporDown == 1) {
  143.  for(int i=0;i<8;i++) lc.setRow(0,i,room5[i]);
  144.  left = 1; // 1 can go left 0 can't
  145. right = 1; // 1 can go right 0 can't
  146. up = 0; // 1 can go up 0 can't
  147. down = 1; // 1 can go down 0 can't
  148. }
  149. if(RightorLeft == 2 && UporDown == 1) {
  150.  for(int i=0;i<8;i++) lc.setRow(0,i,room6[i]);
  151.  left = 1; // 1 can go left 0 can't
  152. right = 0; // 1 can go right 0 can't
  153. up = 1; // 1 can go up 0 can't
  154. down = 1; // 1 can go down 0 can't
  155. }
  156. if(RightorLeft == 0 && UporDown == 2) {
  157.  for(int i=0;i<8;i++) lc.setRow(0,i,room7[i]);
  158.  left = 0; // 1 can go left 0 can't
  159. right = 1; // 1 can go right 0 can't
  160. up = 0; // 1 can go up 0 can't
  161. down = 0; // 1 can go down 0 can't
  162. }
  163. if(RightorLeft == 1 && UporDown == 2) {
  164.  for(int i=0;i<8;i++) lc.setRow(0,i,room8[i]);
  165.  left = 1; // 1 can go left 0 can't
  166. right = 1; // 1 can go right 0 can't
  167. up = 1; // 1 can go up 0 can't
  168. down = 0; // 1 can go down 0 can't
  169. }
  170. if(RightorLeft == 2 && UporDown == 2) {
  171.  for(int i=0;i<8;i++) lc.setRow(0,i,room9[i]);
  172.  left = 1; // 1 can go left 0 can't
  173. right = 0; // 1 can go right 0 can't
  174. up = 1; // 1 can go up 0 can't
  175. down = 0; // 1 can go down 0 can't
  176. }
  177.  
  178. lcd.setCursor(0,1); // go to start of 2nd line
  179. lcd.print(RightorLeft);
  180. lcd.setCursor(1,1); // go to start of 2nd line
  181. lcd.print(UporDown);
  182.  
  183. lcd.setCursor(0,3); // display which ways player can go.
  184. if (left == 1) {
  185.   lcd.print("Left");
  186. } else {
  187.   lcd.print("    ");
  188. }
  189. lcd.setCursor(6,3); // display which ways player can go.
  190. if (right == 1) {
  191.   lcd.print("Right");
  192. }else {
  193.   lcd.print("     ");
  194. }
  195. lcd.setCursor(12,3); // display which ways player can go.
  196. if (up == 1) {
  197.   lcd.print("Up");
  198. }else {
  199.   lcd.print("  ");
  200. }
  201.  
  202.   lcd.setCursor(15,3); // display which ways player can go.
  203. if (down == 1) {
  204.   lcd.print("Down");
  205. }else {
  206.   lcd.print("    ");
  207. }
  208.  
  209.   lcd.setCursor(0,2); //print restart bit status
  210.   lcd.print(restart);
  211.  
  212.    if (restart == 1){ //restart game after winning
  213.   lcd.setCursor(0,3);
  214.   lcd.print("     Game Over      ");
  215.   delay(3000);
  216.   lcd.setCursor(0,3);
  217.   lcd.print("                    ");
  218.   restart = 0;
  219.     RightorLeft = 0;
  220.   UporDown = 1;
  221.    }
  222. Serial.println(RightorLeft);
  223.  }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×