W R / Mbed 2 deprecated 4180_Final_Project

Dependencies:   mbed wave_player mbed-rtos 4180Final SDFileSystem

Files at this revision

API Documentation at this revision

Comitter:
William12
Date:
Mon Apr 27 20:35:13 2020 +0000
Parent:
4:8331c1239f6a
Child:
6:65216abe57c3
Child:
9:c40418c26c04
Commit message:
New mic_thread and getScore1() changes mbed led3 with micScore

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
microphone.h Show annotated file Show diff for this revision Revisions of this file
play.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 27 19:30:53 2020 +0000
+++ b/main.cpp	Mon Apr 27 20:35:13 2020 +0000
@@ -15,14 +15,14 @@
 Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
 uLCD_4DGL uLCD(p28, p27, p30); // serial tx, serial rx, reset pin; 
 PwmOut myled(LED1);
-//SDFileSystem sd(p11, p12, p13, p10, "sd");
+SDFileSystem sd(p11, p12, p13, p10, "sd");
 microphone mymic1(p15);
 AnalogIn pot(p16);
 AnalogOut DACout(p18);
 DigitalOut led(LED2);
-//BusOut led_strip(p21, p22, p23, p24, p25, p26);
+PwmOut myled3(LED3);
 
-Thread thread1;
+Thread thread1, thread2;
 
 // Stuff for sound
 unsigned char *music = NULL;
@@ -44,6 +44,7 @@
 int highScoresArr[10] = {100, 90, 83, 73, 64, 54, 44, 34, 24, 14};
 int s = 0;
 int timer = 0;
+volatile int micScore = 0;
 
 int main();
 void play();
@@ -70,12 +71,20 @@
     while (1) {
         if (timer == 99) {
             s = (int) (pot * 2.0);
-            myled = pot;
         }
 //        Thread::wait(100);
     }
 }
 
+// thread 2
+void mic_thread() {
+    while (1) {
+        myled = pot;
+        micScore = int(mymic1*1000.0);
+        Thread::wait(100);
+    }
+}
+
 void clearVals() {
     score1 = 0;
     score2 = 0;
@@ -338,7 +347,8 @@
 }
 
 int getScore1() {
-    return int(mymic1*1000.0);
+    myled3 = (int)(((float) micScore) / 1000.0);
+    return micScore; //int(mymic1*1000.0);
     
     
     // gameType==0 for mic
@@ -349,26 +359,7 @@
 //    }
 }
 int getScore2() {
-    return mymic1*1000.0;
-}
-
-void update_leds(float val){
-    if(val <= 2.5){
-        led_strip = 0b100000; 
-    } else if (2.5 > val && val < 5){
-        led_strip = 0b110000; 
-    } else if (val > 5 && val < 7.5){
-        led_strip = 0b111000; 
-    } else if (val > 7.5 && val < 10){
-        led_strip = 0b111100; 
-    } else if (val > 10 && val < 12.5){
-        led_strip = 0b111110; 
-    } else if (val > 12.5 && val < 15){
-        led_strip = 0b111111; 
-    } else {
-        led_strip = 0; 
-    }
-    Thread::wait(0.2); 
+    return 10; //int(mymic1*1000.0);
 }
 
 void play() {
@@ -410,7 +401,6 @@
             if (timer % 25 == 0) {
                 score1 = getScore1(); //(int) (pot*100.0); //curTime+1000; // int(mymic1*1000.0);
                 score2 = getScore2(); //curTime+2000;
-                update_leds(getScore2());
                 uLCD.color(BLUE);
                 uLCD.locate(0,1);
                 uLCD.printf("%4d", score1);
@@ -572,14 +562,14 @@
 
 int main() {
      // read in highScoresArr from SD card
-     /*FILE *fp = fopen("/sd/finalProj/highscores.txt", "r");
+     FILE *fp = fopen("/sd/finalProj/highscores.txt", "r");
      if(fp == NULL) {
      uLCD.printf("Error Open \n");
      }
      for (int i=0; i<sizeof(highScoresArr)/4; i++) {
         fscanf(fp, "%4d", &highScoresArr[i]);
      }
-     fclose(fp);*/
+     fclose(fp);
      // create menu interface
      uLCD.background_color(LBLUE);
      uLCD.cls();
@@ -613,6 +603,7 @@
          uLCD.filled_circle(xPos[i], yPos[i], 2, RED);
      }
      thread1.start(pot_thread);
+     thread2.start(mic_thread);
      nextsample.attach(&playMenuMusic, 1.0/8000.0);
      while (1) {
          // play music
--- a/microphone.h	Mon Apr 27 19:30:53 2020 +0000
+++ b/microphone.h	Mon Apr 27 20:35:13 2020 +0000
@@ -30,13 +30,13 @@
     return _pin.read();
 }
 
-microphone mymicrophone(p16);
+extern microphone mymic1;
 
-int get_sound()
+void get_sound()
 {
-    //while(1) {
+    while(1) {
 //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display
-        int val = int(abs((mymicrophone - (0.67/3.3)))*500.0);
+        int val = int(abs((mymic1 - (0.67/3.3)))*500.0);
         if(val <= 2.5){
             led_strip = 0b100000; 
         } else if (2.5 > val && val < 5){
@@ -54,7 +54,7 @@
         }
         wait(0.2); 
 //Use an 8kHz audio sample rate (phone quality audio);
-        //wait(1.0/8000.0);
-  //  }
-    return val; 
+        wait(1.0/8000.0);
+    }
+    //return val; 
 }
--- a/play.cpp	Mon Apr 27 19:30:53 2020 +0000
+++ b/play.cpp	Mon Apr 27 20:35:13 2020 +0000
@@ -1,4 +1,4 @@
-#include "mbed.h"
+/*#include "mbed.h"
 #include "uLCD_4DGL.h"
 #include "Nav_Switch.h"
 //#include "SDFileSystem.h"
@@ -97,7 +97,7 @@
                 nextRound();
             }
             curTime++;
-            */
+            
         }
         timer++;
     }
@@ -107,3 +107,4 @@
 {
     play();
 }
+*/