T-DEV-811/IOT/lib/Capteur/src/Balance.cpp

57 lines
1.5 KiB
C++
Raw Normal View History

2023-04-11 12:35:25 +02:00
#include "../include/Balance.h"
2023-05-02 22:01:10 +02:00
Balance::Balance(int doutPin, int sckPin, String fullVall):
Capteur("W", fullVall){
2023-04-11 12:35:25 +02:00
2023-05-02 22:01:10 +02:00
this->capteur = new HX711();
this->capteur->begin(doutPin, sckPin);
2023-04-11 12:35:25 +02:00
this->initialized = false;
2023-05-02 22:01:10 +02:00
this->capteur->set_scale();
this->capteur->tare();
2023-04-11 12:35:25 +02:00
this->initialVal = 0;
this->calibrationFact = 0;
2023-05-02 22:01:10 +02:00
this->initCalibration();
2023-04-11 12:35:25 +02:00
}
boolean Balance::initCalibration() {
2023-05-02 22:01:10 +02:00
this->capteur->set_scale();
this->capteur->tare();
this->initialVal = this->capteur->read_average(20);
2023-04-11 12:35:25 +02:00
return true;
}
boolean Balance::calibration(int poidsRef,int moyenne_calibration) {
int rawValref = 0; // Valeur brute de plexiglas et poids réf
int tempPoids = 0; // C'est la valeur du poids en grammes calculée grâce au facteur de calibration
2023-05-02 22:01:10 +02:00
rawValref = this->capteur->read_average(moyenne_calibration);
2023-04-11 12:35:25 +02:00
this->calibrationFact = (rawValref - this->initialVal) / poidsRef;
do {
2023-05-02 22:01:10 +02:00
this->capteur->set_scale(calibrationFact);
tempPoids = this->capteur->get_units(5);
2023-04-11 12:35:25 +02:00
if (tempPoids < poidsRef) {
calibrationFact -= 1;
} else if (tempPoids > poidsRef) {
calibrationFact += 1;
}
} while (tempPoids != poidsRef);
return initialized = true;
}
2023-05-02 22:01:10 +02:00
String Balance::read() {
2023-04-11 12:35:25 +02:00
2023-05-02 22:01:10 +02:00
if (this->initialized == true) {
return String(this->capteur->get_units());
2023-04-11 12:35:25 +02:00
} else {
2023-05-02 22:01:10 +02:00
return "0";
2023-04-11 12:35:25 +02:00
}
}
void Balance::setCalibrationFact(int caliFact){
this->initialized = true;
this->calibrationFact = caliFact;
2023-05-02 22:01:10 +02:00
this->capteur->set_scale(caliFact);
2023-04-11 12:35:25 +02:00
}