Chris Ernst / Mbed 2 deprecated detector_ADC_Read

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
cernst2787
Date:
Tue Aug 13 23:35:19 2019 +0000
Parent:
0:cf5c0e861996
Commit message:
Trying to create Repos of my projects

Changed in this revision

detector.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/detector.cpp	Sun Sep 16 21:59:49 2018 +0000
+++ b/detector.cpp	Tue Aug 13 23:35:19 2019 +0000
@@ -1,34 +1,76 @@
 #include "mbed.h"
+FILE *myLogFile;
+Ticker sampleTicker;
+AnalogIn sensor(PA_0);
+Timer fileOpenTimer;
  
-AnalogIn analog_value(A0);
-//AnalogIn analog_value2(A1);
- 
+#define bufferSize 4096
+//float sensorReading[bufferSize];
+//unsigned int readPointer = 0;
+//volatile unsigned int writePointer = 0; // volatile so that the main loop knows to check for changes.
+
+float ADCSamples[bufferSize];
+char SampleCount;
 
-int main() 
+void onSampleTick(void)
 {
-    float meas;
-            
-    while(1) 
+    sensorReading[writePointer++] = sensor; // Unscaled value from 0.0 to 1.0
+    if (writePointer == bufferSize)
+    {
+        writePointer = 0;
+    }
+    if (writePointer == readPointer) 
+    {
+        // BUFFER OVERFLOW. You may want to print an error message or turn an LED on
+    }
+}
+ 
+main()
+{
+    
+    fileOpenTimer.start();
+    sampleTicker.attach(&onSampleTick,0.02); // sets the sample period in seconds. Check to see what this time should be?
+ 
+    while (true)
     {
-        meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
-        //float voltage = meas * 3300.0; // Change the value to be in the 0 to 3300 range
-        //printf("measure = %.0f mV\n", voltage);
-        //led = meas;
-        
-        wait(0.1); // 100 ms
+ 
+        while (writePointer != readPointer) 
+        { // write any waiting data to the SD card
+            printf(/*myLogFile,*/"%f\n",sensorReading[readPointer++]);
+        }
+            if (readPointer == bufferSize)
+            {
+                readPointer = 0;
+            }
+ 
+        if (fileOpenTimer > (5*60)) 
+        { // file has been open 5 minutes
+            fclose(myLogFile); // close the current file
+            //myLogFile = nextLogFile(); // open a new file
+            if (!myLogFile) 
+            {
+                break; // exit the while(true) loop
+                // ERROR failed to open the log file for writing.
+                // card full maybe?
+            }
+            fileOpenTimer.reset() // restart the timer
+        }
     }
-    float n[4096]; //n is an array, size of the ADC
-    float j;
-    
-    for (meas = 0; meas < 4096; meas++)
-    {
-        n[meas] = meas * 3300.0; //set element to Voltage value
-    }
-    
-    for (j = 0; j < 4096; j++)
-    {
-        printf("Element[%d] = %d\n", j, n[j] );
-        }
-        
-    return 0;
 }
+
+// opens the next unused file name in the format set.
+// This can be a little slow the first time if there are already lots of log files
+// since it tries each number in turn but the second call on will be fairly quick.
+/*FILE *nextLogFile(void)  //this part creates file write to SD card. MODIFY!!!! to create serial printline *11/1/18
+{
+    static unsigned int fileNumber = 0;
+    char fileName[32];
+    FILE *filePtr = NULL;
+    do {
+        if (filePtr != NULL)
+            fclose(filePtr);
+        sprintf(fileName,"/sd/log%04u.csv",fileNbr++);
+        filePtr = fopen(fileName,"r");
+    } while (filePtr != NULL);
+    return fopen( fileName,"w");
+}*/
\ No newline at end of file