From b7144ce50eda822da2e4be84790c1d4d94147dc4 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Fri, 31 May 2019 19:05:46 +0300 Subject: [PATCH] Code cleanup --- .../todoFindCoolNameForProject.h | 56 ++++++++++ .../todoFindCoolNameForProject.ino | 103 ++++++++++++------ 2 files changed, 123 insertions(+), 36 deletions(-) create mode 100644 todoFindCoolNameForProject/todoFindCoolNameForProject.h diff --git a/todoFindCoolNameForProject/todoFindCoolNameForProject.h b/todoFindCoolNameForProject/todoFindCoolNameForProject.h new file mode 100644 index 0000000..9c7528e --- /dev/null +++ b/todoFindCoolNameForProject/todoFindCoolNameForProject.h @@ -0,0 +1,56 @@ +#ifndef TODO_FIND_COOL_NAME_FOR_PROJECT_H +#define TODO_FIND_COOL_NAME_FOR_PROJECT_H + +/* ===== INCLUDES ===== */ +#include +#include + +/* ===== DEFINITIONS ===== */ +// Type of DHT sensor used is 11 +#define DHTTYPE DHT11 +// Number of readings to average +#define BUFFER_SIZE 24 +// Interval between readings in milliseconds +#define SLEEP_INTERVAL 1000 +// Duration of average temperature display +#define TEMPERATURE_DISPLAY_DURATION 2000 +// Number of digits on the display +#define NUMBER_OF_DIGITS 4 +// Pin layout +#define DHTPIN 2 +#define LOW_TEMP_LED_PIN 3 +#define HIGH_TEMP_LED_PIN 4 +#define RELAY_PIN 5 +#define DIGIT_1_PIN 14 +#define DIGIT_2_PIN 17 +#define DIGIT_3_PIN 18 +#define DIGIT_4_PIN 12 +#define SEGMENT_A_PIN 15 +#define SEGMENT_B_PIN 19 +#define SEGMENT_C_PIN 7 +#define SEGMENT_D_PIN 9 +#define SEGMENT_E_PIN 6 +#define SEGMENT_F_PIN 16 +#define SEGMENT_G_PIN 8 +#define DECIMAL_POINT_PIN 13 +// Temperature thresholds +#define LOW_TEMP 28 +#define HIGH_TEMP 28 + + +/* ===== GLOBAL VARIABLES AND INITIALIZATIONS ===== */ +// Initializes DHT sensor +DHT dht(DHTPIN, DHTTYPE); +// Instantiates a seven segment object +SevSeg sevseg; +int temperatureReadingsCounter; +float temperatures[BUFFER_SIZE]; +float averageTemp = 0; +unsigned long displayTimeStart = 0; + +/* ===== FUNCTION DEFINITIONS ===== */ +void getNewSamples(); +float getNewTemp(); +float calcAverageTempAndReset(); + +#endif //TODO_FIND_COOL_NAME_FOR_PROJECT_H diff --git a/todoFindCoolNameForProject/todoFindCoolNameForProject.ino b/todoFindCoolNameForProject/todoFindCoolNameForProject.ino index 772ea24..7a7e480 100644 --- a/todoFindCoolNameForProject/todoFindCoolNameForProject.ino +++ b/todoFindCoolNameForProject/todoFindCoolNameForProject.ino @@ -1,37 +1,60 @@ /* * TODO: write something here */ -#include -#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]; +#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) { @@ -55,43 +78,51 @@ void loop() { } if (temperatureReadingsCounter == BUFFER_SIZE) { - float avgTemp = calcAverageTempAndReset(temperatures); - Serial.println(avgTemp); - temperatureReadingsCounter = 0; + // 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(); } - // Wait a few seconds between measurements. - Serial.println(temperatureReadingsCounter); - delay(1000); } float getNewTemp(){ - // Reads temperature as Celsius (the default) - float t = dht.readTemperature(); + // Reads temperature as Celsius + float temperature = dht.readTemperature(); // Checks if the read failed and handles the failure - if (isnan(t)) { + if (isnan(temperature)) { Serial.print(F("Failed to read from DHT sensor!")); - return; + return -1; } - for (int i = BUFFER_SIZE - 1; i>0; i--) { + // 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] = t; + temperatures[0] = temperature; + + // Updates the readings counter ++temperatureReadingsCounter; Serial.print(F("Temperature: ")); - Serial.print(t); + Serial.print(temperature); Serial.println(F("°C ")); - return t; + return temperature; } -float calcAverageTempAndReset(float *array) { - float sum = 0 ; // sum will be larger than an item, float for safety. +float calcAverageTempAndReset() { + float sum = 0; for (int i = 0; i