Claudia Haarman / Mbed 2 deprecated Opstellingbachelor_opdracht

Dependencies:   MODSERIAL SDFileSystemSeth mbed

Fork of Opstellingbachelor_opdracht by Seth Ruiter

Revision:
1:8b45b3ad350a
Parent:
0:f7cd934498e4
Child:
2:5c856ceaea2e
--- a/main.cpp	Sun Sep 04 18:20:08 2016 +0000
+++ b/main.cpp	Sun Sep 04 18:45:07 2016 +0000
@@ -7,17 +7,35 @@
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
+// Thermocouple pin declarations
+DigitalOut SCK_1(2);
+DigitalOut CS_1(3);
+
+int pins[12] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16};
+
+for (int i = 0; i <= 11; i++)
+{
+    DigitalOut(pins[i], INPUT);
+}
+
+double d;
+int b[12] = { };
+
+// Torque sensor
 AnalogIn fcs(PTB2);     // force sensor output connected to analog 0
 
+
+// LED
 DigitalOut r_led(LED_RED);
 DigitalOut g_led(LED_GREEN);
 DigitalOut b_led(LED_BLUE);
 
-
+// Serial connection and SD
 MODSERIAL pc(USBTX,USBRX, 1024, 512);
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
 FILE *fp;
 
+// Other declarations
 Ticker tick;
 
 const int baudrate = 115200;      // baud rate
@@ -31,11 +49,13 @@
 
 float fcs_read;
 
-
 int trial_numb = 0;
 int cal_numb = 1;
 
-char filename[64];
+char filename_torque[64];
+char filename_temp[64];
+char filename_cal[64];
+
 
 
 
@@ -56,6 +76,44 @@
     fn1_go = 1;      // go-flag active
 }
 
+void readTC()
+{
+    CS_1 = 0;
+    wait_ms(70);
+
+    for (int i = 15; i >= 0; i--) {
+        SCK_1 = 0;
+        SCK_1 = 1;
+        for (int j = 0; j <= 11; j++) {
+            if (digitalRead(pins[j])) {
+                b[j] |= (1 << i);
+            }
+        }
+    }
+    
+    CS_1 = 1;
+
+    // capture torque sensor data
+    fcs_read = fcs.read();
+    
+    // output data to file
+    fprintf(fp, "%.3f\t", fcs_read);
+    
+    
+    for (int j = 0; j <= 11; j++) {
+        if (b[j] & 0x1) {
+            b[j] = 20000;
+        } else {
+            b[j] &= 0xfffc;
+            d = b[j] / 16.0 * 100.0;
+            b[j] = d;
+        }
+        fprintf(fp, "%.3f\t", b[j]);
+    }
+    fprintf(fp, "\n\r");
+
+
+}
 
 ////////////////////Main////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
@@ -76,9 +134,9 @@
         if(pc.readable()) {                 // if character available
             switch(pc.getc()) {             // read a character from keyboard
                 case 'c':
-                    led_control(led_on,led_on,led_off);                     //orange LED during calibration
-                    sprintf(filename, "/sd/cal_torque_%d.txt", cal_numb);   //construct new textfile
-                    fp = fopen(filename, "w");
+                    led_control(led_on,led_on,led_off);                         //orange LED during calibration
+                    sprintf(filename_cal, "/sd/cal_torque_%d.txt", cal_numb);   //construct new textfile
+                    fp = fopen(filename_cal, "w");
                     if(fp == NULL) {
                         error("Could not open file for write\n");
                     } else {
@@ -92,7 +150,7 @@
                     }
                     fclose(fp);                                             //close file
                     cal_numb++;                                             //increment calibration number
-                    pc.printf("File saved as %s\n\r", filename);            //print name of calibration file to screen
+                    pc.printf("File saved as %s\n\r", filename_cal);        //print name of calibration file to screen
                     pc.printf("Press 'c' to record new calibration or 't' to start new trial\n\r");    // print message to screen
                     break;
                 case 't':
@@ -112,8 +170,7 @@
                             pc.printf("Measurement started... \n\r");
                             pc.printf("Press 's' to stop measurement\n\r");
                             while(1) {
-                                fcs_read = fcs.read();
-                                fprintf(fp, "%.3f\t \n\r", fcs_read);
+                                readTC(b);
                             }
                             break;
                         }
@@ -123,7 +180,7 @@
                         led_control(led_on,led_off,led_off);                                  // RED LED when ready to stopped measuring
                         fclose(fp);
                         pc.printf("File saved as %s\n\r", filename_torque);                                // print filename to screen
-                        pc.printf("Press 'c' to perform new calibration or 't' to start new trial\n\r");    // print message to screen                   
+                        pc.printf("Press 'c' to perform new calibration or 't' to start new trial\n\r");    // print message to screen
 
                     }
                     break;