You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.3 KiB
98 lines
2.3 KiB
6 years ago
|
/*
|
||
|
* TODO: write something here
|
||
|
*/
|
||
|
#include <SevSeg.h>
|
||
|
#include "DHT.h"
|
||
|
|
||
|
// Digital pin connected to the DHT sensor
|
||
|
#define DHTPIN 2
|
||
|
#define DHTTYPE DHT11 // DHT 11
|
||
|
#define BUFFER_SIZE 24
|
||
|
#define LOW_TEMP_LED_PIN 3
|
||
|
#define HIGH_TEMP_LED_PIN 4
|
||
|
#define RELAY_PIN 5
|
||
|
#define LOW_TEMP 28
|
||
|
#define HIGH_TEMP 28
|
||
|
|
||
|
// Initializes DHT sensor.
|
||
|
DHT dht(DHTPIN, DHTTYPE);
|
||
|
|
||
|
int temperatureReadingsCounter;
|
||
|
float temperatures[BUFFER_SIZE];
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(9600);
|
||
|
Serial.println("Running setup function");
|
||
|
|
||
|
dht.begin();
|
||
|
temperatureReadingsCounter = 0;
|
||
|
pinMode(LOW_TEMP_LED_PIN, OUTPUT);
|
||
|
pinMode(HIGH_TEMP_LED_PIN, OUTPUT);
|
||
|
pinMode(RELAY_PIN, OUTPUT);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
float lastTemperature = getNewTemp();
|
||
|
|
||
|
if (lastTemperature >= HIGH_TEMP) {
|
||
|
// Turns red led and relay on
|
||
|
digitalWrite(HIGH_TEMP_LED_PIN, HIGH);
|
||
|
digitalWrite(RELAY_PIN, HIGH);
|
||
|
// Turns blue led off
|
||
|
digitalWrite(LOW_TEMP_LED_PIN, LOW);
|
||
|
} else if (lastTemperature < LOW_TEMP) {
|
||
|
// Turns blue led on
|
||
|
digitalWrite(LOW_TEMP_LED_PIN, HIGH);
|
||
|
// Turns red led off
|
||
|
digitalWrite(HIGH_TEMP_LED_PIN, LOW);
|
||
|
digitalWrite(RELAY_PIN, LOW);
|
||
|
} else {
|
||
|
// Turns red led off
|
||
|
digitalWrite(LOW_TEMP_LED_PIN, LOW);
|
||
|
// Turns blue led off
|
||
|
digitalWrite(HIGH_TEMP_LED_PIN, LOW);
|
||
|
digitalWrite(RELAY_PIN, LOW);
|
||
|
}
|
||
|
|
||
|
if (temperatureReadingsCounter == BUFFER_SIZE) {
|
||
|
float avgTemp = calcAverageTempAndReset(temperatures);
|
||
|
Serial.println(avgTemp);
|
||
|
temperatureReadingsCounter = 0;
|
||
|
}
|
||
|
// Wait a few seconds between measurements.
|
||
|
Serial.println(temperatureReadingsCounter);
|
||
|
delay(1000);
|
||
|
}
|
||
|
|
||
|
float getNewTemp(){
|
||
|
// Reads temperature as Celsius (the default)
|
||
|
float t = dht.readTemperature();
|
||
|
|
||
|
// Checks if the read failed and handles the failure
|
||
|
if (isnan(t)) {
|
||
|
Serial.print(F("Failed to read from DHT sensor!"));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (int i = BUFFER_SIZE - 1; i>0; i--) {
|
||
|
temperatures[i] = temperatures[i-1];
|
||
|
}
|
||
|
temperatures[0] = t;
|
||
|
++temperatureReadingsCounter;
|
||
|
|
||
|
Serial.print(F("Temperature: "));
|
||
|
Serial.print(t);
|
||
|
Serial.println(F("°C "));
|
||
|
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
float calcAverageTempAndReset(float *array) {
|
||
|
float sum = 0 ; // sum will be larger than an item, float for safety.
|
||
|
for (int i = 0; i<BUFFER_SIZE; i++){
|
||
|
sum += array[i];
|
||
|
}
|
||
|
|
||
|
return sum / BUFFER_SIZE ; // average will be fractional
|
||
|
}
|