Bonjour à toutes et à tous,
Toujours dans l'idée de réaliser mon dynamomètre, avec fonction crête, je suis arrivé a un résultat intéressant grâce à la contribution de certains membres du forum qui m'ont vraiment beaucoup aidé.
Un membre du forum a révisé mon code en proposant de nouvelles fonctions et une nouvelle architecture, cependant, j'ai l'impression que tout ou partie du code lié à la carte SD fait planter le code global dans la mesure ou j'ai fait le test de faire fonctionner le circuit avec et sans les lignes de code liées au module SD, dans un cas, cela fonctionne, dans l'autre non. Cela a été rédigé par quelqu'un qui s'y connaît mieux que moi, donc je ne doute pas de la "véracité" de son code, cependant, j'aimerais comprendre pourquoi, la fonction SD fait que mon circuit ne lit plus l'effort du capteur.
Merci d'avance pour votre aide,
Cordialement,
Vincent
PS: le premier code est sans la fonction SD, le second, avec.
#include <HX711_ADC.h> // https://github.com/olkal/HX711_ADC
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
#include <SD.h> // Importe la librairie SD pour permettre la gestion de la carte SD
HX711_ADC LoadCell(4, 5); // parameters: dt pin, sck pin
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int led = 2;
int taree = 6;
int buzzer = 10;
int a = 0;
float b = 0;
float j = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode (taree, INPUT_PULLUP);
LoadCell.begin(); // start connection to HX711
LoadCell.start(1000); // load cells gets 2000ms of time to stabilize
LoadCell.setCalFactor(42.5); // calibration factor for load cell => strongly dependent on your individual setup
lcd.begin(); // begins connection to the LCD module
lcd.backlight(); // turns on the backlight
lcd.setCursor(1, 0); // set cursor to first row
lcd.print("Dynamometre"); // print out to LCD
lcd.setCursor(0, 1); // set cursor to first row
lcd.print("Par Vincent M"); // print out to LCD
delay(3000);
lcd.clear();
}
void loop() {
lcd.setCursor(1, 0); // set cursor to first row
lcd.print("Valeur d'effort"); // print out to LCD
LoadCell.update(); // retrieves data from the load cell
float i = LoadCell.getData(); // get output value
if (i<0)
{
i = i * (-1);
lcd.setCursor(0, 1);
lcd.print("-");
}
else
{
lcd.setCursor(0, 1);
lcd.print(" ");
}
lcd.setCursor(1, 1); // set cursor to second row
lcd.print(i, 1); // print out the retrieved value to the second row
lcd.print("g ");
if (digitalRead (taree) == HIGH)
{
lcd.setCursor(0, 1); // set cursor to secon row
lcd.print(" Taring... ");
LoadCell.start(1000);
lcd.setCursor(0, 1);
lcd.print(" ");
}
delay(100);
}
//-------------------------------------------------------------------------------------------
//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
//-------------------------------------------------------------------------------------------
//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("Strength")); // Prints the text "Strength" on the text file
Serial.println(F("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
//-------------------------------------------------------------------------------------------
//LCD screen initialization
lcd.begin(); // Initialization of the LCD screen
lcd.backlight(); // Initialize the backlight of the LCD screen
}
//-------------------------------------------------------------------------------------------
// 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 N"); // print out to LCD "effort en N"
LoadCell.update(); // retrieves data from the load cell
float force = LoadCell.getData()*9.8; // 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
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
}
//-------------------------------------------------------------------------------------------
//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.println(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(5000); // Set a delay of 5000ms between each measure
}