I am building an Arduino based frequency generator with an AD9850 module. It ought to generate a set of a frequencies in the time intervals (3 minutes).
I combined a code using tutorials and an examples from an Arduino library because I am not a programmer.
A code for store a frequency set for execution is as follows:
case 11: // funkcja obslugi opcji detox
{
unsigned long freq[] = {
33408, 41984, 45568, 49152, 40000, 50816, 48640, 56320, 50368, 48064,
46528, 43264, 40640, 33408, 59520, 56832, 38912, 39168, 37888, 37376,
31129, 30822, 42434, 40960, 32071, 51609};
wykonaj("Detox", 26, freq);
}
break;
It consists of a frequency table and an execution of a function "wykonaj" which takes a frequency one by one, sends a comand to an AD9850 and display informations for an user. Number "26" it is a number of a frequencies inside this table.
Because a Nano (Pro Mini) modules have only a 2kB RAM I can store only about 200 frequencies for use.
It is too small number of a sets for use. So I decided to use a microSD card module to store frequency tables as a files on a SD card. So this generated a problem for me
about how to take a numbers from a file and write them into a table for use with a function "wykonaj".
I found an example about how to count a comma delimiter and I wrote something like that:
//funkcja odczytu liczby czestotliwosci w pliku z karty SD
int ilefreq(char Name[10], int n)
{
card.init(SPI_HALF_SPEED, 10);
File Preset;
SD.begin(10);
Preset = SD.open(Name,FILE_READ);
while (Preset.available()) //wykonuj pętlę dopókiwszystkie dane
{ //nie zostaną zczytane
if (Preset.read() == ',')
{
n += 1;
}
}
Preset.close();
return n + 1;
}
At the return is a "n+1" because last frequency on the file at the card has no comma after.
This code works OK and returns a number of a frequencies in a file "Name".
Now I ought to read a frequencies from a file and put them into a table for next execution.
I found at this forum a thread about how to read CSV files from a SD card and tried to use an example by a fatlib16 user where we can read a CSV file into an array 5x4. I modified a code slightly to achieve a single row of a frequencies, use long type numbers instead of int type and it works OK when displaying the frequencies on a serial monitor but I don't know how to use them as a table for frequency generation. A code looks like:
int ile = 43; // number of the frequencies in a file
// 1 X ile rray
#define ROW_DIM 1
#define COL_DIM ile
File Preset;
/*
Read a file one field at a time.
file - File to read.
str - Character array for the field.
size - Size of str array.
delim - String containing field delimiters.
return - length of field including terminating delimiter.
Note, the last character of str will not be a delimiter if
a read error occurs, the field is too long, or the file
does not end with a delimiter. Consider this an error
if not at end-of-file.
*/
// funkcja okreslajaca dlugosc rekordu
size_t readField(File* file, char* str, size_t size, char* delim) {
char ch;
size_t n = 0;
while ((n + 1) < size && file->read(&ch, 1) == 1) {
// Delete CR.
if (ch == '\r') {
continue;
}
str[n++] = ch;
if (strchr(delim, ch)) {
break;
}
}
str[n] = '\0';
return n;
}
//------------------------------------------------------------------------------
#define errorHalt(msg) {Serial.println(F(msg)); while(1);}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
lcd.begin();
// oczyt danych z karty do tablicy
// Initialize the SD.
if (!SD.begin(CS_PIN)) {
errorHalt("begin failed");
}
// Create or open the file.
Preset = SD.open("Tinitus", FILE_READ); // file Tinitus for test
if (!Preset) {
errorHalt("open failed");
}
// Rewind file so test data is not appended.
// Array for data.
long array[ROW_DIM][COL_DIM];
int i = 0; // First array index.
int j = 0; // Second array index
size_t n; // Length of returned field with delimiter.
char str[10]; // Must hold longest field with delimiter and zero byte.
char *ptr; // Test for valid field.
// Read the file and store the data.
for (i = 0; i < ROW_DIM; i++) {
for (j = 0; j < COL_DIM; j++) {
n = readField(&Preset, str, sizeof(str), ",\n");
if (n == 0) {
errorHalt("Too few lines");
}
array[i][j] = strtoul(str, &ptr, 10);
if (ptr == str) {
errorHalt("bad number");
}
if (j < (COL_DIM - 1) && str[n - 1] != ',') {
errorHalt("line with too few fields");
}
}
// Allow missing endl at eof.
if (str[n - 1] != '\n' && Preset.available()) {
errorHalt("missing endl");
}
}
// Print the array.
for (i = 0; i < ROW_DIM; i++) {
for (j = 0; j < COL_DIM; j++) {
if (j) {
Serial.print(',');
lcd.print(',');
}
Serial.print(array[i][j]);
lcd.print(array[i][j]);
}
Serial.println();
}
Serial.println("Done");
Preset.close();
}
This code works OK writing to a serial monitor display a list of a frequencies stored in a Tinitus file with 43 frequencies inside.
But I still don't know how to use this examples to achieve a table of a frequencies for use by a next function (wykonaj) similar to that which I put into a code manually.
Please someone help me to resolve this problem.
