Advertisement
Guest User

tej_summative_jan2018

a guest
Jan 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. /*
  4. T E J   S U M M A T I V E
  5. SALMAN A
  6. SHEZAN M
  7. BRAVIEN V
  8. */
  9.  
  10. //delayTime
  11. int delayTime = 250;
  12.  
  13. // lcd variables
  14. const int rs = 12;
  15. const int en = 11;
  16. const int d4 = 10;
  17. const int d5 = 9;
  18. const int d6 = 8;
  19. const int d7 = 7;
  20. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  21.  
  22. // digital components
  23. int gled = 6;
  24. int bled = 2;
  25. int oled = 3;
  26. int rled = 4;
  27.  
  28. // push buttons
  29. int gpushbutton = A1;
  30. int bpushbutton = A2;
  31. int opushbutton = A3;
  32. int rpushbutton = A4;
  33.  
  34. // sequence
  35.  
  36.  
  37. void setup()
  38. {
  39.  
  40.   Serial.begin(9600);
  41.  
  42.   // output devices
  43.   pinMode(gled, OUTPUT);
  44.   pinMode(bled, OUTPUT);
  45.   pinMode(oled, OUTPUT);
  46.   pinMode(rled, OUTPUT);
  47.  
  48.   // input devices
  49.   pinMode(gpushbutton, INPUT);
  50.   pinMode(bpushbutton, INPUT);
  51.   pinMode(opushbutton, INPUT);
  52.   pinMode(rpushbutton, INPUT);
  53.  
  54.  
  55.  
  56.  
  57.   //random setup
  58.   randomSeed(analogRead(A0));
  59.  
  60.  
  61.   // lcd setup
  62.   lcd.begin(16, 2);
  63.  
  64. }
  65.  
  66.  
  67. //********* M A I N   G A M E **************//
  68.  
  69. //declare variables
  70. int level = 1; //initialize level
  71. int numOfLights;
  72. bool gameLost = false;
  73. bool isInputComplete = false;
  74.  
  75. void loop()
  76. {
  77.   while(!gameLost) {
  78.     //declare variables
  79.     bool levelComplete = false;
  80.     lcd.clear();
  81.     lcd.print("Level: " + String(level));
  82.    
  83.    
  84.    
  85.    
  86.     while(!levelComplete) {
  87.       numOfLights = level/3+2; //the minimum number of leds is 2 and for every 3rd level one more led will be added
  88.       //declare variables for levels
  89.       int Light[numOfLights];
  90.       int Inputs[numOfLights];
  91.      
  92.       createLevel(level,Light);
  93.       getInput(level,Inputs);
  94.      
  95.      
  96.      
  97.       bool result = compare(Light,Inputs);
  98.      
  99.       if (result) {
  100.         //update in the lcd
  101.         lcd.clear();
  102.         lcd.print("ROUND WON!");
  103.         Serial.print("nice");
  104.         level++;
  105.         levelComplete = true;
  106.         delay(3000);
  107.       } else {
  108.         levelComplete = true;
  109.         gameLost= true;
  110.       }
  111.  
  112.     }
  113.    
  114.  
  115.    
  116.   }
  117.  
  118.  //this part is when the user got the inputs wrong and game was lost
  119.   //update on the lcd
  120.   lcd.clear();
  121.   lcd.print("GAME LOST!");
  122.   Serial.println("gameLost");
  123.   delay(3000);
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130. //************* C A L C U L A T I O N S *************//
  131.  
  132. //declare variables
  133. int randInt;
  134. /*
  135. this method will be called to create levels for the game
  136. it has the parameter level to know how many lights need to be turned on
  137. it has an array named Light where all the values of the pins are stored
  138. they will be used to later to compare with the input
  139. */
  140. void createLevel(int level, int Light[])
  141. {
  142.   Light[numOfLights];
  143.   for (int i=0; i<numOfLights ; i++) {
  144.     Light[i]=random(1,5);
  145.    
  146.     if (Light[i] == 1) { digitalWrite(gled, HIGH); delay(500); digitalWrite(gled, LOW); delay(500); }
  147.     if (Light[i] == 2) { digitalWrite(bled, HIGH); delay(500); digitalWrite(bled, LOW); delay(500);}
  148.     if (Light[i] == 3) { digitalWrite(oled, HIGH); delay(500); digitalWrite(oled, LOW); delay(500);}
  149.     if (Light[i] == 4) { digitalWrite(rled, HIGH); delay(500); digitalWrite(rled, LOW); delay(500);}
  150.     ////////////////////////////////////////////////////
  151.  
  152.     isInputComplete = false;
  153.   }
  154. }
  155.  
  156.  
  157. void getInput(int level, int Inputs[])
  158. {
  159.     for (int i=0; i < numOfLights; )
  160.     {
  161.         if (digitalRead(gpushbutton) == HIGH) {
  162.             Inputs[i] = 1;
  163.             Serial.println("green pressed");
  164.             digitalWrite(gled, HIGH);
  165.             i++;
  166.             //delay a bit
  167.             delay(delayTime);
  168.             digitalWrite(gled, LOW);
  169.         }
  170.         if (digitalRead(bpushbutton) == HIGH) {
  171.             Inputs[i] = 2;
  172.             Serial.println("blue pressed");
  173.             digitalWrite(bled, HIGH);
  174.             i++;
  175.           //delay a bit
  176.             delay(delayTime);
  177.             digitalWrite(bled, LOW);
  178.         }
  179.         if (digitalRead(opushbutton) == HIGH) {
  180.             Inputs[i] = 3;
  181.             Serial.println("orange pressed");
  182.             //test
  183.             digitalWrite(oled, HIGH);
  184.             i++;
  185.           //delay a bit
  186.             delay(delayTime);
  187.             digitalWrite(oled, LOW);
  188.         }
  189.         if (digitalRead(rpushbutton) == HIGH) {
  190.             Inputs[i] = 4;
  191.             Serial.println("red pressed");
  192.             i++;
  193.             digitalWrite(rled, HIGH);
  194.           //delay a bit
  195.             delay(delayTime);
  196.             digitalWrite(rled, LOW);
  197.         }
  198.     }
  199. }
  200.  
  201. /*
  202. this method 'compare' will return a boolean value of if the Level(that was created) match with the Inputs(by the user)
  203. it works by using a while loop with the condition that they values inside the array has to match.
  204. inside the while loop we have a for loop which goes through all the values in the array and see if they match and
  205. if it find any that don't match it will end the while loops and return that the boolean (theyMatch = false), which tells us that the user has lost
  206. */
  207. bool compare(int Light[], int Inputs[]) {
  208.   bool theyMatch=true;
  209.   bool foundMistake = false;
  210.  
  211.   for (int i=0; i<numOfLights && foundMistake == false; i++)
  212.   {
  213.     if (Light[i] == Inputs[i]) {
  214.       theyMatch=true;
  215.       Serial.println("they match");
  216.     } else {
  217.       theyMatch=false;
  218.       foundMistake=true;
  219.     }
  220.   }
  221.  
  222.   return theyMatch;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement