Hello everybody
I'm new of the forum, thanks in advance for the precious contribute that many users give! seriously
Hope to have searched well inside the forum since I did not find any solution for my problem that works.
I'm doing a project with my Arduino UNO, connected to a couple of sensors (tri-axial gyro and accel.), I'm storing the values read from sensors inside an SD card, in a file called for example "FILE 1".
My purpose is to acquire data and process them, so I do need to open FILE 1, read the three reading of angular acceleration (contained in a single line), and process them calculating the derivative.
In order to do processing in the meantime I read data from FILE 1 I need to open another file, let's call it "FILE 2" to save processed data. In the code below I'trying to read LINE 4 inside FILE 1 and when it happens, open FILE 2 and write just a random number.
The entire code is pretty heavy so I just upload the part relating to Read-Write on SD card, if anybody need the rest I can upload it.
Thanks to everyone that will help me!!
F.
#include "I2cSerial.h"
#include "TimerOne.h"
#include "JointCalibration.h"
#include <SPI.h>
#include <SD.h>
#define CYCLE 10
I2cSerial *i2c = new I2cSerial();
JointCalibration *jC = new JointCalibration();
// Interrupt Constants y
const long int ISR_PERIOD = 20000.00;Â Â Â // ISR Clock value in (us)
volatile float deltatime = ISR_PERIOD / 1000000.0000;
const byte chipSelect = 4;
// Variables
static float W1[3];Â Â Â Â Â Â Â Â Â Â Â Â // angular Velocities
static float W2[3];
// Flag and Counters
int new_data = 0;Â Â Â Â Â Â Â Â Â Â Â Â // flag for new read data from gyro and Acc
int isr_flag = 0;Â Â Â Â Â Â Â Â Â Â Â Â // Interrupt positive flag
volatile int count_intr = 0;Â Â Â Â Â Â Â Â // Interrupt counter
int count_data = 0;
byte good_calib = 0;Â Â Â Â Â Â Â Â Â Â Â // counter for calibration cycle
volatile byte state = 0;Â Â Â Â Â Â Â Â Â // counter for setup calibration
boolean logic = false;
byte flagCalib = 1;Â Â Â Â Â Â Â Â Â Â Â Â // flag to create angular velocities matrix
File myFile;Â Â Â Â // creation of new file on SD card
/*Reading SD Card Parameters*/
float aW1[3];
float a1[3];
float der1[3];
void setup() {
 Serial.begin(9600);
Â
 // LED INITIALIZATION
 pinMode(9, OUTPUT);
 pinMode(5, OUTPUT);
 // TURN ON SENSORS
 i2c->i2cSetAD0();                    // Setup Scale Sensors
 i2c->i2cSetAD1();
Â
  if (!SD.begin(4)){
   Serial.print(F("\n\nInitialization failed!"));
   while(1);Â
  }
  Serial.print(F("\n\nInitialization done"));
  //################### Removing files from SD CARD
   Serial.print(F("\n-->Check if 1st Calibration File already exists"));
   Serial.print(F("\nSearching for 1st Calibration file..."));
    if (SD.exists("calib1.txt")){
     Serial.print(F(" File present!"));
     Serial.print(F("\n\n...Proceeding removing existent file..."));Â
     Serial.print(SD.remove("calib1.txt"));
     if (SD.exists("calib1.txt")){
      Serial.print(F("\nError removing 1st Calibration File"));                                  Â
       }else{
        Serial.print(F("\nFile removed successfully!"));
       } Â
    } else {
     Serial.print(F("\n1st Calibration File NOT Present on SD"));
    }
    Serial.print(F("\n\n-->Check if 1st Calibration Processing File already exists"));
    Serial.print(F("\nSearching for 1st Calibration Processing file..."));
    if (SD.exists("calP1.txt")){
     Serial.print(F(" File present!"));
     Serial.print(F("\n\n...Proceeding removing existent file..."));Â
     Serial.print(SD.remove("calP1.txt"));
     if (SD.exists("calP1.txt")){
      Serial.print(F("\nError removing 1° Calibration File"));                                  Â
       }else{
        Serial.print(F("\nFile removed successfully!"));
       } Â
    } else {
     Serial.print(F("\n1st Calibration Processing File NOT Present on SD"));
    }
 Serial.print(F("\n\nCreation of a new 1st Calibration File..."));
Â
 // Remember files might be named with 8.3 DOS eg: 12345678.123
 myFile = SD.open("calib1.txt", FILE_WRITE);
 Serial.print(F("\nCheck if file has been created-->"));
 Serial.print(SD.exists("calib1.txt"));
 (myFile == true) ? (Serial.print(F("\nDIO C'e'"))) : (Serial.print(F("\nDIO NON C'e'")));Â
}
void loop() {
Â
 Timer1.initialize(ISR_PERIOD);              // initialize timer1, and set a the period in millisecond
 Timer1.attachInterrupt(isrMethod);            // attaches ISR() as a timer interrupt function
 if (!logic){
  if (myFile){
   Serial.print(F("\n\nTest writing to SD file"));
   while(( count_intr < CYCLE )){
    readData();                     // Reading data from sensors
    if (isr_flag == 1){
//Â Â Â Â Â printIsrGyro();
     isr_flag = 0;                    // reinizializzazione del flag dell'interrupt
     for (int i=0; i<3; i++){
     /* Writing angular velocities*/
      myFile.print(W1[i],4);            Â
      myFile.print("\t");
     }
     myFile.print("\n");
    }
  }
 /*closing the file*/
 myFile.close();
 Serial.println(F ("\nClosing File ...Done"));Â
  } else {  Â
    Serial.println(F("\n\nerror during Writing on myFile"));   // Error message!
    myFile.close();
  }
 Â
  /* Reopen file for Reading and PROCESSING*/ <--- HERE WHERE COMES TROUBLE!
  Serial.println(SD.remove("CALP2.txt"));
  int recNum1 = 0;
  int num = 888;
  myFile = SD.open("calib1.txt");
  while(myFile.available()){
   String valueD = myFile.readStringUntil('\t');
   recNum1++;
   Serial.print(valueD);
   Serial.print("\t");
   Serial.print(recNum1);
   Serial.print("\n");
   if(recNum1 == 4){
    File myFile2 = SD.open("calP2.txt",FILE_WRITE);
    Serial.println(myFile2);
    myFile2.print(num);
    myFile2.close();
    digitalWrite(5,HIGH);
    delay(500);
    digitalWrite(5,LOW);
   }
  }
//Â Â myFile.close();
 logic = true;
 }
}
void readData() {Â Â Â Â // Values read from Gyro without Offset
 i2c->i2cReadGyroAD0(0);
 new_data = 1;
 count_data++;
}
void isrMethod() {Â Â Â Â Â
 if (new_data == 1) {
  digitalWrite(9, digitalRead(9) ^ 1);
  W1[0] = (i2c->rot1.x);
  W1[1] = (i2c->rot1.y);
  W1[2] = (i2c->rot1.z);
  isr_flag = 1;
  new_data = 0;
  count_intr++;
 }
}Â
void printIsrGyro(){
 Serial.print("\n");
 Serial.print(count_intr);
 Serial.print(" ");
 Serial.print(count_data);
 Serial.print(" ");
  for (int i=0;i<3;i++){
   Serial.print(W1[i],4);
   Serial.print("\t");Â
  }Â
}