Należało by dopisać własny kawałek kodu aby wartości powyżej 100% traktować jako 100% a nie wartości błędne.
Ja mam coś podobnego zrobione dla czujnika odległości w zbiorniku na pellet. Bez korekty czujnik dawał przeróżne wartości ze względu na to że pojemnik na pellet jest metalowy, dwa że pellet nie jest powierzchnią płaską
Kod: Zaznacz cały
#ifndef _DiffHCSR04_h
#define _DiffHCSR04_h
#include "supla/channel.h"
#include "supla/sensor/distance.h"
#include "supla/sensor/HC_SR04.h"
namespace Supla {
namespace Sensor {
double lastValue = 0.5;
double goodValue = 0.5;
class DiffHCSR04: public Supla::Sensor::HC_SR04 {
public:
DiffHCSR04(int8_t trigPin, int8_t echoPin) : HC_SR04(trigPin, echoPin) {}
double getValue() {
if ((0.5 - Supla::Sensor::HC_SR04::getValue()) > 0) {
goodValue = (0.5 - Supla::Sensor::HC_SR04::getValue());
}
if (goodValue > 0) {
lastValue = goodValue;
return goodValue;
}
else {
return lastValue;
}
}
void iterateAlways() { // Pomiar co XX minut/sekund
if (lastReadTime + 10000 < millis()) {
lastReadTime = millis();
channel.setNewValue(getValue());
}
}
};
}; // namespace Sensor
}; // namespace Supla
#endif