turn on/off sonar sensor

This commit is contained in:
Nicu Hodos 2022-10-07 09:34:03 +02:00
parent 7dcbbfa57f
commit ef00f5f417
2 changed files with 33 additions and 14 deletions

View File

@ -3,9 +3,6 @@
#include "GenericSensor.h"
#include <NewPing.h>
#define MAX_DISTANCE 200
#define TRIGGER_PIN PIN_B3
#define ECHO_PIN PIN_B4
class SonarSensor : public GenericSensor {
private:
@ -13,7 +10,19 @@ class SonarSensor : public GenericSensor {
public:
SonarSensor(short id) : GenericSensor(id) {}
void setup() {
pinMode(VCC_PIN, OUTPUT);
}
unsigned int scan() {
return sonar.convert_cm(sonar.ping_median());
}
void turnOn() {
digitalWrite(VCC_PIN, HIGH);
}
void turnOff() {
digitalWrite(VCC_PIN, LOW);
}
};

View File

@ -1,12 +1,18 @@
#include <Arduino.h>
#include <TinyPower.h>
#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 37 // 37*8s = ~5min
#define SEND_VCC_INTERVAL (SEND_INTERVAL*6) // every half hour
#define SEND_VCC_INTERVAL (SEND_INTERVAL*6) // ~30min
// Pins
#define SENDER PIN_B2
SonarSensor oilSensor(OIL_SENSOR);
@ -14,7 +20,8 @@ volatile int counter = 0;
volatile bool shouldSend = true;
void setup() {
TinySwitch::setup(SENDER);
oilSensor.setup();
TinySwitch::setup(SENDER_PIN);
delay(10000);
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
@ -23,7 +30,10 @@ void setup() {
void loop() {
if (shouldSend) {
shouldSend = false;
oilSensor.turnOn();
delay(1000);
unsigned int distance = oilSensor.scan();
oilSensor.turnOff();
if (distance) {
if (counter) {
oilSensor.sendValue(distance);