Hi,
I need to measure signal with sampling frequency about 1-2 kHz while writing data on SD card (SPI) simulatenously. Unfortunately I noticed that with the Arduino the maximum sampling frequency is about only about 70 samples/s - not 9.6kHz as it supposed to be. I think that the problem is SD.h library which seems to slow down the whole process significantly and ADC just can't handle this. Does anybody has any clue to solve this and increase the sampling rate? I know that it's possible to change the prescaler but with such a big difference in sampling with and without SD it seems to has naothing to do with it...
#include <LiquidCrystal.h>
#include <SD.h>
int SS_Pin = 8;
boolean SD_check;
volatile int PressButtonCounter = 1;
long int freq;
long int b;
int i;
int j;
long int tab[100];
File data_file;
LiquidCrystal lcd(7, 6, 5, 4, 3, 10);
void generator_setup()
{
DDRB |= 0x06;
TCCR1A = 0x40;
TCCR1B = 0x09;
}
void function()
{
PressButtonCounter++;
}
void setup()
{
pinMode(SS_Pin, OUTPUT);
digitalWrite(SS_Pin, LOW);
lcd.begin(8, 2);
Serial.begin(9600);
SD_check = SD.begin(SS_Pin);
attachInterrupt(0, function, FALLING);
generator_setup();
lcd.clear();
lcd.print("Init.");
delay(1000);
Serial.println("OK!");
if(SD_check)
{
Serial.println("SD initialization is done");
lcd.clear();
lcd.print("OK!");
delay(1000);
lcd.clear();
}
}
void loop()
{
if(PressButtonCounter%2 == 0)
{
lcd.print("Writing"); //code which
data_file = SD.open("TextFile.txt", FILE_WRITE); //cunduct the
data_file.println(analogRead(0)); // the aquisition
Serial.println("File created. Data streaming works correctly"); // form analog0
data_file.close(); // to SD card
//delay(10);
lcd.clear();
}
else if(PressButtonCounter%2 != 0)
{
int wynik=0;
float srednia=0;
long int suma = 0;
freq = analogRead(1);
freq= map(freq, 0, 1018, 2000, 50000);
OCR1A = (16000000/freq - 2)/2;
lcd.print(freq/100);
lcd.setCursor(6,0);
lcd.print("Hz");
for(i=0; i<99; i++)
{
b=analogRead(1);
tab[i] = b;
}
for(j=0; j<99; j++)
{
suma = suma + tab[j];
srednia = suma/100;
wynik = srednia/995 * 100;
}
lcd.setCursor(0,1);
lcd.print("wy=");
lcd.setCursor(3,1);
lcd.print(wynik);
lcd.setCursor(6,1);
lcd.print("%");
delay(500);
lcd.clear();
}
}