move temperature sensor code inside its own class
This commit is contained in:
parent
ead9e06bf9
commit
20749e389a
@ -2,11 +2,11 @@
|
||||
|
||||
#include <TinySensor.h>
|
||||
|
||||
class TempSensor : public TinySensor {
|
||||
class TemperatureSensor : public TinySensor {
|
||||
SensorType sensorType = TEMPERATURE;
|
||||
|
||||
public:
|
||||
TempSensor(short id) :
|
||||
TemperatureSensor(short id) :
|
||||
TinySensor(id) {
|
||||
}
|
||||
|
||||
13
temp_sensor/include/TempSensor.h
Normal file
13
temp_sensor/include/TempSensor.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <TemperatureSensor.h>
|
||||
|
||||
class TempSensor : public TemperatureSensor {
|
||||
public:
|
||||
TempSensor(short id) :
|
||||
TemperatureSensor(id) {
|
||||
}
|
||||
|
||||
virtual void setup() = 0;
|
||||
virtual int readTemp() = 0;
|
||||
};
|
||||
30
temp_sensor/include/Tmp36Sensor.h
Normal file
30
temp_sensor/include/Tmp36Sensor.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "TempSensor.h"
|
||||
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define TEMP_PIN A2
|
||||
|
||||
class Tmp36Sensor : public TempSensor {
|
||||
public:
|
||||
Tmp36Sensor(short id) :
|
||||
TempSensor(id) {
|
||||
}
|
||||
|
||||
void setup() override {
|
||||
analogReference(INTERNAL);
|
||||
pinMode(TEMP_POSITIVE, OUTPUT);
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
}
|
||||
|
||||
int readTemp() override {
|
||||
digitalWrite(TEMP_POSITIVE, HIGH);
|
||||
delay(10);
|
||||
int reading = analogRead(TEMP_PIN);
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
|
||||
float voltage = reading * (1100 / 1024.0);
|
||||
float temperatureC = (voltage - 500) / 10;
|
||||
return roundf(temperatureC * 10);
|
||||
}
|
||||
};
|
||||
@ -1,5 +1,5 @@
|
||||
#include <Arduino.h>
|
||||
#include <TempSensor.h>
|
||||
#include "Tmp36Sensor.h"
|
||||
#include <TinyPower.h>
|
||||
#include <SoftwareSerial_Tiny.h>
|
||||
|
||||
@ -7,21 +7,17 @@
|
||||
#define SEND_VCC_INTERVAL (int)(60*60/8)
|
||||
|
||||
// Pins
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define SENDER PIN_B2
|
||||
#define TEMP_PIN A2
|
||||
|
||||
int readTemp();
|
||||
|
||||
TempSensor sensor = TempSensor(TEMP_SENSOR);
|
||||
TempSensor &sensor = *(new Tmp36Sensor(TEMP_SENSOR));
|
||||
|
||||
volatile int counter = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
analogReference(INTERNAL);
|
||||
pinMode(TEMP_POSITIVE, OUTPUT);
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
sensor.setup();
|
||||
|
||||
TinySwitch::setup(SENDER);
|
||||
TinyPower::setup();
|
||||
@ -30,25 +26,14 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
if (counter % SEND_VCC_INTERVAL == 0) {
|
||||
sensor.sendTempAndVoltage(readTemp());
|
||||
sensor.sendTempAndVoltage(sensor.readTemp());
|
||||
counter = 0;
|
||||
} else if (counter % SEND_INTERVAL == 0) {
|
||||
sensor.sendTemp(readTemp());
|
||||
sensor.sendTemp(sensor.readTemp());
|
||||
}
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
int readTemp() {
|
||||
digitalWrite(TEMP_POSITIVE, HIGH);
|
||||
delay(10);
|
||||
int reading = analogRead(TEMP_PIN);
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
|
||||
float voltage = reading * (1100 / 1024.0);
|
||||
float temperatureC = (voltage - 500) / 10;
|
||||
return roundf(temperatureC * 10);
|
||||
}
|
||||
|
||||
ISR(PCINT0_vect) {
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user