move temperature sensor code inside its own class

This commit is contained in:
Nicu Hodos 2022-01-01 16:07:45 +01:00
parent ead9e06bf9
commit 20749e389a
4 changed files with 50 additions and 22 deletions

View File

@ -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) {
}

View 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;
};

View 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);
}
};

View File

@ -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) {
}