Hi everyone! I'm not a big expert of coding.
I'm developing a code which allows me to record force applied in a bag ( through a load cell the system is able to detect the force applied on it ), what I want design is that the system recording in real-time always ( that already works ) and when I hit the bag with a punch, I would that system print only the maximum value in only one string of text ( that almost works ). Furthermore, I want to substitute the delay function with milliss, but as I said I'm not an expert and I don't know how to use millis in a system that start, run always and it needs just a sort of countdown in some part of code where it request to be stopped for 2 seconds and then restart again to work and start from that point it left.
#include<Keyboard.h>
#include <HX711.h>
#define calibration_factor -9850
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
// Dichiarazione delle variabili
HX711 scale;
float measurement;
float startpoint;
float highMeasurement;
float sogliafissa;
float stopsogliafissa;
bool startedRecording;
void setup() {
//Inizializzazione Board Arduino
Serial.begin(9600);
Keyboard.begin();
while(!Serial);
//Inizializzazione dei PIN di lavoro cella di carico
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
//Inizializzazione condizioni di lavoro cella di carico
measurement = 0;
startpoint = 1;
sogliafissa = 1;
stopsogliafissa = -1;
highMeasurement = 0;
startedRecording = false;
Serial.println("Waiting for Highest Measurement...");
}
void loop() {
//Attivazione acquisizione dati da cella di carico
measurement = scale.get_units();
if (measurement < startpoint){
startedRecording = true;
highMeasurement = measurement;
}
else if (measurement > startpoint){
startpoint = measurement;
highMeasurement = startpoint;
delay(300);
startpoint = sogliafissa;
if (startedRecording){
finish();
}
}
}
//Stampare potenza rilevata sul Serial Monitor
void finish(){
Serial.print("Highest Measurement: ");
Serial.print(highMeasurement, 1);
Serial.print("Kg");
Serial.println();
}
This is the code that I wrote, using the delay works but I know it might create problem when the program is run for a long time.
Do you have suggestion about how to improve my code ?