Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Revision:
12:21268d8bf979
Parent:
11:418a4437a693
Child:
13:b302c71e300f
--- a/main.cpp	Fri Oct 13 15:48:24 2017 +0000
+++ b/main.cpp	Mon Oct 16 15:45:17 2017 +0000
@@ -2,6 +2,7 @@
 #include "rtos.h"
 #include "wave_player.h"
 #include "SDFileSystem.h"
+#include "button.h"
 
 PwmOut myservo(p21);
 DigitalIn pb1 (p20);
@@ -11,34 +12,15 @@
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
 AnalogOut DACout(p26);
 wave_player waver(&DACout);
+button button1(myservo, pb1);
 
 // add start, mode, reset buttons
 int start = 0;
 int submit = 0;
+// FIX THIS: button up: state = 2, button halfway: state = 0; button down: state = 1
+int state = 2;
 
 // FUNCTIONS
-
-void moveServoIn(PwmOut servo, DigitalIn pb)
-{
-    while(pb == 1)
-        //myled = 1;
-        // rotate 90 degrees one way
-        for(int i=3; i<=7; i++) {
-            servo = i/100.0;
-            wait(0.01);
-        }
-}
-
-void moveServoOut(PwmOut servo, DigitalIn pb)
-{
-    while(pb == 1)
-        //myled = 0;
-        for(int i=7; i>3; i--) {
-            servo = i/100.0;
-            wait(0.01);
-        }
-}
-
 void playSound(wave_player waver)
 {
     FILE *wave_file;
@@ -47,13 +29,19 @@
     fclose(wave_file);
 }
 
-
 // THREADS
-
-
 void button_thread()
 {
-
+    // button was up and is moving down, move servo in
+    if (pb1 == 1 && state == 2) {
+        button1.moveServoIn();
+        state = 1;
+    }
+    // button was down and is being pushed again, move servo out
+    if (pb1 == 1 && state == 1) {
+        button1.moveServoOut();
+        state = 2;
+    }
 }
 
 void submit_thread()
@@ -72,12 +60,11 @@
 int main()
 {
     // SETUP
-    // button up: state = 0, button halfway: state = 1; button down: state = 2
-    int state = 2;
     // pull up the pushbutton to prevent bouncing
     pb1.mode(PullUp);
     wait(.001);
     // servo begins at 30 degrees
+    // replace with a button setup function
     for(int i=0; i<=3; i++) {
         myservo = i/100.0;
         wait(0.01);
@@ -88,38 +75,23 @@
 
         // start threads for reset, mode, start
         Thread t1(start_thread);
-
         // setup SDcard and Speaker
-
-
+        
         // when started
         while (start == 0) {}
         Thread t2(button_thread);
-        Thread t3(submit_thread);
+        
+        //Thread t3(submit_thread);
 
         // when submitted
-        while (submit == 0) {}
-                
+        //while (submit == 0) {}
+
 
         // start button threads and submit thread
         // if submit close button threads and submit thread
         // check result, speaker result
         // save results
 
-
-
-
-        // Servo code
-        if (pb1 == 1 && state == 2) {
-            moveServoIn(myservo, pb1);
-            state = 1;
-        }
-        // rotate 90 degrees other way
-        if (pb1 == 1 && state == 1) {
-            moveServoOut(myservo, pb1);
-            state = 2;
-        }
-
         // SD card code
 
         // speaker code