Semester assignment for the course "Microprocessors and Peripherals" of THMMY in AUTH university.
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.

153 lines
4.3 KiB

6 years ago
/*
* TODO: write something here
*/
6 years ago
#include "todoFindCoolNameForProject.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
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
if (!lastReading || millis() - lastReading > SLEEP_INTERVAL) {
getNewSamples();
Serial.println(temperatureReadingsCounter);
lastReading = millis();
}
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();
} else {
sevseg.blank();
sevseg.refreshDisplay();
6 years ago
}
// Waits a few seconds between measurements.
//delay(SLEEP_INTERVAL);
6 years ago
}
/* ===== FUNCTION IMPLEMENTATIONS ===== */
void getNewSamples() {
6 years ago
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) {
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();
Serial.print("Average temperature: ");
Serial.println(averageTemp);
// 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;
Serial.print(F("Temperature: "));
6 years ago
Serial.print(temperature);
6 years ago
Serial.println(F("°C "));
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;
return sum/BUFFER_SIZE;
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);
// 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;
}