optimize sending logic

This commit is contained in:
Nicu Hodos 2022-10-06 15:29:03 +02:00
parent 9ad40ba8e6
commit 7dcbbfa57f
2 changed files with 11 additions and 8 deletions

View File

@ -13,7 +13,7 @@ class SonarSensor : public GenericSensor {
public:
SonarSensor(short id) : GenericSensor(id) {}
unsigned long scan() {
return sonar.ping_median();
unsigned int scan() {
return sonar.convert_cm(sonar.ping_median());
}
};

View File

@ -15,6 +15,7 @@ volatile bool shouldSend = true;
void setup() {
TinySwitch::setup(SENDER);
delay(10000);
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
}
@ -22,13 +23,12 @@ void setup() {
void loop() {
if (shouldSend) {
shouldSend = false;
unsigned long distance = oilSensor.scan();
unsigned int distance = oilSensor.scan();
if (distance) {
if (counter % SEND_VCC_INTERVAL == 0) {
oilSensor.sendValueAndVoltage(distance);
counter = 0;
} else {
if (counter) {
oilSensor.sendValue(distance);
} else {
oilSensor.sendValueAndVoltage(distance);
}
}
}
@ -37,7 +37,10 @@ void loop() {
ISR(WDT_vect) {
counter++;
if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) {
if ((counter % SEND_INTERVAL == 0)) {
shouldSend = true;
if ((counter % SEND_VCC_INTERVAL == 0)) {
counter = 0;
}
}
}