Hello,
I am using an Arduino Nano BLE Sense to collect datas from several sensors. My final goal is to make possible that these datas are saved at different sampling frequency (4 Hz, 32 Hz or 64 Hz depending on the data) on a SD card and are sent by BLE. I am currently working on the part of the program to save on SD card. Here is the basic program for recording one data.
#include <Arduino_HTS221.h> // temperature sensor
#include <SPI.h>
#include <SD.h>
File myFile_T;
float tSensor=0;
float t=0;//time initialisation
const float f_temp=20; //sampling rate of temperature (Hz)
const float delta_temp=1/f_temp*1000; //period between 2 saving (ms)
float last_t_temp =0;
void setup() {
HTS.begin(); //initialisation of the temperature sensor
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("delta_temp \t");
Serial.println(delta_temp);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void loop() {
t=millis();
//Serial.println(t-last_t_temp);
if ((t-last_t_temp)>delta_temp){
Serial.print(t);
Serial.print("\t");
Serial.print(last_t_temp);
Serial.print("\t");
Serial.print(t-last_t_temp);
Serial.print("\t");
tSensor=HTS.readTemperature();
Serial.println(tSensor);
// open the temperature file and write inside
myFile_T = SD.open("temp.txt", FILE_WRITE);
if (myFile_T) { // if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
myFile_T.print(t);
myFile_T.print("\t");
myFile_T.println(tSensor);
myFile_T.close();// close the file:
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening temp.txt");
}
last_t_temp=t;
}
//delay (1000);
}
I have changed sampling rate to check until which value I can go. It seems that it is difficult to go higher than 20 Hz. To overcome that, I tried keep the file opened (so the file is never closed). However, this method leads to empty files. Does anyone would have a solution?
#include <Arduino_HTS221.h> // temperature sensor
#include <SPI.h>
#include <SD.h>
File myFile_T;
float tSensor=0;
float t=0;//initialisation du temps
const float f_temp=4; //fréquence d'enregistrement de la température (en Hz)
const float delta_temp=1/f_temp*1000; //écart entre 2 enregistrements de température (en ms)
float last_t_temp =0;
void setup() {
HTS.begin(); //initialisation of the temperature sensor
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("delta_temp \t");
Serial.println(delta_temp);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the temperature file and write inside
myFile_T = SD.open("temp.txt", FILE_WRITE);
Serial.println("file opened");
}
void loop() {
t=millis();
//Serial.println(t-last_t_temp);
if ((t-last_t_temp)>delta_temp){
Serial.print(t);
Serial.print("\t");
Serial.print(last_t_temp);
Serial.print("\t");
Serial.print(t-last_t_temp);
Serial.print("\t");
tSensor=HTS.readTemperature();
Serial.println(tSensor);
if (myFile_T) { // if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
myFile_T.print(t);
myFile_T.print("\t");
myFile_T.println(tSensor);
}
// myFile_T.close();// close the file:
// Serial.println("done.");
// } else {
// // if the file didn't open, print an error:
// Serial.println("error opening temp.txt");
last_t_temp=t;
}
//delay (1000);
}