turn on/off sonar sensor
This commit is contained in:
parent
7dcbbfa57f
commit
ef00f5f417
@ -3,17 +3,26 @@
|
||||
#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:
|
||||
NewPing sonar{TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE};
|
||||
public:
|
||||
SonarSensor(short id) : GenericSensor(id) {}
|
||||
private:
|
||||
NewPing sonar{ TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE };
|
||||
public:
|
||||
SonarSensor(short id) : GenericSensor(id) {}
|
||||
|
||||
unsigned int scan() {
|
||||
return sonar.convert_cm(sonar.ping_median());
|
||||
}
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user