Chopsticks
Active member
Hello!
I've been busy with measuring analog values. It's possible to measure the values, but somehow i don't know how to increase the sample rate, it's maybe a noob question, but i'm using a combination of the following libraries
- SDfat (for logging to SDcard)
- RTC
- Teensy Synchronous measurement
When i remove the 'WriteToSD' part, the samples are higher then when i use 'WriteToSD'. My first thought it has something to do with continuously opening and closing the file over and over again...
Or has it something to do with the averaging part which is used in the 'Teensy's synchronous measurement' example?
Does anyone had the same problem? Or know how to fix this to get higher sample rates. At the moment I have 50 samples/second.
My logfile:
View attachment ADC_syncMeasSDlogging.txt
My sourcecode:
I've been busy with measuring analog values. It's possible to measure the values, but somehow i don't know how to increase the sample rate, it's maybe a noob question, but i'm using a combination of the following libraries
- SDfat (for logging to SDcard)
- RTC
- Teensy Synchronous measurement
When i remove the 'WriteToSD' part, the samples are higher then when i use 'WriteToSD'. My first thought it has something to do with continuously opening and closing the file over and over again...
Or has it something to do with the averaging part which is used in the 'Teensy's synchronous measurement' example?
Does anyone had the same problem? Or know how to fix this to get higher sample rates. At the moment I have 50 samples/second.
My logfile:
View attachment ADC_syncMeasSDlogging.txt
My sourcecode:
Code:
/*
- Sensing two ADC values,
- Analog files being logged on sd-card
Time: A-B: A: B:
*/
//Including Libraries
#include <ADC.h>
#include <string.h>
#include <Time.h>
#include <SdFat.h>
SdFat sd;
SdFile myFile;
//Define ADC Inputs
#define ADC_0 0
#define ADC_1 1
//Teensy 3.1 has the LED on pin 13
const int ledPin = 13;
const int readPin0 = A2; //ADC read A2
const int readPin1 = A3; //ADC read A3
// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = SS;
//const int chipSelect = 10; //CS(SS) select SD-Card
char filename[] = "00000000.txt";
ADC *adc = new ADC(); // adc object
ADC::Sync_result result;
int ADCvalue_0 = ADC_ERROR_VALUE;
int ADCvalue_1 = ADC_ERROR_VALUE;
void setup() {
setSyncProvider(getTeensy3Time);
pinMode(ledPin, OUTPUT);
pinMode(readPin0, INPUT); //Read analog signal pin1
pinMode(readPin1, INPUT); //Read analog signal pin2
Serial.begin(9600);
SetTime();
//Set ADC (Averaging and Resolution)
adc->setAveraging(32, ADC_0); // set number of averages
adc->setResolution(8), ADC_0; // set bits of resolution
adc->setAveraging(32, ADC_1); // set number of averages
adc->setResolution(8, ADC_1); // set bits of resolution
// always call the compare functions after changing the resolution!
//adc->enableCompare(1.0/3.3*adc->getMaxValue(), 0, ADC_0); // measurement will be ready if value < 1.0V
//adc->enableCompareRange(1.0*adc->getMaxValue(ADC_1)/3.3, 2.0*adc->getMaxValue(ADC_1)/3.3, 0, 1, ADC_1); // ready if value lies out of [1.0,2.0] V
while (!Serial) {} // Wait for serial
Serial.println("Type any character to start");
while (Serial.read() <= 0) {}
delay(400);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
getFileName();
createFileName();
}
void loop() {
InitializeClock();
WriteToSerial();
WriteToSD();
}
//Test sequence read from file
void readFile(){
// re-open the file for reading:
if (!myFile.open(filename, O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
int data;
while ((data = myFile.read()) >= 0) Serial.write(data);
// close the file:
myFile.close();
}
//Test sequence write to file
void writeFile(){
// open the file for write at end like the Native SD library
if (!myFile.open(filename, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening filename for write failed");
}
// if the file opened okay, write to it:
Serial.print("Writing to new filename");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
}
void getFileName(){
filename[0] = (month()/10)%10 + '0';
filename[1] = month()%10 + '0';
filename[2] = (day()/10)%10 + '0';
filename[3] = day()%10 + '0';
filename[4] = (hour()/10)%10 + '0';
filename[5] = hour()%10 + '0';
filename[6] = (minute()/10)%10 + '0';
filename[7] = minute()%10 + '0';
/*
filename[0] = (year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = year()%10 + '0'; //To get 4th digit from year()
filename[4] = (month()/10)%10 + '0'; //To get 1st digit from month()
filename[5] = month()%10 + '0'; //To get 2nd digit from month()
filename[6] = (day()/10)%10 + '0'; //To get 1st digit from day()
filename[7] = day()%10 + '0'; //To get 2nd digit from day()
*/
filename[8] = '.';
filename[9] = 't';
filename[10] = 'x';
filename[11] = 't';
Serial.println(filename);
}
void createFileName(){
//Check file name exist?
if (sd.exists(filename)) {
Serial.println("exists.");
}
else {
Serial.println("Filename doesn't exist.");
Serial.println("Creating a new file");
Serial.println(filename);
if (!myFile.open(filename, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening filename for write failed");
}
myFile.print("Date: ");
myFile.print(day());
myFile.print("-");
myFile.print(month());
myFile.print("-");
myFile.print(year());
myFile.print("\t");
myFile.print(hour());
printDigitsSD(minute());
printDigitsSD(second());
myFile.println("");
myFile.print("Time:");
myFile.print("\t");
myFile.print("A-B:");
myFile.print("\t");
myFile.print("A:");
myFile.print("\t");
myFile.println("B:");
myFile.close();
}
}
//Digital Clock for Serial
void digitalClockDisplay_WriteToSerial() {
// digital clock display of the time
Serial.print(day());
Serial.print("-");
Serial.print(month());
Serial.print("-");
Serial.print(year());
Serial.print("\t");
Serial.print(hour());
printDigitsSerial(minute());
printDigitsSerial(second());
Serial.print(",");
Serial.print(millis());
//Serial.println("\t");
}
//Digital Clock for SD Card
void digitalClockDisplay_WriteToSD() {
// digital clock display of the time
myFile.print(hour());
printDigitsSD(minute());
printDigitsSD(second());
myFile.print(",");
myFile.print(millis());
//Serial.println("\t");
}
//Print digits for Serial
void printDigitsSerial(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
//Print digits for SD
void printDigitsSD(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
myFile.print(":");
if(digits < 10)
myFile.print('0');
myFile.print(digits);
}
//Setting Time
void SetTime(){
//Set Time
if (timeStatus()!= timeSet) {
Serial.println("Unable to sync with the RTC");
} else {
Serial.println("RTC has set the system time");
}
}
//Initializing RTC
void InitializeClock() {
if (Serial.available()) {
time_t t = processSyncMessage();
if (t != 0) {
Teensy3Clock.set(t); // set the RTC
setTime(t);
}
}
}
//Set RTC Time
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
// END Set RTC Time
//Write ADC value to serial monitor
void WriteToSerial() {
if( (result.result_adc0 !=ADC_ERROR_VALUE) && (result.result_adc1 !=ADC_ERROR_VALUE) ) {
//ADC values
result = adc->analogSynchronizedRead(readPin0, readPin1);
float ADCvalue_0 = result.result_adc0*3.3/adc->adc0->getMaxValue();
float ADCvalue_1 = result.result_adc1*3.3/adc->adc1->getMaxValue();
Serial.print("Time: ");
digitalClockDisplay_WriteToSerial();
Serial.print("\tDiff(ADC0-ADC1): ");
Serial.print(ADCvalue_0-ADCvalue_1, DEC);
Serial.print("\t");
Serial.print("A2(ADC0): ");
Serial.print(ADCvalue_0, DEC);
Serial.print("\t");
Serial.print("A3(ADC1): ");
Serial.println(ADCvalue_1, DEC);
} else {
Serial.println("Error 2: Comparison failed");
}
}
//Write ADC value to sdcard
void WriteToSD() {
if (!myFile.open(filename, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening filename for write failed");
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
if( (result.result_adc0 !=ADC_ERROR_VALUE) && (result.result_adc1 !=ADC_ERROR_VALUE) ) {
//ADC values
result = adc->analogSynchronizedRead(readPin0, readPin1);
float ADCvalue_0 = result.result_adc0*3.3/adc->adc0->getMaxValue();
float ADCvalue_1 = result.result_adc1*3.3/adc->adc1->getMaxValue();
digitalClockDisplay_WriteToSD();
myFile.print("\t");
myFile.print(ADCvalue_0-ADCvalue_1, DEC);
myFile.print("\t");
myFile.print(ADCvalue_0, DEC);
myFile.print("\t");
myFile.println(ADCvalue_1, DEC);
//close the file:
myFile.close();
//Serial.println("done.");*
} else {
// if the file didn't open, print an error:
Serial.println("Error 1: Can't open file!");
}
}
Code:
Last edited: