Evan Zhang / Mbed 2 deprecated StepperMotorSong4180

Dependencies:   mbed mbed-rtos SDFileSystem11

Revision:
6:2450dc369670
Parent:
5:bc7213111da0
Child:
7:dd65856c3982
--- a/main.cpp	Mon Apr 26 21:31:02 2021 +0000
+++ b/main.cpp	Mon Apr 26 22:05:32 2021 +0000
@@ -28,9 +28,9 @@
 DigitalOut f1(p21);
 DigitalOut f2(p22);
 DigitalOut f3(p23);
-DigitalOut m1e(p5);
-DigitalOut m2e(p6);
-DigitalOut m3e(p7);
+DigitalOut m1e(p17);
+DigitalOut m2e(p18);
+DigitalOut m3e(p19);
 Ticker t1;
 Ticker t2;
 Ticker t3;
@@ -58,6 +58,16 @@
 }
 
 int main() {
+    f1 = 0;
+    f2 = 0;
+    f3 = 0;
+    m1e = 0;
+    m2e = 0;
+    m3e = 0;
+    motor1stop = 0;
+    motor2stop = 0;
+    motor3stop = 0;
+
      bool holdNotes = 0; //Stop reading notes file
      bool holdStart = 0; //Stop reading start times
      bool holdFinish = 0; //Stop reading in finish times
@@ -69,7 +79,7 @@
     float num_float = std::atof(d);
 
     // convert string to double
-    pc.printf("%f\n", num_float-1.0);
+    //pc.printf("%f\n", num_float-1.0);
     int temp = 0;
 
     pc.printf("Hello World!");
@@ -79,29 +89,31 @@
     FILE *fpstart = fopen("/sd/notesstart.txt", "r");
     FILE *fpfinish = fopen("/sd/notesfinish.txt","r");
     unsigned char c;
-    pc.printf("I'm here\n");
+    //pc.printf("I'm here\n");
     Note myNote = Note(0, 0, 0);
+     motor1timer.start();
     while (!feof(fpn)){                        // while not end of file
         while(!holdNotes) {
            c = fgetc(fpn);                         // get a character/byte from the file
            if(c == ','){
                stringstream degree(output);
                degree >> temp;
-               pc.printf("%d\n", temp);
+               //pc.printf("%d\n", temp);
+                myNote.midiNote = temp;
                output = "";
                holdNotes = 1;
            } else {
           output += c;
           }
         }
-    myNote.midiNote = temp;
+
     output = "";
         while(!holdStart) {
             c = fgetc(fpstart);
             if(c == ','){
                 d = output.c_str();
                 num_float = std::atof(d);
-                pc.printf("%f\n", num_float);
+                //pc.printf("%f\n", num_float);
                 output = "";  
                 myNote.start = num_float;
                 holdStart = 1;
@@ -116,7 +128,7 @@
         if(c == ','){
             d = output.c_str();
             num_float = std::atof(d);
-            pc.printf("%f\n", num_float);
+            //pc.printf("%f\n", num_float);
             output = "";  
             myNote.stop = num_float;
             holdFinish = 1;
@@ -125,17 +137,54 @@
            }
            
     } //ONCE WE REACH THIS POINT ALL PARTS OF THE NOTE OBJECT SHOULD BE SET
-    pc.printf("Hello World!\n");
-    pc.printf("%d", myNote.midiNote);
-    // spin in a main loop. flipper will interrupt it to call flip
-    while(1) {
-        led1 = !led1;
-        wait(0.2);
-      
+    if(motor1stop< motor1timer.read()) {
+        t1.detach();
+        m1e = 0;
+    }
+    if(motor2stop< motor1timer.read()){
+        t2.detach();
+        m2e = 0;
+    }
+    if(motor3stop< motor1timer.read()) {
+        t3.detach();
+        m3e = 0;
     }
     
-}    
-      
+    if(myNote.start <= motor1timer.read()){
+            if(motor1stop < motor1timer.read()) { //Check if we should add it
+                t1.attach(&flip1, .5/midi2freq(myNote.midiNote)); //
+                motor1stop = myNote.stop+motor1timer.read();
+                m1e = 1;
+                holdNotes = 0; 
+                holdStart = 0;
+                holdFinish = 0;
+            } else if(motor2stop < motor1timer.read()) {
+                t2.attach(&flip2, .5/midi2freq(myNote.midiNote));
+                motor2stop = myNote.stop+motor1timer.read();
+                m2e = 1;
+                holdNotes = 0; 
+                holdStart = 0;
+                holdFinish = 0;
+            } else if(motor3stop < motor1timer.read()) {
+                t3.attach(&flip3, .5/midi2freq(myNote.midiNote));
+                motor3stop = myNote.stop+motor1timer.read();
+                m3e = 1;
+                holdNotes = 0; 
+                holdStart = 0;
+                holdFinish = 0;
+            } else {
+                holdNotes = 0; 
+                holdStart = 0;
+                holdFinish = 0;
+                }
+    } //We have now finished assigning the note to be played
+    led1 = !led1;      
+    
+    
+    } //End of while loop for playing    
+    motor1stop = 0;
+    motor2stop = 0;
+    motor3stop = 0;
       
    
 }
\ No newline at end of file