Another ADC detector program

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers detector.cpp Source File

detector.cpp

00001 #include "mbed.h"
00002 FILE *myLogFile;
00003 Ticker sampleTicker;
00004 AnalogIn sensor(PA_0);
00005 Timer fileOpenTimer;
00006  
00007 #define bufferSize 4096
00008 //float sensorReading[bufferSize];
00009 //unsigned int readPointer = 0;
00010 //volatile unsigned int writePointer = 0; // volatile so that the main loop knows to check for changes.
00011 
00012 float ADCSamples[bufferSize];
00013 char SampleCount;
00014 
00015 void onSampleTick(void)
00016 {
00017     sensorReading[writePointer++] = sensor; // Unscaled value from 0.0 to 1.0
00018     if (writePointer == bufferSize)
00019     {
00020         writePointer = 0;
00021     }
00022     if (writePointer == readPointer) 
00023     {
00024         // BUFFER OVERFLOW. You may want to print an error message or turn an LED on
00025     }
00026 }
00027  
00028 main()
00029 {
00030     
00031     fileOpenTimer.start();
00032     sampleTicker.attach(&onSampleTick,0.02); // sets the sample period in seconds. Check to see what this time should be?
00033  
00034     while (true)
00035     {
00036  
00037         while (writePointer != readPointer) 
00038         { // write any waiting data to the SD card
00039             printf(/*myLogFile,*/"%f\n",sensorReading[readPointer++]);
00040         }
00041             if (readPointer == bufferSize)
00042             {
00043                 readPointer = 0;
00044             }
00045  
00046         if (fileOpenTimer > (5*60)) 
00047         { // file has been open 5 minutes
00048             fclose(myLogFile); // close the current file
00049             //myLogFile = nextLogFile(); // open a new file
00050             if (!myLogFile) 
00051             {
00052                 break; // exit the while(true) loop
00053                 // ERROR failed to open the log file for writing.
00054                 // card full maybe?
00055             }
00056             fileOpenTimer.reset() // restart the timer
00057         }
00058     }
00059 }
00060 
00061 // opens the next unused file name in the format set.
00062 // This can be a little slow the first time if there are already lots of log files
00063 // since it tries each number in turn but the second call on will be fairly quick.
00064 /*FILE *nextLogFile(void)  //this part creates file write to SD card. MODIFY!!!! to create serial printline *11/1/18
00065 {
00066     static unsigned int fileNumber = 0;
00067     char fileName[32];
00068     FILE *filePtr = NULL;
00069     do {
00070         if (filePtr != NULL)
00071             fclose(filePtr);
00072         sprintf(fileName,"/sd/log%04u.csv",fileNbr++);
00073         filePtr = fopen(fileName,"r");
00074     } while (filePtr != NULL);
00075     return fopen( fileName,"w");
00076 }*/