Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- /*
- T E J S U M M A T I V E
- SALMAN A
- SHEZAN M
- BRAVIEN V
- */
- //delayTime
- int delayTime = 250;
- // lcd variables
- const int rs = 12;
- const int en = 11;
- const int d4 = 10;
- const int d5 = 9;
- const int d6 = 8;
- const int d7 = 7;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- // digital components
- int gled = 6;
- int bled = 2;
- int oled = 3;
- int rled = 4;
- // push buttons
- int gpushbutton = A1;
- int bpushbutton = A2;
- int opushbutton = A3;
- int rpushbutton = A4;
- // sequence
- void setup()
- {
- Serial.begin(9600);
- // output devices
- pinMode(gled, OUTPUT);
- pinMode(bled, OUTPUT);
- pinMode(oled, OUTPUT);
- pinMode(rled, OUTPUT);
- // input devices
- pinMode(gpushbutton, INPUT);
- pinMode(bpushbutton, INPUT);
- pinMode(opushbutton, INPUT);
- pinMode(rpushbutton, INPUT);
- //random setup
- randomSeed(analogRead(A0));
- // lcd setup
- lcd.begin(16, 2);
- }
- //********* M A I N G A M E **************//
- //declare variables
- int level = 1; //initialize level
- int numOfLights;
- bool gameLost = false;
- bool isInputComplete = false;
- void loop()
- {
- while(!gameLost) {
- //declare variables
- bool levelComplete = false;
- lcd.clear();
- lcd.print("Level: " + String(level));
- while(!levelComplete) {
- numOfLights = level/3+2; //the minimum number of leds is 2 and for every 3rd level one more led will be added
- //declare variables for levels
- int Light[numOfLights];
- int Inputs[numOfLights];
- createLevel(level,Light);
- getInput(level,Inputs);
- bool result = compare(Light,Inputs);
- if (result) {
- //update in the lcd
- lcd.clear();
- lcd.print("ROUND WON!");
- Serial.print("nice");
- level++;
- levelComplete = true;
- delay(3000);
- } else {
- levelComplete = true;
- gameLost= true;
- }
- }
- }
- //this part is when the user got the inputs wrong and game was lost
- //update on the lcd
- lcd.clear();
- lcd.print("GAME LOST!");
- Serial.println("gameLost");
- delay(3000);
- }
- //************* C A L C U L A T I O N S *************//
- //declare variables
- int randInt;
- /*
- this method will be called to create levels for the game
- it has the parameter level to know how many lights need to be turned on
- it has an array named Light where all the values of the pins are stored
- they will be used to later to compare with the input
- */
- void createLevel(int level, int Light[])
- {
- Light[numOfLights];
- for (int i=0; i<numOfLights ; i++) {
- Light[i]=random(1,5);
- if (Light[i] == 1) { digitalWrite(gled, HIGH); delay(500); digitalWrite(gled, LOW); delay(500); }
- if (Light[i] == 2) { digitalWrite(bled, HIGH); delay(500); digitalWrite(bled, LOW); delay(500);}
- if (Light[i] == 3) { digitalWrite(oled, HIGH); delay(500); digitalWrite(oled, LOW); delay(500);}
- if (Light[i] == 4) { digitalWrite(rled, HIGH); delay(500); digitalWrite(rled, LOW); delay(500);}
- ////////////////////////////////////////////////////
- isInputComplete = false;
- }
- }
- void getInput(int level, int Inputs[])
- {
- for (int i=0; i < numOfLights; )
- {
- if (digitalRead(gpushbutton) == HIGH) {
- Inputs[i] = 1;
- Serial.println("green pressed");
- digitalWrite(gled, HIGH);
- i++;
- //delay a bit
- delay(delayTime);
- digitalWrite(gled, LOW);
- }
- if (digitalRead(bpushbutton) == HIGH) {
- Inputs[i] = 2;
- Serial.println("blue pressed");
- digitalWrite(bled, HIGH);
- i++;
- //delay a bit
- delay(delayTime);
- digitalWrite(bled, LOW);
- }
- if (digitalRead(opushbutton) == HIGH) {
- Inputs[i] = 3;
- Serial.println("orange pressed");
- //test
- digitalWrite(oled, HIGH);
- i++;
- //delay a bit
- delay(delayTime);
- digitalWrite(oled, LOW);
- }
- if (digitalRead(rpushbutton) == HIGH) {
- Inputs[i] = 4;
- Serial.println("red pressed");
- i++;
- digitalWrite(rled, HIGH);
- //delay a bit
- delay(delayTime);
- digitalWrite(rled, LOW);
- }
- }
- }
- /*
- this method 'compare' will return a boolean value of if the Level(that was created) match with the Inputs(by the user)
- it works by using a while loop with the condition that they values inside the array has to match.
- inside the while loop we have a for loop which goes through all the values in the array and see if they match and
- 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
- */
- bool compare(int Light[], int Inputs[]) {
- bool theyMatch=true;
- bool foundMistake = false;
- for (int i=0; i<numOfLights && foundMistake == false; i++)
- {
- if (Light[i] == Inputs[i]) {
- theyMatch=true;
- Serial.println("they match");
- } else {
- theyMatch=false;
- foundMistake=true;
- }
- }
- return theyMatch;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement