Dependencies:   N5110 mbed

Revision:
30:e6f59801ea2a
Parent:
29:f70d927c3e11
--- a/main.cpp	Wed May 06 14:24:49 2015 +0000
+++ b/main.cpp	Wed May 06 17:17:38 2015 +0000
@@ -1,6 +1,6 @@
 /**
 @file main.cpp
- 
+
 @brief Game Algorithm implementation
  */
 
@@ -8,21 +8,22 @@
 
 int main()
 {
-     //set_time(0); // enter unix time then delete from main function to set time
+    //set_time(0); // enter unix time then delete from main function to set time
     power = 1;
-    lcd.init(); 
+    lcd.init();
     calibrateJoystick();  // get centred values of joystick
     pollJoystick.attach(&updateJoystick,1.0/15.0);  // read joystick 15 times per second
     wallMovement.attach(&flagForWall,0.2); // call function flagForWall to make flag=1, every 0.2 seconds
     wallMovement2.attach(&flagForWall2,0.2); // call function flagForWall2 to make flag=1, every 0.2 seconds
     wallMovement3.attach(&flagForWall3,0.2); // call function flagForWall3 to make flag=1, every 0.2 seconds
     flash.attach(&flagForFlash,1); // call function flagForWall3 to make flag=1, every 1 seconds
+    button.rise(&buttonFlag); // call flag when ever joystick button is pressed
     menu();
 }
 
 void calibrateJoystick()
 {
-    button.mode(PullDown); // must not move while calibrating 
+    button.mode(PullDown); // must not move while calibrating
     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
     joystick.y0 = yPot;
 }
@@ -56,19 +57,19 @@
         joystick.direction = downLeft;
     }
 
-    printFlag = 1; // set flag for printing 
+    printFlag = 1; // set flag for printing
 }
 
 
 
 void clearCells()
 {
-    for (int i = 0; i < nx ; i++) { // loops through cells in x direction first 
-        for (int j = 0; j < ny ; j++) { // then moves in y direction 
-            lcd.clearPixel(i,j); // clears each pixel that is checked 
+    for (int i = 0; i < nx ; i++) { // loops through cells in x direction first
+        for (int j = 0; j < ny ; j++) { // then moves in y direction
+            lcd.clearPixel(i,j); // clears each pixel that is checked
         }
     }
-    lcd.refresh(); // must refresh to write buffer display 
+    lcd.refresh(); // must refresh to write buffer display
 }
 
 
@@ -89,20 +90,25 @@
     FLAG3=1;
 }
 
+void buttonFlag()
+{
+    BFlag =1;
+}
 
 void menu()
 {
     int m = 0;
+    BFlag = 0;
 
-    char buffer[14]; // buffer used to store time string 
+    char buffer[14]; // buffer used to store time string
 
     while(1) {
-        powerSave(); // call sleep function 
+        powerSave(); // call sleep function
         clearCells();
-        time_t seconds = time(NULL); // gets current time 
-        strftime(buffer, 14 , "%H:%M", localtime(&seconds)); // send time string to buffer 
+        time_t seconds = time(NULL); // gets current time
+        strftime(buffer, 14 , "%H:%M", localtime(&seconds)); // send time string to buffer
 
-        lcd.printString(buffer,26,4); // show time on lcd screen 
+        lcd.printString(buffer,26,4); // show time on lcd screen
 
 
         if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) {
@@ -121,44 +127,33 @@
             case 0 :
                 lcd.printString("<  Play Game >",0,2);
                 wait(0.3);
-                while(1) {
-                    if(button == 1) {
-                        clearCells();
-                        playGame();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag=0;
+                    clearCells();
+                    playGame();
                 }
                 break;
             case 1 :
                 lcd.printString("<  Settings  >",0,2);
                 wait(0.3);
-                while(1) {
-                    if(button == 1) {
-                        clearCells();
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag=0;
+                    clearCells();
+                    BandVMenu();
                 }
                 break;
             case 2 :
                 lcd.printString("<  HighScore >",0,2);
                 wait(0.3);
-                while(1) {
-                    if(button == 1) {
-                        clearCells();
-                        while(1) {
-                            readDataFromFile();
-                            lcd.printString("HighScore",16,0);
-                            if(joystick.direction == DOWN) {
-                                menu();
-                            }
+                if(BFlag) {
+                    BFlag=0;
+                    clearCells();
+                    while(1) {
+                        readDataFromFile();
+                        lcd.printString("HighScore",16,0);
+                        if(joystick.direction == DOWN) {
+                            menu();
                         }
-                    } else {
-                        break;
                     }
                 }
                 break;
@@ -191,29 +186,29 @@
     lcd.printString("GO!",35,2);
     wait(1);
     while(1) {
-        if(soundFlag==1) { // if mute hasn't been toggled in settings then the song will play 
+        if(soundFlag==1) { // if mute hasn't been toggled in settings then the song will play
             tone();
         }
-        lcd.drawRect(i,j,2,2,0); // draws player object 
+        lcd.drawRect(i,j,2,2,0); // draws player object
         clearCells();
         fallingWalls();
         boundries();
         playerMovement();
-        
-        if (lcd.getPixel(i,j)) { // if wall meets player object then there is a collision  
+
+        if (lcd.getPixel(i,j)) { // if wall meets player object then there is a collision
             scoreCheck();
             int m = 1;
-            GameOverFlash(); // this means that it is game over 
+            GameOverFlash(); // this means that it is game over
             if (soundFlag == 1) {
                 deadTone();
             }
 
             while(1) {
                 clearCells();
-                if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { // reads joystick direction and counts down 
+                if ((joystick.direction == RIGHT)||(joystick.direction == upRight)||(joystick.direction == downRight)) { // reads joystick direction and counts down
                     m--;
                 }
-                if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { // reads joystick direction and counts up 
+                if ((joystick.direction == LEFT)||(joystick.direction == upLeft)||(joystick.direction == downLeft)) { // reads joystick direction and counts up
                     m++;
                 }
                 if(m > 2) { // stops counter from going above total number of cases
@@ -222,18 +217,13 @@
                 if(m < 0) { // stops counter from going below zero
                     m = 0;
                 }
-                switch (m) { //switches to the case that the counter is on 
+                switch (m) { //switches to the case that the counter is on
                     case 0 :
                         lcd.printString("YES",0,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                playGame();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag=0;
+                            clearCells();
+                            playGame();
                         }
                         break;
                     case 1 :
@@ -244,23 +234,18 @@
                         break;
                     case 2 :
                         lcd.printString("NO",70,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                lcd.printString("Leaving Game",0,2); 
-                                wait(1.0);
-                                menu();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            lcd.printString("Leaving Game",0,2);
+                            wait(1.0);
+                            menu();
                         }
                         break;
                 }
             }
         }
-        if (lcd.getPixel(i+2,j)) { // same as above but for different side of player object 
+        if (lcd.getPixel(i+2,j)) { // same as above but for different side of player object
 
             scoreCheck();
             clearCells();
@@ -287,15 +272,10 @@
                 switch (m) {
                     case 0 :
                         lcd.printString("YES",0,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                playGame();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            playGame();
                         }
                         break;
                     case 1 :
@@ -306,24 +286,19 @@
                         break;
                     case 2 :
                         lcd.printString("NO",70,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                lcd.printString("Leaving Game",0,2);
-                                wait(1.0);
-                                menu();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            lcd.printString("Leaving Game",0,2);
+                            wait(1.0);
+                            menu();
                         }
                         break;
                 }
             }
         }
         if (lcd.getPixel(i+2,j+2)) {
-            scoreCheck(); 
+            scoreCheck();
             clearCells();
             int m = 1;
             GameOverFlash();
@@ -348,15 +323,10 @@
                 switch (m) {
                     case 0 :
                         lcd.printString("YES",0,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                playGame();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            playGame();
                         }
                         break;
                     case 1 :
@@ -367,17 +337,12 @@
                         break;
                     case 2 :
                         lcd.printString("NO",70,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                lcd.printString("Leaving Game",0,2); 
-                                wait(1.0);
-                                menu();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            lcd.printString("Leaving Game",0,2);
+                            wait(1.0);
+                            menu();
                         }
                         break;
 
@@ -385,7 +350,7 @@
             }
         }
         if (lcd.getPixel(i,j+2)) {
-            scoreCheck(); 
+            scoreCheck();
             clearCells();
             int m = 1;
             GameOverFlash();
@@ -410,15 +375,10 @@
                 switch (m) {
                     case 0 :
                         lcd.printString("YES",0,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                playGame();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            playGame();
                         }
                         break;
                     case 1 :
@@ -429,17 +389,12 @@
                         break;
                     case 2 :
                         lcd.printString("NO",70,3);
-
-                        while(1) {
-                            if(button == 1) {
-                                clearCells();
-                                lcd.printString("Leaving Game",0,2); 
-                                wait(1.0);
-                                menu();
-                                break;
-                            } else {
-                                break;
-                            }
+                        if(BFlag) {
+                            BFlag = 0;
+                            clearCells();
+                            lcd.printString("Leaving Game",0,2);
+                            wait(1.0);
+                            menu();
                         }
                         break;
 
@@ -478,35 +433,23 @@
         switch (t) {
             case 0 :
                 lcd.printString("< Brightness >",0,2);
-                while(1) {
-                    if(button == 1) {
-                        brightness();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    brightness();
                 }
                 break;
             case 1 :
                 lcd.printString("<   Volume   >",0,2);
-                while(1) {
-                    if(button == 1) {
-                        volume();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    volume();
                 }
                 break;
             case 2 :
                 lcd.printString("<Instructions>",0,2);
-                while(1) {
-                    if(button == 1) {
-                        instructions();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    instructions();
                 }
                 break;
         }
@@ -536,90 +479,62 @@
                 lcd.printString("  Brightness ",0,2);
                 lcd.printString(" ||||||",0,4);
                 lcd.setBrightness(0.6);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case 1 :
                 lcd.printString("  Brightness ",0,2);
                 lcd.printString(" ||||||||",0,4);
                 lcd.setBrightness(0.8);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case 2 :
                 lcd.printString("  Brightness ",0,2);
                 lcd.printString(" ||||||||||",0,4);
                 lcd.setBrightness(0.9);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case 3 :
                 lcd.printString("  Brightness ",0,2);
                 lcd.printString(" ||||||||||||",0,4);
                 lcd.setBrightness(1);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case -1 :
                 lcd.printString("  Brightness ",0,2);
                 lcd.printString(" ||||",0,4);
                 lcd.setBrightness(0.4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case -2 :
                 lcd.printString("  Brightness ",0,2);
                 lcd.printString(" ||",0,4);
                 lcd.setBrightness(0.2);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case -3 :
                 lcd.printString("  Brightness ",0,2);
                 lcd.setBrightness(0);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
         }
@@ -648,86 +563,58 @@
             case 0 :
                 lcd.printString("    Volume   ",0,2);
                 lcd.printString(" ||||||",0,4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case 1 :
                 lcd.printString("    Volume   ",0,2);
                 lcd.printString(" ||||||||",0,4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case 2 :
                 lcd.printString("    Volume   ",0,2);
                 lcd.printString(" ||||||||||",0,4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case 3 :
                 lcd.printString("    Volume   ",0,2);
                 lcd.printString(" ||||||||||||",0,4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case -1 :
                 lcd.printString("    Volume   ",0,2);
                 lcd.printString(" ||||",0,4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case -2 :
                 lcd.printString("    Volume   ",0,2);
                 lcd.printString(" ||",0,4);
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
             case -3 :
                 lcd.printString("    Volume    ",0,2);
                 lcd.printString(" MUTE",0,4);
                 soundFlag = 0;
-                while(1) {
-                    if(button == 1) {
-                        BandVMenu();
-                        break;
-                    } else {
-                        break;
-                    }
+                if(BFlag) {
+                    BFlag = 0;
+                    BandVMenu();
                 }
                 break;
         }
@@ -737,30 +624,30 @@
 
 void writeDataToFile()
 {
-    time_t seconds = time(NULL); // get current time 
-    char buffer2[14]; // buffer used to store time string 
-    strftime(buffer2, 14, " %d/%m/%y", localtime(&seconds)); // displays unix time in day/ month/ year format and sends it to buffer 
+    time_t seconds = time(NULL); // get current time
+    char buffer2[14]; // buffer used to store time string
+    strftime(buffer2, 14, " %d/%m/%y", localtime(&seconds)); // displays unix time in day/ month/ year format and sends it to buffer
 
-    FILE *fp = fopen("/local/score.txt", "w"); // open file called 'score.txt' 
-/* if the file doesn't exist it is created, if it exists, data is updated */
-    fprintf(fp,"%i%s",score,buffer2); // print integer for score and date to file 
-    fclose(fp); // close file 
+    FILE *fp = fopen("/local/score.txt", "w"); // open file called 'score.txt'
+    /* if the file doesn't exist it is created, if it exists, data is updated */
+    fprintf(fp,"%i%s",score,buffer2); // print integer for score and date to file
+    fclose(fp); // close file
 }
 
 
 void readDataFromFile()
 {
-    int score1; // states the score to be read from the file is an integer value 
+    int score1; // states the score to be read from the file is an integer value
     char buffer2recieved[14]; // buffer to recieve the string of data read from the text file
 
-    FILE *fp2 = fopen("/local/score.txt", "r"); // open 'score.txt' 
-    fscanf (fp2,"%i%s",&score1,buffer2recieved); // scans text file for an integer followed by a string 
-    fclose(fp2); // close file 
+    FILE *fp2 = fopen("/local/score.txt", "r"); // open 'score.txt'
+    fscanf (fp2,"%i%s",&score1,buffer2recieved); // scans text file for an integer followed by a string
+    fclose(fp2); // close file
 
     char Points[14]; // buffer to store score value
-    sprintf(Points,"%i",score1); // send score to buffer (Points) 
-    lcd.printString(Points,39,2); // print score on LCD 
-    lcd.printString(buffer2recieved,18,4); // print date on LCD 
+    sprintf(Points,"%i",score1); // send score to buffer (Points)
+    lcd.printString(Points,39,2); // print score on LCD
+    lcd.printString(buffer2recieved,18,4); // print date on LCD
 
 }
 
@@ -805,10 +692,10 @@
 
     FILE *fp2 = fopen("/local/score.txt", "r");
     fscanf (fp2,"%i",&score1);
-    fclose(fp2); 
+    fclose(fp2);
 
     if(score1 < score) {
-        writeDataToFile(); // if the score is bigger then the data is written to the file and overwrites the previous data 
+        writeDataToFile(); // if the score is bigger then the data is written to the file and overwrites the previous data
     }
 }
 
@@ -823,12 +710,12 @@
 {
     int flash = 0;
     while(1) {
-        if (flashFlag==1) { // flag equals one every second  
-            flashFlag = 0; // flag is reset  
-            lcd.printString("Game Over!",12,2); // print phrase  
-            flash++; // adds one to counter 
+        if (flashFlag==1) { // flag equals one every second
+            flashFlag = 0; // flag is reset
+            lcd.printString("Game Over!",12,2); // print phrase
+            flash++; // adds one to counter
         }
-        if (flash > 3) { // moves onto next function when counter gets to 3 
+        if (flash > 3) { // moves onto next function when counter gets to 3
             break;
         }
     }
@@ -843,16 +730,17 @@
             sleepCounter++;
         }
         if(sleepCounter > 20) {
-            Sleep(); // put mbed into sleep mode 
-            lcd.turnOff(); // turn LCD off 
-            while(1) {
-            if (button ==1) {
-                lcd.init(); // turn LCD back on 
+            Sleep(); // put mbed into sleep mode
+            lcd.turnOff(); // turn LCD off
+            while(1){
+            if(BFlag) {
+                BFlag = 0;
+                lcd.init(); // turn LCD back on
                 sleepCounter = 0;
+                wait(0.5);
                 menu();
-                break;
-                }
-                }
+            }
+            }
         }
 
     } else {
@@ -863,7 +751,7 @@
 
 void boundries()
 {
-    if( j > 45) { 
+    if( j > 45) {
         j = 45;
     }
     if( i > 81) {
@@ -881,7 +769,7 @@
 void playerMovement()
 {
 
-    if (printFlag) {  // if flag set, clear flag and implicate joystick functions 
+    if (printFlag) {  // if flag set, clear flag and implicate joystick functions
         printFlag = 0;
         /* check joystick direction */
         if (joystick.direction == UP) {
@@ -922,93 +810,92 @@
 
 void fallingWalls()
 {
-      lcd.drawLine(x,y,z,y,1); // draws a line at the top of the screen to symbolise a wall 
-        lcd.drawLine(z+10,y,83,y,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it 
-        if(FLAG==1) { // the flag changes every 0.2 seconds 
-            FLAG=0;
-            y++;  // everytime the flag changes to one it is reset and the wall moves down by one pixel 
-        }
-        if(y > 47) { // when the wall reaches the bottom a new wall is generated at the top 
-            y = 0;
-            z = rand()%74; // the gap in the is moved to a random postion in the new wall 
-            score++; // score increases by one everytime a wall falling from the top reaches the bottom of the screen 
+    lcd.drawLine(x,y,z,y,1); // draws a line at the top of the screen to symbolise a wall
+    lcd.drawLine(z+10,y,83,y,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it
+    if(FLAG==1) { // the flag changes every 0.2 seconds
+        FLAG=0;
+        y++;  // everytime the flag changes to one it is reset and the wall moves down by one pixel
+    }
+    if(y > 47) { // when the wall reaches the bottom a new wall is generated at the top
+        y = 0;
+        z = rand()%74; // the gap in the is moved to a random postion in the new wall
+        score++; // score increases by one everytime a wall falling from the top reaches the bottom of the screen
+    }
+    /* there are 6 levels of difficulty */
+    if(( score > 1)&&(score < 5)) {
+        lcd.drawLine(a,b,d,b,1);
+        lcd.drawLine(d+10,b,83,b,1);
+        if(FLAG3==1) {
+            FLAG3=0;
+            b--;
         }
-        /* there are 6 levels of difficulty */
-        if(( score > 1)&&(score < 5)) {
-            lcd.drawLine(a,b,d,b,1); 
-            lcd.drawLine(d+10,b,83,b,1); 
-            if(FLAG3==1) {
-                FLAG3=0;
-                b--;  
-            }
-        }
-        if(b < 0) { 
-            b = 47;
-            d = rand()%74;
-        }
-        if(( score > 5)&&(score < 9)) {
-            lcd.drawLine(f,g,f,h,1);
-            lcd.drawLine(f,h+10,f,47,1);
-            if(FLAG2==1) {
-                FLAG2=0;
-                f++;  
-            }
+    }
+    if(b < 0) {
+        b = 47;
+        d = rand()%74;
+    }
+    if(( score > 5)&&(score < 9)) {
+        lcd.drawLine(f,g,f,h,1);
+        lcd.drawLine(f,h+10,f,47,1);
+        if(FLAG2==1) {
+            FLAG2=0;
+            f++;
         }
-        if( f > 83) { 
-            f = 0;
-            h = rand()%38;
-        }
-        if(( score > 10)&&(score < 15)) {
-            lcd.drawLine(q,w,q,e,1); 
-            lcd.drawLine(q,e+10,q,47,1); 
-            if(FLAG3==1) {
-                FLAG3=0;
-                q--;  
-            }
-        }
-        if(q < 0) { 
-            q = 83;
-            e = rand()%38;
+    }
+    if( f > 83) {
+        f = 0;
+        h = rand()%38;
+    }
+    if(( score > 10)&&(score < 15)) {
+        lcd.drawLine(q,w,q,e,1);
+        lcd.drawLine(q,e+10,q,47,1);
+        if(FLAG3==1) {
+            FLAG3=0;
+            q--;
         }
-        if( score > 15) {
-            lcd.drawLine(q,w,q,e,1); 
-            lcd.drawLine(q,e+10,q,47,1); 
-            if(FLAG3==1) {
-                FLAG3=0;
-                q--;  
-            }
+    }
+    if(q < 0) {
+        q = 83;
+        e = rand()%38;
+    }
+    if( score > 15) {
+        lcd.drawLine(q,w,q,e,1);
+        lcd.drawLine(q,e+10,q,47,1);
+        if(FLAG3==1) {
+            FLAG3=0;
+            q--;
         }
-        if( score > 20) {
-            lcd.drawLine(a,b,d,b,1); 
-            lcd.drawLine(d+10,b,83,b,1); 
-            if(FLAG2==1) {
-                FLAG2=0;
-                b--;  
-            }
+    }
+    if( score > 20) {
+        lcd.drawLine(a,b,d,b,1);
+        lcd.drawLine(d+10,b,83,b,1);
+        if(FLAG2==1) {
+            FLAG2=0;
+            b--;
         }
-        }
-     
+    }
+}
+
 void instructions()
 {
-    while(1) 
-    {
+    while(1) {
         lcd.printString("Welcome :-)",9,0);
         lcd.printString("Select using",6,1);
-        lcd.printString("joystickButton",0,2); 
+        lcd.printString("joystickButton",0,2);
         lcd.printString("Press >",27,4);
         wait(0.5);
-        if(button) { // press joystick button to continue
+        if(BFlag) {
+            BFlag = 0; // press joystick button to continue
             clearCells();
-            while(1)
-            {
+            while(1) {
                 lcd.printString("Return to menu",0,0);
                 lcd.printString("by moving the",3,1);
                 lcd.printString("JS downward",9,2);
                 if(joystick.direction == DOWN) { // direct joystick down to continue
                     menu();
                     break;
-                    }
-                    }
-                    }
-                    }
-                    }
\ No newline at end of file
+                }
+            }
+        }
+    }
+}
\ No newline at end of file