#include #include #include "SonarSensor.h" #define SEND_INTERVAL 37 // 37*8s = ~5min #define SEND_VCC_INTERVAL (SEND_INTERVAL*6) // every half hour // Pins #define SENDER PIN_B2 SonarSensor oilSensor(OIL_SENSOR); volatile int counter = 0; volatile bool shouldSend = true; void setup() { TinySwitch::setup(SENDER); delay(10000); TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); } void loop() { if (shouldSend) { shouldSend = false; unsigned int distance = oilSensor.scan(); if (distance) { if (counter) { oilSensor.sendValue(distance); } else { oilSensor.sendValueAndVoltage(distance); } } } TinyPower::sleep(); } ISR(WDT_vect) { counter++; if ((counter % SEND_INTERVAL == 0)) { shouldSend = true; if ((counter % SEND_VCC_INTERVAL == 0)) { counter = 0; } } }