//CONSTANTS #define THERM_PIN 36 #define NOMINAL_TEMPERATURE 25 #define BCOEFFICIENT 4300 #define VI 3.3 #define CALI_CONST -11 #define R1 10000 //GLOBALS// float temp; float maxVal = -9999; float minVal = 9999; int totalAve; float steinhart; float vo; float r2; int adc; float pastTenMin[10*60] = {}; int countTen; float allAves[10*6*24] = {}; int countAll; bool toggleinfo = 0; //UTILITY FUNCTIONS// //converts units float celsiusToFahrenheit(float tempCelsius){ return (tempCelsius * 9.0/5.0) + 32.0; } //appends a value to the end of a list: {1,2,3, , } --> {1,2,3, appendedvalue, } void appendArr(int* myArr, int lenArr, int value){ myArr[lenArr] = value; } //calculates average numerical value of an array of nums float calculateAveInt(int arr[], int size) { float sum = 0; for (int i = 0; i != size; i+=1) { sum += arr[i]; } float ave = sum / size; return ave; } float calculateAveFloat(float arr[], int size) { float sum = 0; for (int i = 0; i != size; i+=1) { sum += arr[i]; } float ave = sum / size; return ave; } //returns bool if 2 strings are the same or not bool compareStr(const char* strA, const char* strB) { int lenA = 0; int lenB = 0; while (strA[lenA] != 0) { lenA += 1; } while (strB[lenB] != 0) { lenB += 1; } if (lenA != lenB) { return false; } for (int i = 0; i < lenA; i++) { if (strA[i] != strB[i]) { return false; } } return true; } //uses millis() function to create our own delay function void delayFunction(int delay){ unsigned long startTime = millis(); unsigned long currentTime = startTime; while(currentTime - startTime <= delay){ //wait here currentTime = millis(); } } //OTHER FUNCTIONS// //checks if we have a new maxVal and minVal which is even lower/higher void updateExtrema(){ if (temp > maxVal){ maxVal = temp; } if (temp < minVal){ minVal = temp; } } //updates 10 minValute average chunk and record our 10 minValute averages in another array void updateAves(){ pastTenMin[countTen] = temp; if (countTen == 599){ //we have all of the data from this chunk of 10 minutes, lets compute the average allAves[countAll] = calculateAveFloat(pastTenMin, countTen); //now clear this chunk as we are on to the next average countTen = 0; //add to count of num of averages countAll += 1; } } //Get and check our sensor data for this instance float getTemp(){ adc = analogRead(THERM_PIN); vo = VI * (adc / 4095.0); r2 = 10000/(4095.0/adc)-1; steinhart = r2 / R1; // (R/Ro) steinhart = log(steinhart); // ln(R/Ro) steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro) steinhart += 1.0 / (NOMINAL_TEMPERATURE + 273.15); // + (1/To) steinhart = 1.0 / steinhart; // Invert steinhart -= 273.15; // convert to C steinhart += CALI_CONST; //calibrate using offset temp = celsiusToFahrenheit(steinhart); return temp; } int commandGetAll(){ Serial.println("\n---------------------------\nGetting all 10 min averages.."); if (countAll == 0){ Serial.println("none"); return 0; } for (int i = 0; i != countAll + 1; i+=1){ if (toggleinfo){ Serial.print(allAves[i]); Serial.println(" Degrees F"); } else{ Serial.print(allAves[i]); } } Serial.println("Done\n\n---------------------------\n"); delayFunction(5000); return 1; } void commandGetStats(){ Serial.print("\n---------------------------\n"); Serial.print("Min: "); Serial.print(minVal); Serial.println(" Degrees F"); Serial.print("maxVal: "); Serial.print(maxVal); Serial.println(" Degrees F"); Serial.print("Entire Average: "); if (countAll != 0){ Serial.print( calculateAveFloat(allAves,countAll)); } else{ Serial.print( calculateAveFloat(pastTenMin,countTen)); } Serial.println(" Degrees F"); Serial.print("\nDone\n---------------------------\n"); delayFunction(5000); } void setup() { Serial.begin(115200); } void loop() { char user[10] = {0,0,0,0,0,0,0,0,0,0}; char zeroArr[10] = {0,0,0,0,0,0,0,0,0,0}; while(1){ delayFunction(1000); temp = getTemp(); updateExtrema(); updateAves(); if (toggleinfo){ Serial.print(millis()); Serial.print(" ms "); Serial.print(temp); Serial.println(" Degrees F"); } else{ Serial.println(temp); } //get user input for commands if (Serial.available()) { String receivedString = Serial.readString(); receivedString.trim(); // This line is added to remove any newline characters. receivedString.toCharArray(user, 10); for (int i = 0; user[i] != 0; i += 1) { Serial.print(user[i]); } } //find proper command demanded by user if (compareStr(user,zeroArr)){ //do nothing } else if (compareStr(user,"getall")){ commandGetAll(); } else if (compareStr(user,"getstats")){ commandGetStats(); } else if (compareStr(user,"stop")){ Serial.println("\n---------------------------\nStopping data collection...\nDone\n---------------------------\n"); break; } else if (compareStr(user,"toginfo")){ if (toggleinfo){ toggleinfo = 0; } else{ toggleinfo = 1; } } else { Serial.print("\n******INVALID COMMAND******\nCommands are: getstats, stop, toginfo & getall\n\n---------------------------\n"); } countTen += 1; //countAll += 1; for(int i = 0; i != 10; i += 1){ user[i] = 0; } } while(1){ //stall } }