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.
173 lines
4.8 KiB
173 lines
4.8 KiB
6 years ago
|
/*
|
||
|
* TODO: write something here
|
||
|
*/
|
||
6 years ago
|
#include "projectGroot.h"
|
||
6 years ago
|
|
||
6 years ago
|
/* ===== SETUP AND LOOP ===== */
|
||
6 years ago
|
void setup() {
|
||
6 years ago
|
// Starts the serial com with PC
|
||
6 years ago
|
Serial.begin(9600);
|
||
|
Serial.println("Running setup function");
|
||
|
|
||
6 years ago
|
// Other initializations
|
||
6 years ago
|
dht.begin();
|
||
|
temperatureReadingsCounter = 0;
|
||
|
pinMode(LOW_TEMP_LED_PIN, OUTPUT);
|
||
|
pinMode(HIGH_TEMP_LED_PIN, OUTPUT);
|
||
|
pinMode(RELAY_PIN, OUTPUT);
|
||
6 years ago
|
|
||
6 years ago
|
pinMode(DIST_TRIG_PIN, OUTPUT);
|
||
|
pinMode(DIST_ECHO_PIN, INPUT);
|
||
|
|
||
6 years ago
|
byte numDigits = NUMBER_OF_DIGITS;
|
||
|
byte digitPins[] = {DIGIT_1_PIN, DIGIT_2_PIN, DIGIT_3_PIN, DIGIT_4_PIN};
|
||
|
byte segmentPins[] = {SEGMENT_A_PIN, SEGMENT_B_PIN, SEGMENT_C_PIN, SEGMENT_D_PIN, SEGMENT_E_PIN,
|
||
|
SEGMENT_F_PIN, SEGMENT_G_PIN, DECIMAL_POINT_PIN};
|
||
|
bool resistorsOnSegments = false;
|
||
|
byte hardwareConfig = COMMON_CATHODE;
|
||
|
bool updateWithDelays = false;
|
||
|
bool leadingZeros = false;
|
||
|
bool disableDecPoint = false;
|
||
|
|
||
|
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
|
||
|
updateWithDelays, leadingZeros, disableDecPoint);
|
||
|
sevseg.setBrightness(100);
|
||
6 years ago
|
}
|
||
|
|
||
|
void loop() {
|
||
6 years ago
|
// Gets new readings
|
||
6 years ago
|
if (!lastReading || millis() - lastReading > SLEEP_INTERVAL) {
|
||
|
getNewSamples();
|
||
|
lastReading = millis();
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
float distance = getDistance();
|
||
|
|
||
|
if (distance < DIST_THRESHOLD || (millis() - displayTimeStart) < TEMPERATURE_DISPLAY_DURATION) {
|
||
|
// Should display the average temperature
|
||
6 years ago
|
sevseg.setNumber(averageTemp, 1);
|
||
|
sevseg.refreshDisplay();
|
||
6 years ago
|
} else {
|
||
|
sevseg.blank();
|
||
|
sevseg.refreshDisplay();
|
||
6 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/* ===== FUNCTION IMPLEMENTATIONS ===== */
|
||
|
void getNewSamples() {
|
||
6 years ago
|
float lastTemperature = getNewTemp();
|
||
|
|
||
|
if (lastTemperature >= HIGH_TEMP) {
|
||
6 years ago
|
// Turns red led on
|
||
6 years ago
|
digitalWrite(HIGH_TEMP_LED_PIN, HIGH);
|
||
6 years ago
|
sendEmail(CASE_HIGH_TEMP, 0);
|
||
6 years ago
|
//todo send email
|
||
6 years ago
|
} else {
|
||
6 years ago
|
// Turns red led off
|
||
|
digitalWrite(HIGH_TEMP_LED_PIN, LOW);
|
||
6 years ago
|
}
|
||
|
if (lastTemperature < LOW_TEMP){
|
||
6 years ago
|
// Turns blue led on
|
||
|
digitalWrite(LOW_TEMP_LED_PIN, HIGH);
|
||
6 years ago
|
sendEmail(CASE_LOW_TEMP, 0);
|
||
6 years ago
|
//todo send email
|
||
6 years ago
|
} else {
|
||
6 years ago
|
// Turns blue led off
|
||
6 years ago
|
digitalWrite(LOW_TEMP_LED_PIN, LOW);
|
||
6 years ago
|
}
|
||
|
if (lastTemperature > HIGH_TEMP_RELAY){
|
||
6 years ago
|
// Turns relay on
|
||
|
// this could start a fan
|
||
|
digitalWrite(RELAY_PIN, HIGH);
|
||
|
Serial.print(F("Warning: extremely high temperature. "));
|
||
|
Serial.println(F("Fan activated."));
|
||
6 years ago
|
} else {
|
||
6 years ago
|
// Turns relay off
|
||
|
// this could cause the fan to stop working
|
||
6 years ago
|
digitalWrite(RELAY_PIN, LOW);
|
||
|
}
|
||
|
|
||
|
if (temperatureReadingsCounter == BUFFER_SIZE) {
|
||
6 years ago
|
// Two minutes have passed by. At this point the desired number of readings has been reached.
|
||
|
// Calculates the new average temperature
|
||
|
averageTemp = calcAverageTempAndReset();
|
||
6 years ago
|
sendEmail(CASE_AVERAGE_TEMP, averageTemp);
|
||
6 years ago
|
|
||
|
// Updates the starting time for the display
|
||
|
displayTimeStart = millis();
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
float getNewTemp(){
|
||
6 years ago
|
// Reads temperature as Celsius
|
||
|
float temperature = dht.readTemperature();
|
||
6 years ago
|
|
||
|
// Checks if the read failed and handles the failure
|
||
6 years ago
|
if (isnan(temperature)) {
|
||
6 years ago
|
Serial.print(F("Failed to read from DHT sensor!"));
|
||
6 years ago
|
return -1;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Shifts all previous readings to the right and puts the new one at the beginning of the table
|
||
|
for (int i=BUFFER_SIZE-1; i>0; --i) {
|
||
6 years ago
|
temperatures[i] = temperatures[i-1];
|
||
|
}
|
||
6 years ago
|
temperatures[0] = temperature;
|
||
|
|
||
|
// Updates the readings counter
|
||
6 years ago
|
++temperatureReadingsCounter;
|
||
|
|
||
6 years ago
|
return temperature;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
float calcAverageTempAndReset() {
|
||
|
float sum = 0;
|
||
6 years ago
|
for (int i = 0; i<BUFFER_SIZE; i++){
|
||
6 years ago
|
sum += temperatures[i];
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Resets the readings counter
|
||
|
temperatureReadingsCounter = 0;
|
||
|
|
||
6 years ago
|
// TODO send email -> emailAverageTemp(averageTemp)
|
||
|
|
||
6 years ago
|
return sum/BUFFER_SIZE;
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
float getDistance() {
|
||
|
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
|
||
|
// Gives a short LOW pulse beforehand to ensure a clean HIGH pulse:
|
||
|
digitalWrite(DIST_TRIG_PIN, LOW);
|
||
|
delayMicroseconds(5);
|
||
|
digitalWrite(DIST_TRIG_PIN, HIGH);
|
||
|
delayMicroseconds(10);
|
||
|
digitalWrite(DIST_TRIG_PIN, LOW);
|
||
6 years ago
|
|
||
6 years ago
|
// Reads the signal from the sensor: a HIGH pulse whose
|
||
|
// duration is the time (in microseconds) from the sending
|
||
|
// of the ping to the reception of its echo off of an object.
|
||
|
pinMode(DIST_ECHO_PIN, INPUT);
|
||
|
unsigned long duration = pulseIn(DIST_ECHO_PIN, HIGH);
|
||
|
|
||
|
// Converts the time into a distance
|
||
|
float distance = (duration/2) / 29.1;
|
||
|
return distance;
|
||
|
}
|
||
6 years ago
|
|
||
|
void sendEmail(int _case, float averageTemp) {
|
||
|
if (_case == CASE_HIGH_TEMP){
|
||
|
Serial.print("EMAIL : The temperature is above ");
|
||
|
Serial.print(HIGH_TEMP);
|
||
|
Serial.println("°C ");
|
||
|
} else if (_case == CASE_LOW_TEMP){
|
||
|
Serial.print("EMAIL : The temperature is below ");
|
||
|
Serial.print(LOW_TEMP);
|
||
|
Serial.println("°C ");
|
||
|
} else if (_case == CASE_AVERAGE_TEMP){
|
||
|
Serial.print("EMAIL : The average temperature is ");
|
||
|
Serial.print(averageTemp);
|
||
|
Serial.println("°C ");
|
||
|
}
|
||
|
}
|