Guitar Hero Project on LPC1768 using push buttons, analog joystick. speaker and LCD Display

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SparkfunAnalogJoystick SDFileSystem_OldbutworkswithRTOS PinDetect

Revision:
7:5ac5056cde2c
Parent:
6:4bc137ec1022
Child:
8:32a0bb14dbd2
--- a/main.cpp	Thu Nov 26 16:40:43 2020 +0000
+++ b/main.cpp	Sun Nov 29 19:03:38 2020 +0000
@@ -6,13 +6,16 @@
 #include "uLCD_4DGL.h"
 #include "PinDetect.h"
 #include "song1.h"
-
+#include "SparkfunAnalogJoystick.h"
 #define sample_freq 8000.0
 //code change
 Mutex lcd_mut;
 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
+//AnalogOut DACout(p18);
+//wave_player waver(&DACout);
 AnalogOut speaker(p18);
+SparkfunAnalogJoystick joy(p16, p17, p15);
 PinDetect pb_1(p9, PullUp);
 PinDetect pb_2(p10, PullUp);
 PinDetect pb_3(p11, PullUp);
@@ -29,9 +32,9 @@
 #define YELLOW 16709376
 
 //speed based on difficulty
-#define Easy 2   
-#define Medium 5     
-#define Hard 8       
+#define Easy 10   
+#define Medium 15     
+#define Hard 20     
 
 
 DigitalOut myled1(LED1);
@@ -41,23 +44,608 @@
 
 Ticker sampletick;
 
-volatile int Game_State = Game_Over;
+volatile int Game_State = Main_Menu;
 volatile bool draw_enable = true;
 volatile int Speed = Easy; 
 
+volatile int y1;
+volatile int y2;
+volatile int y3;
+volatile int y4;
 volatile int score = 0;
-volatile int high_score = 0;
-volatile int hits = 0;
-volatile int misses = 0;
+volatile int highScore = 0;
+volatile int easyHigh = 0;
+volatile int medHigh = 0;
+volatile int hardHigh = 0;
+volatile int hit = 0;
+volatile int miss = 0;
+volatile int totalBubbles = 0;
+
+volatile bool greenRelease = false;
+volatile bool redRelease = false;
+volatile bool yellowRelease = false;
+volatile bool blueRelease = false;
+
+volatile bool greenPress = false;
+volatile bool redPress = false;
+volatile bool yellowPress = false;
+volatile bool bluePress = false;
+
 int i=0;
+
+void otherMode(int speed) {
+    int scoreUp;
+    if (speed == Medium) {
+        y1 = 5;
+        y2 = 5;
+        y3 = 5;
+        y4 = 5;
+        scoreUp = 1000;
+    } else {
+        y1 = 10;
+        y2 = 10;
+        y3 = 10;
+        y4 = 10;
+        scoreUp = 1500;
+    }
+    int pattern = 1;
+    score = 0;
+    hit = 0;
+    totalBubbles = 0;
+    Speed = speed;
+    //uLCD.cls();
+    //uLCD.printf("GameStart");
+    uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
+    uLCD.background_color(BLACK);
+    Thread::wait(1000);
+        uLCD.circle(10,110, 8, GREEN);
+        uLCD.circle(45,110, 8, RED);
+        uLCD.circle(80,110, 8, YELLOW);
+        uLCD.circle(115,110, 8, BLUE);
+    Thread::wait(750);
+    switch (pattern) {
+        case 1:
+            totalBubbles += 4;
+            greenPress = false;
+            redPress = false;
+            yellowPress = false;
+            bluePress = false;
+            redRelease = false;
+            yellowRelease = false;
+            greenRelease = false;
+            blueRelease = false;
+            if (speed == Medium) {
+                y1 = 5;
+                y2 = 5;
+                y3 = 5;
+                y4 = 5;
+            } else {
+                y1 = 30;
+                y2 = 30;
+                y3 = 30;
+                y4 = 30;
+            }
+            while (y4 <= 130) {
+                if(y1 <= 130) {
+                    y1 += Speed;
+                }
+                if (redRelease) {
+                    y2+= Speed;
+                }
+                if (yellowRelease) {
+                    y3+= Speed;
+                }
+                if (blueRelease) {
+                    y4 += Speed;
+                }
+                
+                if(y1 <= 130 && !greenPress) {
+                    uLCD.filled_circle(10,y1, 8 , GREEN);
+                }
+                if ((y1 >= 31 && y2 <= 130) && !redPress ) {
+                    uLCD.filled_circle(45,y2, 8 , RED);
+                    redRelease = true;
+                }
+                if ((y2 >= 31 && y3 <= 130) && !yellowPress) {
+                    uLCD.filled_circle(80,y3, 8 , YELLOW);
+                    yellowRelease = true;
+                }
+                if((y3 >= 31 && y4 <= 130) && !bluePress) {
+                    uLCD.filled_circle(115,y4, 8 , BLUE);
+                    blueRelease = true;
+                }
+                Thread::wait(250);
+                uLCD.filled_circle(10, y1, 8 , BLACK);
+                uLCD.filled_circle(45, y2, 8 , BLACK);
+                uLCD.filled_circle(80, y3, 8 , BLACK);
+                uLCD.filled_circle(115, y4, 8 , BLACK);
+                
+                if (y1 == 110 && greenPress) {
+                    //uLCD.filled_circle(10, y1, 8 , WHITE);
+                    uLCD.circle(10,110, 8, GREEN);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(10, y1, 8 , BLACK);
+                    uLCD.circle(10,110, 8, GREEN);
+                }
+                if (y2 == 110 && redPress) {
+                    //uLCD.filled_circle(45, y2, 8 , WHITE);
+                    uLCD.circle(45,110, 8, RED);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(45, y2, 8 , BLACK);
+                    uLCD.circle(45,110, 8, RED);
+                }
+                if(y3 == 110 && yellowPress) {
+                    //uLCD.filled_circle(80, y3, 8 , WHITE);
+                    uLCD.circle(80,110, 8, YELLOW);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(80, y3, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                }
+                if(y4 == 110 && bluePress) {
+                    uLCD.circle(115,110, 8, BLUE);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(115, y4, 8 , BLACK);
+                    uLCD.circle(115,110, 8, BLUE);
+                }
+            }
+            
+        case 2: //green and red then yellow and blue
+            totalBubbles += 4;
+            greenPress = false;
+            redPress = false;
+            yellowPress = false;
+            bluePress = false;
+            redRelease = false;
+            yellowRelease = false;
+            greenRelease = false;
+            blueRelease = false;
+            if (speed == Medium) {
+                y1 = 5;
+                y2 = 5;
+                y3 = 5;
+                y4 = 5;
+            } else {
+                y1 = 10;
+                y2 = 10;
+                y3 = 10;
+                y4 = 10;
+            }
+            while (y4 <= 130) {
+                if(y1 <= 130) {
+                    y1 += Speed;
+                }
+                if (y2 <= 130) {
+                    y2+= Speed;
+                }
+                if (yellowRelease) {
+                    y3+= Speed;
+                }
+                if (blueRelease) {
+                    y4 += Speed;
+                }
+                
+                if(y1 <= 130 && !greenPress) {
+                    uLCD.filled_circle(10,y1, 8 , GREEN);
+                }
+                if (y2 <= 130 && !redPress ) {
+                    uLCD.filled_circle(45,y2, 8 , RED);
+                    redRelease = true;
+                }
+                if ((y2 >= 60 && y3 <= 130) && !yellowPress) {
+                    uLCD.filled_circle(80,y3, 8 , YELLOW);
+                    yellowRelease = true;
+                }
+                if((y2 >= 60 && y4 <= 130) && !bluePress) {
+                    uLCD.filled_circle(115,y4, 8 , BLUE);
+                    blueRelease = true;
+                }
+                Thread::wait(250);
+                uLCD.filled_circle(10, y1, 8 , BLACK);
+                uLCD.filled_circle(45, y2, 8 , BLACK);
+                uLCD.filled_circle(80, y3, 8 , BLACK);
+                uLCD.filled_circle(115, y4, 8 , BLACK);
+                
+                if (y1 == 110 && greenPress) {
+                    //uLCD.filled_circle(10, y1, 8 , WHITE);
+                    uLCD.circle(10,110, 8, GREEN);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(10, y1, 8 , BLACK);
+                    uLCD.circle(10,110, 8, GREEN);
+                }
+                if (y2 == 110 && redPress) {
+                    //uLCD.filled_circle(45, y2, 8 , WHITE);
+                    uLCD.circle(45,110, 8, RED);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(45, y2, 8 , BLACK);
+                    uLCD.circle(45,110, 8, RED);
+                }
+                if(y3 == 110 && yellowPress) {
+                    uLCD.circle(80,110, 8, YELLOW);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(80, y3, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                }
+                if(y4 == 110 && bluePress) {
+                    uLCD.circle(115,110, 8, BLUE);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(115, y4, 8 , BLACK);
+                    uLCD.circle(115,110, 8, BLUE);
+                }
+            }
+            
+        case 3: //green and yellow then red and blue
+            totalBubbles += 4;
+            greenPress = false;
+            redPress = false;
+            yellowPress = false;
+            bluePress = false;
+            redRelease = false;
+            yellowRelease = false;
+            greenRelease = false;
+            blueRelease = false;
+            if (speed == Medium) {
+                y1 = 5;
+                y2 = 5;
+                y3 = 5;
+                y4 = 5;
+            } else {
+                y1 = 10;
+                y2 = 10;
+                y3 = 10;
+                y4 = 10;
+            }
+            while (y4 <= 130) {
+                if(y1 <= 130) {
+                    y1 += Speed;
+                }
+                if (redRelease) {
+                    y2+= Speed;
+                }
+                if (y3 <= 130) {
+                    y3+= Speed;
+                }
+                if (blueRelease) {
+                    y4 += Speed;
+                }
+                
+                if(y1 <= 130 && !greenPress) {
+                    uLCD.filled_circle(10,y1, 8 , GREEN);
+                }
+                if ((y1 >= 60 && y2 <= 130) && !redPress ) {
+                    uLCD.filled_circle(45,y2, 8 , RED);
+                    redRelease = true;
+                }
+                if (y3 <= 130 && !yellowPress) {
+                    uLCD.filled_circle(80,y3, 8 , YELLOW);
+                    yellowRelease = true;
+                }
+                if((y1 >= 60 && y4 <= 130) && !bluePress) {
+                    uLCD.filled_circle(115,y4, 8 , BLUE);
+                    blueRelease = true;
+                }
+                Thread::wait(250);
+                uLCD.filled_circle(10, y1, 8 , BLACK);
+                uLCD.filled_circle(45, y2, 8 , BLACK);
+                uLCD.filled_circle(80, y3, 8 , BLACK);
+                uLCD.filled_circle(115, y4, 8 , BLACK);
+                
+                if (y1 == 110 && greenPress) {
+                    //uLCD.filled_circle(10, y1, 8 , WHITE);
+                    uLCD.circle(10,110, 8, GREEN);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(10, y1, 8 , BLACK);
+                    uLCD.circle(10,110, 8, GREEN);
+                }
+                if (y2 == 110 && redPress) {
+                    //uLCD.filled_circle(45, y2, 8 , WHITE);
+                    uLCD.circle(45,110, 8, RED);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(45, y2, 8 , BLACK);
+                    uLCD.circle(45,110, 8, RED);
+                }
+                if(y3 == 110 && yellowPress) {
+                    //uLCD.filled_circle(80, y3, 8 , WHITE);
+                    uLCD.circle(80,110, 8, YELLOW);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(80, y3, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                }
+                if(y4 == 110 && bluePress) {
+                    uLCD.circle(115,110, 8, BLUE);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(115, y4, 8 , BLACK);
+                    uLCD.circle(115,110, 8, BLUE);
+                }
+            }
+            
+        case 4:
+            totalBubbles += 4;
+            greenPress = false;
+            redPress = false;
+            yellowPress = false;
+            bluePress = false;
+            redRelease = false;
+            yellowRelease = false;
+            greenRelease = false;
+            blueRelease = false;
+            if (speed == Medium) {
+                y1 = 5;
+                y2 = 5;
+                y3 = 5;
+                y4 = 5;
+                scoreUp = 1000;
+            } else {
+                y1 = 10;
+                y2 = 10;
+                y3 = 10;
+                y4 = 10;
+                scoreUp = 1500;
+            }
+            while (y1 <= 130) {
+                if(y4 <= 130) {
+                    y4 += Speed;
+                }
+                if (yellowRelease) {
+                    y3+= Speed;
+                }
+                if (redRelease) {
+                    y2+= Speed;
+                }
+                if (greenRelease) {
+                    y1 += Speed;
+                }
+                if(y4 <= 130 && !bluePress) {
+                    uLCD.filled_circle(115,y4, 8 , BLUE);
+                }
+                if ((y4 >= 30 && y3 <= 130) && !yellowPress ) {
+                    uLCD.filled_circle(80,y3, 8 , YELLOW);
+                    yellowRelease = true;
+                }
+                if ((y3 >= 30 && y2 <= 130) && !redPress) {
+                    uLCD.filled_circle(45,y2, 8 , RED);
+                    redRelease = true;
+                }
+                if((y2 >= 30 && y1 <= 130) && !greenPress) {
+                    uLCD.filled_circle(10,y1, 8 , GREEN);
+                    greenRelease = true;
+                }
+                Thread::wait(250);
+                uLCD.filled_circle(10, y1, 8 , BLACK);
+                uLCD.filled_circle(45, y2, 8 , BLACK);
+                uLCD.filled_circle(80, y3, 8 , BLACK);
+                uLCD.filled_circle(115, y4, 8 , BLACK);
+                
+                if (y1 == 110 && greenPress) {
+                    uLCD.circle(10,110, 8, GREEN);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(10, y1, 8 , BLACK);
+                    uLCD.circle(10,110, 8, GREEN);
+                }
+                if (y2 == 110 && redPress) {
+                    uLCD.circle(45,110, 8, RED);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(45, y2, 8 , BLACK);
+                    uLCD.circle(45,110, 8, RED);
+                }
+                if(y3 == 110 && yellowPress) {
+                    uLCD.circle(80,110, 8, YELLOW);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(80, y3, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                }
+                if(y4 == 110 && bluePress) {
+                    uLCD.circle(115,110, 8, BLUE);
+                    score += scoreUp;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(115, y4, 8 , BLACK);
+                    uLCD.circle(115,110, 8, BLUE);
+                }
+            }
+            sampletick.detach();
+            i = 0;
+            if (score> highScore) {
+                highScore = score;
+            }
+            miss = totalBubbles - hit;
+            Game_State = Game_Over;
+            break;
+    }
+}
+
+
+void easyMode()
+{
+    int pattern = 1;
+    score = 0;
+    hit = 0;
+    totalBubbles = 0;
+    uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
+    uLCD.background_color(BLACK);
+    Thread::wait(1000);
+        uLCD.circle(10,110, 8, GREEN);
+        uLCD.circle(45,110, 8, RED);
+        uLCD.circle(80,110, 8, YELLOW);
+        uLCD.circle(115,110, 8, BLUE);
+    switch (pattern) {
+        case 1:
+            totalBubbles += 3;
+            greenPress = false;
+            redPress = false;
+            yellowPress = false;
+            redRelease = false;
+            yellowRelease = false;
+            greenRelease = false;
+            y1 = 10;
+            y2 = 10;
+            y3 = 10;
+            while(y3 <= 130) {
+                if(y1 <= 130) {
+                    y1 += Speed;
+                }
+                if (redRelease) {
+                    y2+= Speed;
+                }
+                if (yellowRelease) {
+                    y3+= Speed;
+                }
+                
+                if(y1 <= 130 && !greenPress) {
+                    uLCD.filled_circle(10,y1, 8 , GREEN);
+                }
+                if ((y1 >= 40 && y2 <= 130) && !redPress ) {
+                    uLCD.filled_circle(45,y2, 8 , RED);
+                    redRelease = true;
+                }
+                if ((y2 >= 40 && y3 <= 130) && !yellowPress) {
+                    uLCD.filled_circle(80,y3, 8 , YELLOW);
+                    yellowRelease = true;
+                }
+                Thread::wait(250);
+                uLCD.filled_circle(10, y1, 8 , BLACK);
+                uLCD.filled_circle(45, y2, 8 , BLACK);
+                uLCD.filled_circle(80, y3, 8 , BLACK);
+                
+                if (y1 == 110 && greenPress) {
+                    uLCD.circle(10,110, 8, GREEN);
+                    score += 500;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(10, y1, 8 , BLACK);
+                    uLCD.circle(10,110, 8, GREEN);
+                }
+                if (y2 == 110 && redPress) {
+                    uLCD.circle(45,110, 8, RED);
+                    score += 500;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(45, y2, 8 , BLACK);
+                    uLCD.circle(45,110, 8, RED);
+                }
+                if(y3 == 110 && yellowPress) {
+                    uLCD.circle(80,110, 8, YELLOW);
+                    score += 500;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(80, y3, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                }
+                
+            }
+            pattern = 4;
+            
+        case 4:
+            totalBubbles += 3;
+            greenPress = false;
+            redPress = false;
+            yellowPress = false;
+            redRelease = false;
+            yellowRelease = false;
+            greenRelease = false;
+            y1 = 10;
+            y2 = 10;
+            y3 = 10;
+            while(y1 <= 130) {
+                if(y3 <= 130) {
+                    y3 += Speed;
+                }
+                if (redRelease) {
+                    y2+= Speed;
+                }
+                if (greenRelease) {
+                    y1+= Speed;
+                }
+                if (y3 <= 130 && !yellowPress) {
+                    uLCD.filled_circle(80,y3, 8 , YELLOW);
+                }
+                if ((y3 >= 40 && y2 <= 130) && !redPress ) {
+                    uLCD.filled_circle(45,y2, 8 , RED);
+                    redRelease = true;
+                }
+                if ((y2 >= 40 && y1 <= 130) && !greenPress) {
+                    uLCD.filled_circle(10,y1, 8 , GREEN);
+                    greenRelease = true;
+                }
+                Thread::wait(250);
+                uLCD.filled_circle(10, y1, 8 , BLACK);
+                uLCD.filled_circle(45, y2, 8 , BLACK);
+                uLCD.filled_circle(80, y3, 8 , BLACK);
+                
+                if (y3 == 110 && yellowPress) {
+                    //uLCD.filled_circle(80, y1, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                    score+=500;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(80, y3, 8 , BLACK);
+                    uLCD.circle(80,110, 8, YELLOW);
+                }
+                if (y2 == 110 && redPress) {
+                    uLCD.circle(45,110, 8, RED);
+                    score += 500;
+                    hit++;
+                } else {
+                    uLCD.filled_circle(45, y2, 8 , BLACK);
+                    uLCD.circle(45,110, 8, RED);
+                }
+                if(y1 == 110 && greenPress) {
+                    uLCD.circle(10,110, 8, GREEN);
+                    score += 500;
+                    hit++;
+                } else {
+                    uLCD.circle(10,110,8, GREEN);
+                }
+                
+            }
+            if (score > highScore) {
+                highScore = score;
+            }
+            sampletick.detach();
+            i = 0;
+            miss = totalBubbles - hit;
+            Game_State = Game_Over;
+            break;
+    }
+}
+
 void audio_sample ()
 {
     speaker.write_u16(sound_data[i]);
     i++;
     if (i>= NUM_ELEMENTS) {
         i = 0;
-        sampletick.detach();
-        Game_State = Game_Over;
+        //sampletick.detach();
+        //Game_State = Game_Over;
     }
 }
 
@@ -69,7 +657,12 @@
                if (draw_enable) {
                     lcd_mut.lock();
                         uLCD.cls();
-                        uLCD.printf("Start_Game");
+                        //uLCD.printf("Start_Game");
+                        if(Speed == Easy) {
+                            easyMode();
+                        } else {
+                            otherMode(Speed);
+                        }
                     lcd_mut.unlock();
                 }
                 break;
@@ -94,11 +687,11 @@
 
                         uLCD.color(GREEN);
                         uLCD.locate(2,5);
-                        uLCD.printf("Hits:%d", hits);
+                        uLCD.printf("Hits:%d", hit);
 
                         uLCD.color(RED);
                         uLCD.locate(2,7);
-                        uLCD.printf("Misses:%d", misses);
+                        uLCD.printf("Misses:%d", miss);
                         uLCD.text_height(2);
 
                         uLCD.color(WHITE);
@@ -108,7 +701,7 @@
                         uLCD.text_height(1);
                         uLCD.locate(4,13);
                         uLCD.text_height(2);
-                        uLCD.printf("%d", high_score);
+                        uLCD.printf("%d", highScore);
                         uLCD.text_height(1);
                         
                     lcd_mut.unlock();
@@ -271,6 +864,11 @@
     switch (Game_State) {
             case Start_Game :
                 draw_enable = true;
+                if(y1 == 110) {
+                    if (joy.yAxis() > 0.05 || joy.yAxis() < -0.05) {
+                        greenPress = true; //joy > .55 || joy < .45
+                    }
+                }
                 break;
             case Game_Over :
                draw_enable = true;
@@ -301,6 +899,11 @@
         switch (Game_State) {
             case Start_Game :
                 draw_enable = true;
+                if(y2 == 110) {
+                    if (joy.yAxis() > 0.05 || joy.yAxis() < -0.05) {
+                        redPress = true;
+                    }
+                }
                 break;
             case Game_Over :
                draw_enable = true;
@@ -331,6 +934,11 @@
         switch (Game_State) {
             case Start_Game :
                 draw_enable = true;
+                if(y3 == 110) {
+                    if (joy.yAxis() > 0.05 || joy.yAxis() < -0.05) {
+                        yellowPress = true;
+                    }
+                }
                 break;
             case Game_Over :
                draw_enable = true;
@@ -361,6 +969,11 @@
         switch (Game_State) {
             case Start_Game :
                 draw_enable = true;
+                if(y4 == 110) {
+                    if (joy.yAxis() > 0.05 || joy.yAxis() < -0.05) {
+                        bluePress = true;
+                    }
+                }
                 break;
             case Game_Over :
                draw_enable = true;