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: public:
SonarSensor(short id) : GenericSensor(id) {} SonarSensor(short id) : GenericSensor(id) {}
unsigned long scan() { unsigned int scan() {
return sonar.ping_median(); return sonar.convert_cm(sonar.ping_median());
} }
}; };

View File

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