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.
 
 

128 lines
3.6 KiB

/*
* TODO: write something here
*/
#include "todoFindCoolNameForProject.h"
/* ===== SETUP AND LOOP ===== */
void setup() {
// Starts the serial com with PC
Serial.begin(9600);
Serial.println("Running setup function");
// Other initializations
dht.begin();
temperatureReadingsCounter = 0;
pinMode(LOW_TEMP_LED_PIN, OUTPUT);
pinMode(HIGH_TEMP_LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
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);
}
void loop() {
// Gets new readings
getNewSamples();
Serial.println(temperatureReadingsCounter);
bool getAnotherSample = true;
while ((millis() - displayTimeStart) < TEMPERATURE_DISPLAY_DURATION) {
// Should display the average temperature for ten seconds
sevseg.setNumber(averageTemp, 1);
sevseg.refreshDisplay();
if ((getAnotherSample && millis() - displayTimeStart) >= (TEMPERATURE_DISPLAY_DURATION/2)){
getNewSamples();
getAnotherSample = false;
}
}
sevseg.blank();
sevseg.refreshDisplay();
// Waits a few seconds between measurements.
delay(SLEEP_INTERVAL);
}
/* ===== FUNCTION IMPLEMENTATIONS ===== */
void getNewSamples() {
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) {
// 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();
}
}
float getNewTemp(){
// Reads temperature as Celsius
float temperature = dht.readTemperature();
// Checks if the read failed and handles the failure
if (isnan(temperature)) {
Serial.print(F("Failed to read from DHT sensor!"));
return -1;
}
// 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) {
temperatures[i] = temperatures[i-1];
}
temperatures[0] = temperature;
// Updates the readings counter
++temperatureReadingsCounter;
Serial.print(F("Temperature: "));
Serial.print(temperature);
Serial.println(F("°C "));
return temperature;
}
float calcAverageTempAndReset() {
float sum = 0;
for (int i = 0; i<BUFFER_SIZE; i++){
sum += temperatures[i];
}
// Resets the readings counter
temperatureReadingsCounter = 0;
return sum/BUFFER_SIZE;
}