turn on/off sonar sensor
This commit is contained in:
parent
7dcbbfa57f
commit
ef00f5f417
@ -3,17 +3,26 @@
|
|||||||
#include "GenericSensor.h"
|
#include "GenericSensor.h"
|
||||||
#include <NewPing.h>
|
#include <NewPing.h>
|
||||||
|
|
||||||
#define MAX_DISTANCE 200
|
|
||||||
#define TRIGGER_PIN PIN_B3
|
|
||||||
#define ECHO_PIN PIN_B4
|
|
||||||
|
|
||||||
class SonarSensor : public GenericSensor {
|
class SonarSensor : public GenericSensor {
|
||||||
private:
|
private:
|
||||||
NewPing sonar{TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE};
|
NewPing sonar{ TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE };
|
||||||
public:
|
public:
|
||||||
SonarSensor(short id) : GenericSensor(id) {}
|
SonarSensor(short id) : GenericSensor(id) {}
|
||||||
|
|
||||||
unsigned int scan() {
|
void setup() {
|
||||||
return sonar.convert_cm(sonar.ping_median());
|
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 <Arduino.h>
|
||||||
#include <TinyPower.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"
|
#include "SonarSensor.h"
|
||||||
|
|
||||||
#define SEND_INTERVAL 37 // 37*8s = ~5min
|
#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);
|
SonarSensor oilSensor(OIL_SENSOR);
|
||||||
|
|
||||||
@ -14,7 +20,8 @@ volatile int counter = 0;
|
|||||||
volatile bool shouldSend = true;
|
volatile bool shouldSend = true;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
TinySwitch::setup(SENDER);
|
oilSensor.setup();
|
||||||
|
TinySwitch::setup(SENDER_PIN);
|
||||||
delay(10000);
|
delay(10000);
|
||||||
TinyPower::setup();
|
TinyPower::setup();
|
||||||
TinyPower::enableWdt(WDTO_8S);
|
TinyPower::enableWdt(WDTO_8S);
|
||||||
@ -23,7 +30,10 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
if (shouldSend) {
|
if (shouldSend) {
|
||||||
shouldSend = false;
|
shouldSend = false;
|
||||||
|
oilSensor.turnOn();
|
||||||
|
delay(1000);
|
||||||
unsigned int distance = oilSensor.scan();
|
unsigned int distance = oilSensor.scan();
|
||||||
|
oilSensor.turnOff();
|
||||||
if (distance) {
|
if (distance) {
|
||||||
if (counter) {
|
if (counter) {
|
||||||
oilSensor.sendValue(distance);
|
oilSensor.sendValue(distance);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user