#include #include #define SENDER_PIN PIN_B2 #define TRIGGER_PIN PIN_B3 #define ECHO_PIN PIN_B4 #define VCC_PIN PIN_B1 #define MAX_DISTANCE 200 // 2 meters #include "SonarSensor.h" #define SEND_INTERVAL 450 // 450*8s = ~1 hour #define SEND_VCC_INTERVAL (SEND_INTERVAL*12) // ~12 hours SonarSensor oilSensor(OIL_SENSOR); volatile int counter = 0; volatile bool shouldSend = true; void setup() { oilSensor.setup(); TinySwitch::setup(SENDER_PIN); TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); } void loop() { if (shouldSend) { shouldSend = false; oilSensor.turnOn(); delay(500); unsigned int distance = oilSensor.scan(); oilSensor.turnOff(); 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; } } }