Creating an array with SD values

Hi guys, I have this code I’ve been working on and it’s working perfectly fine. I’ve been trying to modify it in order to save the voltage values in an array, but it looks like I’m doing something wrong.

I was trying to do two things:

  1. at the same time, I save the voltage values in the SD and in an array;

  2. after I save the voltage values in the SD (the code already does that), I read it outside of the loop and save it in an array.

I also have to use a simple if to determine if the I’m getting the first values (data000.txt) to be my reference, or anything after that (data001.txt, data002…), which I will use to compare with my reference.

After I compare them, I’ll do something like if comparison < 2, green LED turns on, else, red LED turns on.

And by comparison, I mean sum(([data001]-[data000])ˆ2).

What do you guys suggest?

Thanks.

 #define W_CLK 5       // Pin 8 - connect to AD9850 module word load clock pin (CLK)
 #define FQ_UD 9       // Pin 9 - connect to freq update pin (FQ)
 #define DATA 6       // Pin 10 - connect to serial data load pin (DATA)
 #define RESET 7      // Pin 11 - connect to reset pin (RST).
 char filename[20];
 
 
 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
 
 int n=300;
 int sensorValue[300];
 const int chipSelect = 10;
 #include<SD.h>
 
 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
  for (int i=0; i<8; i++, data>>=1) {
    digitalWrite(DATA, data & 0x01);
    pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
  }
}
 
 // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
  int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850
  for (int b=0; b<4; b++, freq>>=8) {
    tfr_byte(freq & 0xFF);
  }
  tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
  pulseHigh(FQ_UD);  // Done!  Should see output
}
 
void setup() {
 // configure arduino data pins for output
  pinMode(FQ_UD, OUTPUT);
  pinMode(W_CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(RESET, OUTPUT);
 
  pulseHigh(RESET);
  pulseHigh(W_CLK);
  pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
  Serial.begin(9600);
    Serial.print("Initializing SD Card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  int l=0;
  snprintf(filename, sizeof(filename), "data%03d.txt", l); // includes a three-digit sequence number in the file name
  while(SD.exists(filename)) {
    l++;
    snprintf(filename, sizeof(filename), "data%03d.txt", l);
  }
  File dataFile = SD.open(filename,FILE_READ);
  
  dataFile.close();
  Serial.println(l);
  Serial.println(filename);
  
}
 
void loop() {
  for (unsigned long fq = 20000; fq<= 40000; fq+=10) {
  sendFrequency(fq);  // freq
  //while(1);
  int x=0;
  int sensorPeakSum = 0;
  for (int i = 2; i<= n; ++i) {
  sensorValue[i] = analogRead(A0);
  
  if (sensorValue[i-2] < sensorValue[i-1] & sensorValue[i-1]>sensorValue[i]) {
    sensorPeakSum+= sensorValue[i-1]; 
    x=x+1;
  }
  else { 
  }
  }
  float voltage = (sensorPeakSum/x)* (10.0/1023.0);
  File dataFile = SD.open(filename, FILE_WRITE);
// if the file is available, write to it:
  if (dataFile) {
    dataFile.print(voltage);
    dataFile.print(",");
    dataFile.print(fq); 
    dataFile.println();
    dataFile.close();
    // print to the serial port too:
    Serial.print(voltage);
    Serial.print(",");
    Serial.print(fq); 
    Serial.println();
  }  
  // if the file isn't open, pop up an error:
  else {
    //Serial.println("error opening file");
  }
 delay(1);
}

while (1) {}
}

I tried that and I think/hope I’m the right way, but I still can’t convert char to float in order to do the right math.

Can anybody help me?

/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */
 
#include <SD.h>

File myFile;
File myFile2;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
 
  // re-open the file for reading:
  float sum = 0;
  myFile = SD.open("data000.txt");
  myFile2 = SD.open("data001.txt");
  if (myFile) {
    Serial.println("FUNCIONANDO 1");
    if (myFile2) {
      Serial.println("FUNCIONANDO 2");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      while (myFile2.available()) {
        sum = sum + (myFile.read()-myFile2.read())*(myFile.read()-myFile2.read());
        Serial.println(sum);
    }
    }
    
    // close the file:
    myFile.close();
    myFile2.close();
  } 
  }
  else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  //Serial.println(sum);
}

void loop()
{
	// nothing happens after setup
}