How to collect time data and export it into a .CSV file without an RTC module

Hello everyone,

Today I have a question,

Thanks to this forum, I made a dynamometer able to collect data and export it into Excel.
The . CSV file that I obtain is a single column made of values.

In addition of that, I would like to know if it is possible without an RTC to add another column in my .CSV file with the execution time of the program, or not ?

Thanks for your answers.

Serial.print(millis());// time since startup

What do you you mean by execution time ?
The actual time of day, the time that the program takes to run or something else ?

Which Arduino are you using ?

Thanks for your input, my query was inadequate, hence your answer.

I would like to create a kind of table with on the left column, the time of each measurement, and on the right the give measurement, starting from the launch of the program.

Thanks for your answer.

So not the time of day. As suggested, millis() will do this for you

Hello,

Like I launch the Arduino the timer goes on, then I press a button (the tare button of the machine) the timer reset itself and for each measurement that is done every X second I want to collect the value of X and obtain a .CSV file this way:

1 100
2 125
3 150
... ...

I am using an Arduino Uno Rev 3

Won't Millis return me the overall running time ?

Yes, but if you save the value of millis() when you press the tare button then millis() - tareTime will give you the time since pressing the button

Yes, but what about creating the table aforementioned ?

An example

unsigned long currentTime = millis();
unsigned long previousTareTime = 0;
const unsigned long tarePeriod = 1000;
byte seconds = 0;
boolean takingTare = false;

const byte tareButton = A3;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  pinMode(tareButton, INPUT_PULLUP);
}

void loop()
{
  currentTime = millis();
  if (digitalRead(tareButton) == LOW)
  {
    takingTare = true;
  }
  else
  {
    takingTare = false;
  }
  if (takingTare)
  {
    if (currentTime - previousTareTime >= tarePeriod)
    {
      seconds++;
      Serial.print(seconds);
      Serial.print("\t");
      Serial.println(random(100, 200));  //fake tare value for demo purposes
      previousTareTime = currentTime;
    }
  }
}

Hello, thanks for your input, I'll figure out how it works.

Maybe I can join the whole program too.

Give it a try

Basically, while the button is pressed something happens every second. It would be easy to change the sketch so that the first button press starts the process and a second one stops it, but I did not want to confuse the issue

Okay it works as I want, however I can't print two variables at the same time, dunno why.
I use a force sensor and an HX 711 and when I want to
-Print the two variables in two columns in the serial monitor

  • Print the two variables in a CSV file in two columns
    I obtain a single value which neither is the time or the effort, and I don't know why.
    Anyone have a clue on this issue ?

Thanks for the answer.

Post the full sketch that is not working as you want

Yes, I will but in only two hours, I am in the public transportation :frowning:

Here it is:

unsigned long currentTime = millis();
unsigned long previousTareTime = 0;
const unsigned long tarePeriod = 1000;
byte seconds = 0;
boolean takingTare = false;

//-------------------------------------------------------------------------------------------
//Librairies

#include <LiquidCrystal_I2C.h> // Imports the liquid crystal display I²C library
#include <HX711_ADC.h> // Imports the HX711 library to make the loadcell and the HX711 work with the Arduino
#include <SD.h> // Imports the SD library to allow the use of an SD card with the Arduino.

//-------------------------------------------------------------------------------------------
//Initialization

HX711_ADC LoadCell(4, 5); // Allocate the load cell on the pins 4 and 5 of the Arduino, parameters : dt pin, sck pin
LiquidCrystal_I2C lcd(0x27, 16, 2);// Set the LCD address to 0x27 for a 16 chars and 2 line display
File monFichier; // Generate the file which where the data will be printed on
int taree = 6; // Set the push button on the pin 6 of the Arduino

//-------------------------------------------------------------------------------------------

void setup() {
pinMode (taree, INPUT_PULLUP); // Set the tare button as an pull up input

//-------------------------------------------------------------------------------------------
//Serial monitor initialization

Serial.begin(115200); //Initialize the serial monitor to 115200 Bauds

//-------------------------------------------------------------------------------------------
//LCD screen initialization

lcd.begin(); // Initialization of the LCD screen
lcd.backlight(); // Initialize the backlight of the LCD screen

//-------------------------------------------------------------------------------------------
//Texte
{
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Initialisation ..."); // print out to LCD "effort en N"
Serial.print("Initialisation");
delay(5000);
lcd.clear();
}
{
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Maroquinerie"); // print out to LCD "effort en N"
Serial.print("Maroquinerie");

Serial.print("xxx");
lcd.setCursor(0, 1); // set cursor to first row
lcd.print("Auguste Thomas"); // print out to LCD "effort en N"
delay(5000);
lcd.clear();
}
//-------------------------------------------------------------------------------------------
//SD Card initialization

Serial.print("Initialisation de la carte SD en cours..."); // Print on the serial monitor "Initialization of the SD card in process ..."
if (SD.begin()) { // If the SD card is initalized
Serial.println(" Initialisation terminee."); // Print on the serial monitor Initialization over
} else { // If the Initialization does not work
Serial.println(" Echec de l'initialisation."); // Print on the serial monitor Initialization failure
while (true); // We stop the program there if there is a failure in the initialization of the SD card
}

//-------------------------------------------------------------------------------------------
//SD Writing process

monFichier = SD.open("donnees.csv", FILE_WRITE); // We create the file if needed, otherwise we write data into the file
if (monFichier) {
monFichier.println(F("Time,Strength")); // Prints the text "Strength" on the text file
Serial.println(F("Time,Strength")); // Prints the text "Strength" on the serial monitor
monFichier.close(); // Closes the text file
} else { // If the writing process does not work,
Serial.println(F("Writing failure"));// Print on the serial monitor "Writing failure"
while (true); // We stops the program there in case of a writing failure
}

//-------------------------------------------------------------------------------------------
//Load cell initialization

LoadCell.begin(); // Connexion to the HX711 amplifier
LoadCell.start(2000); // The loadcell takes 2000ms to stabilize
LoadCell.setCalFactor(42.5); // Setting the calibration factor, which is unique to each loadcell
}

//-------------------------------------------------------------------------------------------
// Retrieve and print the sensor data on the LCD screen

void loop() {
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Effort en Kg"); // print out to LCD "effort en N"
LoadCell.update(); // retrieves data from the load cell
float force = LoadCell.getData()/98.07; // get output value in Newtons
lcd.setCursor(0, 1); //Set cursor to the second line, first row
if (force >= 0)// If the strength measured by the sensor is over 0
lcd.print(" "); // Print " "
lcd.print(force, 1); // print out the retrieved value to the second row

//-------------------------------------------------------------------------------------------
//Peak function

static float forceMax = 0; // Define the variable maximum strength as a static float
if (force > forceMax) // If the instant strenght is over the maximum strength
{
forceMax = force; // the maximum strenght takes the value of the instant strenght
lcd.setCursor(9, 1); //Set cursor to the second line and nineth row
lcd.print (forceMax, 1); // Print the value of the maximum strenght on the LCD screen
}

//-------------------------------------------------------------------------------------------
//Tare function

if (digitalRead (taree) == HIGH) // If the tare button is pressed
{
lcd.setCursor(0, 1); // set cursor to second row
lcd.print(" Tare... "); // print "tare ..." on rhe second row of the LCD screen
Serial.println("taree");
LoadCell.start(1000); //Reboot the loadcell for a duration of 1000 ms
lcd.setCursor(0, 1); //set cursor to second row
lcd.print(" "); //Print " " on the LCD screen
forceMax=0;
}

//-------------------------------------------------------------------------------------------
//Writing the strenght data on the .csv file

monFichier = SD.open("donnees.csv", FILE_WRITE); //Open the file "donnes.csv" and write into it
if (monFichier) { // if mon fichier
monFichier.print(force); //Print the stenght value on the .csv file
monFichier.println(random(100,200));
Serial.print(force); // Print the strenght value on the serial monitor
Serial.print("\t");
Serial.println(random(100,200));
monFichier.close(); // Close the .csv file
} else { // Otherwise, if the writing process does not work
Serial.println(F("Impossible d'ouvrir le fichier")); // Print on the serial monitor ''Impossible d'ouvrir le fichier"
}

//-------------------------------------------------------------------------------------------
//Delay between each measure

delay(500); // Set a delay of 50ms between each measure

On a side note, the peak function does not work anymore.

Your code appears to be incomplete

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

unsigned long currentTime = millis();
unsigned long previousTareTime = 0;
const unsigned long tarePeriod = 1000;
byte seconds = 0;
boolean takingTare = false;

//-------------------------------------------------------------------------------------------
//Librairies

#include <LiquidCrystal_I2C.h> // Imports the liquid crystal display I²C library
#include <HX711_ADC.h> // Imports the HX711 library to make the loadcell and the HX711 work with the Arduino
#include <SD.h> // Imports the SD library to allow the use of an SD card with the Arduino.

//-------------------------------------------------------------------------------------------
//Initialization

HX711_ADC LoadCell(4, 5); // Allocate the load cell on the pins 4 and 5 of the Arduino, parameters : dt pin, sck pin
LiquidCrystal_I2C lcd(0x27, 16, 2);// Set the LCD address to 0x27 for a 16 chars and 2 line display
File monFichier; // Generate the file which where the data will be printed on
int taree = 6; // Set the push button on the pin 6 of the Arduino

//-------------------------------------------------------------------------------------------

void setup() {
pinMode (taree, INPUT_PULLUP); // Set the tare button as an pull up input

//-------------------------------------------------------------------------------------------
//Serial monitor initialization

Serial.begin(115200); //Initialize the serial monitor to 115200 Bauds

//-------------------------------------------------------------------------------------------
//LCD screen initialization

lcd.begin(); // Initialization of the LCD screen
lcd.backlight(); // Initialize the backlight of the LCD screen

//-------------------------------------------------------------------------------------------
//Texte
{
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Initialisation ..."); // print out to LCD "effort en N"
Serial.print("Initialisation");
delay(5000);
lcd.clear();
}
{
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Maroquinerie"); // print out to LCD "effort en N"
Serial.print("Maroquinerie");

Serial.print("xxx");
lcd.setCursor(0, 1); // set cursor to first row
lcd.print("xxx"); // print out to LCD "effort en N"
delay(5000);
lcd.clear();
}
//-------------------------------------------------------------------------------------------
//SD Card initialization

Serial.print("Initialisation de la carte SD en cours..."); // Print on the serial monitor "Initialization of the SD card in process ..."
if (SD.begin()) { // If the SD card is initalized
Serial.println(" Initialisation terminee."); // Print on the serial monitor Initialization over
} else { // If the Initialization does not work
Serial.println(" Echec de l'initialisation."); // Print on the serial monitor Initialization failure
while (true); // We stop the program there if there is a failure in the initialization of the SD card
}

//-------------------------------------------------------------------------------------------
//SD Writing process

monFichier = SD.open("donnees.csv", FILE_WRITE); // We create the file if needed, otherwise we write data into the file
if (monFichier) {
monFichier.println(F("Time,Strength")); // Prints the text "Strength" on the text file
Serial.println(F("Time,Strength")); // Prints the text "Strength" on the serial monitor
monFichier.close(); // Closes the text file
} else { // If the writing process does not work,
Serial.println(F("Writing failure"));// Print on the serial monitor "Writing failure"
while (true); // We stops the program there in case of a writing failure
}

//-------------------------------------------------------------------------------------------
//Load cell initialization

LoadCell.begin(); // Connexion to the HX711 amplifier
LoadCell.start(2000); // The loadcell takes 2000ms to stabilize
LoadCell.setCalFactor(42.5); // Setting the calibration factor, which is unique to each loadcell
}

//-------------------------------------------------------------------------------------------
// Retrieve and print the sensor data on the LCD screen

void loop() {
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Effort en Kg"); // print out to LCD "effort en N"
LoadCell.update(); // retrieves data from the load cell
float force = LoadCell.getData()/98.07; // get output value in Newtons
lcd.setCursor(0, 1); //Set cursor to the second line, first row
if (force >= 0)// If the strength measured by the sensor is over 0
lcd.print(" "); // Print " "
lcd.print(force, 1); // print out the retrieved value to the second row

//-------------------------------------------------------------------------------------------
//Peak function

static float forceMax = 0; // Define the variable maximum strength as a static float
if (force > forceMax) // If the instant strenght is over the maximum strength
{
forceMax = force; // the maximum strenght takes the value of the instant strenght
lcd.setCursor(9, 1); //Set cursor to the second line and nineth row
lcd.print (forceMax, 1); // Print the value of the maximum strenght on the LCD screen
}

//-------------------------------------------------------------------------------------------
//Tare function

if (digitalRead (taree) == HIGH) // If the tare button is pressed
{
lcd.setCursor(0, 1); // set cursor to second row
lcd.print(" Tare... "); // print "tare ..." on rhe second row of the LCD screen
Serial.println("taree");
LoadCell.start(1000); //Reboot the loadcell for a duration of 1000 ms
lcd.setCursor(0, 1); //set cursor to second row
lcd.print(" "); //Print " " on the LCD screen
forceMax=0;
}

//-------------------------------------------------------------------------------------------
//Writing the strenght data on the .csv file

monFichier = SD.open("donnees.csv", FILE_WRITE); //Open the file "donnes.csv" and write into it
if (monFichier) { // if mon fichier
monFichier.print(force); //Print the stenght value on the .csv file
monFichier.println(random(100,200));
Serial.print(force); // Print the strenght value on the serial monitor
Serial.print("\t");
Serial.println(random(100,200));
monFichier.close(); // Close the .csv file
} else { // Otherwise, if the writing process does not work
Serial.println(F("Impossible d'ouvrir le fichier")); // Print on the serial monitor ''Impossible d'ouvrir le fichier"
}

//-------------------------------------------------------------------------------------------
//Delay between each measure

delay(500); // Set a delay of 50ms between each measure
delay(500); // Set a delay of 50ms between each measure

Is this really the last line of code in the sketch ?