Bonsoir à toutes et à tous,
Je tiens à préciser que j'ai aussi fait un thread sur le forum anglais afin d'avoir une plus grande diversité de réponse, mais comme je suis Français, je me dis que je peux aussi tenter ma chance ici.
Donc je vous explique la situation:
J'ai crée à l'aide de ce forum, un dynamomètre (une balance quoi) qui stocke les valeurs de force sur une carte SD et qui crée un fichier .CSV.
J'aimerais modifier ce programme pour avoir en plus de la valeur de force le temps entre chaque essai.
En gros, on lance un timer dès l'allumage de l'arduino, on tare la machine avant l'essai mécanique ce qui a pour effet de remettre à zéro le timer, on réalise notre essai mécanique et on affiche sur le moniteur série et sur le fichier .CSV deux colonnes, une avec le temps en secondes et l'autre avec la valeur d'effort.
Je suis pas très fort en code et j'ai eu beau essayer de faire des trucs, j'ai à peu près réussi à afficher le temps et l'effort en simultané sur le moniteur série avec un programme que l'on m'a donné, cependant la fonction de remise à zéro du temps et la réalisation des deux colonnes dans le fichier .CSV ne marchent pas et je sais pas pourquoi.
Ce projet est urgent, et j'ai pas le temps d'acheter de me faire livrer et de paramétrer un module RTC d'ou ma demande de le faire sans module RTC.
Je vous remercie pour votre aide,
//Variables
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("xxx"); // print out to LCD "effort en N"
Serial.print("xxx");
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;
}
//-------------------------------------------------------------------------------------------
//Counting seconds and printing the data on the serial monitor and SD card
{
currentTime = millis();
if (digitalRead(taree) == LOW)
{
takingTare = true;
}
else
{
takingTare = false;
seconds = 0;
}
if (takingTare)
{
if (currentTime - previousTareTime >= tarePeriod)
{
seconds++;
previousTareTime = currentTime;
monFichier = SD.open("donnees.csv", FILE_WRITE); //Open the file "donnes.csv" and write into it
if (monFichier) { // if mon fichier
monFichier.print(seconds);
Serial.print(seconds); // Print the seconds value on the serial monitor
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"
}
}
}
}
//-------------------------------------------------------------------------------------------
//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
Serial.println(force); // Print the strenght value on the serial monitor
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(1000); // Set a delay of 50ms between each measure
}
