Components and supplies
Temperature Sensor
Jumper wires (generic)
RGB Backlight LCD - 16x2
Arduino UNO
Breadboard (generic)
Resistor 221 ohm
Single Turn Potentiometer- 10k ohms
Project description
Code
AvgStd.ino
c_cpp
main file.
1#include "AvgStd.h" 2#include <LiquidCrystal.h> 3 4LiquidCrystal lcd(12,11,5,4,3,2); 5const int sensorPin = A0; 6AvgStd mySamples; 7 8void setup(){ 9 mySamples = AvgStd(); 10 mySamples.setRejectionSigma(7.0); 11 Serial.begin(9600); 12 while (!Serial) { 13 ; // wait for serial port to connect. Needed for native USB port only 14 } 15 16 // send an intro: 17 Serial.println("\ 18\ 19AvgStd:"); 20 Serial.println(); 21 lcd.begin(16, 2); 22 // Print a message to the LCD. 23 lcd.print("hello, world!"); 24 } 25 26void loop(){ 27 28 // read temperature 29 float T = readTemperature(); 30 mySamples.checkAndAddReading(T); 31 32 // print string 33 // 34 lcdPrint(T); 35 36 //if reset button pressed 37 // mySamples.reset(); 38 delay(1000); 39 } 40 41float readTemperature(){ 42 //read device 43 int v = analogRead(sensorPin); 44 // convert to V (ADC 1024 values, 5V max) 45 // float vr = ((float)v / 1024.0) * 5.0; 46 // T = ( v [V] - 0.5 V) * 100 [C] 47 return 100 * ((((float)v / 1024.0) * 5.0) - 0.5); 48 } 49 50void serialPrint(float T){ 51 Serial.print("T = " + String(T, 2)); 52 Serial.println(" N = " + String(mySamples.getN())); 53 Serial.print("avg: " + String(mySamples.getMean(),2)); 54 Serial.println(" +- " + String(mySamples.getStd(),2)); 55 Serial.print("min: " + String(mySamples.getMin(),2)); 56 Serial.println(" max: " + String(mySamples.getMax(),2)); 57 58 } 59 60void lcdPrint(float T){ 61 lcd.clear(); 62 // print first row with avg/std 63 lcd.setCursor(0,0); 64 String a = "T=" + String(mySamples.getMean(),2); 65 lcd.print(a); 66 lcd.setCursor(8,0); 67 a = "+- " + String(mySamples.getStd(),2); 68 lcd.print(a); 69 lcd.setCursor(15,0); 70 lcd.print( mySamples.getN()%2 == 0? "* " : " " ); 71 // second row alternates min/max with current reading 72 lcd.setCursor(0,1); 73 if ((mySamples.getN()/10)%2 == 0){ 74 //display avg+std 75 lcd.setCursor(0,1); 76 77 a = " " + String(T,2) + " N=" + String(mySamples.getN()); 78 lcd.print(a); 79 80 } else { 81 // display min/max 82 a = "m " + String(mySamples.getMin(),2); 83 lcd.print(a); 84 lcd.setCursor(9,1); 85 a = "M " + String(mySamples.getMax(),2); 86 lcd.print(a); 87 } 88 89} 90
AvgStd.cpp
c_cpp
implementation of simple statistics
1/** 2 * Library to calculate the average and standard deviation of a 3 4 * reading iteratively: without storing the whole data set. 5 * 6 * Stores: 7 8 * double average 9 * double variance 10 * int numberOfReadings 11 * double 12 min 13 * double max 14 **/ 15 16 /* 17Copyright (c) 2016 Edoardo Pasca 18 19Permission 20 is hereby granted, free of charge, to any person obtaining a copy of this software 21 and associated documentation files (the "Software"), to deal in the Software without 22 restriction, including without limitation the rights to use, copy, modify, merge, 23 publish, distribute, sublicense, and/or sell copies of the Software, and to permit 24 persons to whom the Software is furnished to do so, subject to the following conditions: 25 26The 27 above copyright notice and this permission notice shall be included in all copies 28 or substantial portions of the Software. 29 30THE SOFTWARE IS PROVIDED "AS IS", 31 WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 32 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 33 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES 34 OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 35 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 THE SOFTWARE. 37 */ 38 39#include "AvgStd.h" 40#include <math.h> 41 42 43AvgStd::AvgStd(){ 44 45 AvgStd::reset(); 46} 47 48void AvgStd::checkAndAddReading(float val){ 49 50 51 if (N < 10) 52 AvgStd::addReading(val); 53 else { 54 if 55 (r_sigma == -1) 56 AvgStd::addReading(val); 57 else { 58 if 59 (abs(avg - val) <= (r_sigma * sqrt(var)) ) 60 AvgStd::addReading(val); 61 62 } 63 } 64 65} 66 67void AvgStd::addReading(float val){ 68 69 70 if (N == 0) { 71 avg = val; 72 min = val; 73 max = val; 74 75 } else if (N == 1) { 76 //set min/max 77 max = val > max? val 78 : max; 79 min = val < min? val : min; 80 81 82 83 float thisavg = (avg + val)/2; 84 // initial value is in avg 85 86 var = (avg - thisavg)*(avg-thisavg) + (val - thisavg) * (val-thisavg); 87 88 avg = thisavg; 89 } else { 90 // set min/max 91 max 92 = val > max? val : max; 93 min = val < min? val : min; 94 95 96 float M = (float)N; 97 98 var = var * ((M-1)/M) + ((val - avg)*(val 99 - avg)/(M+1)) ; 100 101 avg = avg * (M/(M+1)) + val / (M+1); 102 103 } 104 N++; 105} 106 107 108 109void AvgStd::reset(){ 110 N = 0; 111 avg 112 = 0; 113 var = 0; 114 min = 0; 115 max = 0; 116 r_sigma = -1; 117} 118 119float 120 AvgStd::getMean(){return avg;} 121float AvgStd::getStd() { 122 float ret = -1; 123 124 if (N>1) 125 ret = sqrt(var); 126 return ret; 127} 128float AvgStd::getVariance() 129 {return var;} 130unsigned int AvgStd::getN(){return N;} 131float AvgStd::getMin() 132 {return min;} 133float AvgStd::getMax() {return max;} 134 135void AvgStd::setRejectionSigma(float 136 sigmas){ 137 r_sigma = sigmas; 138}; 139 140
AvgStd.ino
c_cpp
main file.
1#include "AvgStd.h" 2#include <LiquidCrystal.h> 3 4LiquidCrystal lcd(12,11,5,4,3,2); 5const int sensorPin = A0; 6AvgStd mySamples; 7 8void setup(){ 9 mySamples = AvgStd(); 10 mySamples.setRejectionSigma(7.0); 11 Serial.begin(9600); 12 while (!Serial) { 13 ; // wait for serial port to connect. Needed for native USB port only 14 } 15 16 // send an intro: 17 Serial.println("\ 18\ 19AvgStd:"); 20 Serial.println(); 21 lcd.begin(16, 2); 22 // Print a message to the LCD. 23 lcd.print("hello, world!"); 24 } 25 26void loop(){ 27 28 // read temperature 29 float T = readTemperature(); 30 mySamples.checkAndAddReading(T); 31 32 // print string 33 // 34 lcdPrint(T); 35 36 //if reset button pressed 37 // mySamples.reset(); 38 delay(1000); 39 } 40 41float readTemperature(){ 42 //read device 43 int v = analogRead(sensorPin); 44 // convert to V (ADC 1024 values, 5V max) 45 // float vr = ((float)v / 1024.0) * 5.0; 46 // T = ( v [V] - 0.5 V) * 100 [C] 47 return 100 * ((((float)v / 1024.0) * 5.0) - 0.5); 48 } 49 50void serialPrint(float T){ 51 Serial.print("T = " + String(T, 2)); 52 Serial.println(" N = " + String(mySamples.getN())); 53 Serial.print("avg: " + String(mySamples.getMean(),2)); 54 Serial.println(" +- " + String(mySamples.getStd(),2)); 55 Serial.print("min: " + String(mySamples.getMin(),2)); 56 Serial.println(" max: " + String(mySamples.getMax(),2)); 57 58 } 59 60void lcdPrint(float T){ 61 lcd.clear(); 62 // print first row with avg/std 63 lcd.setCursor(0,0); 64 String a = "T=" + String(mySamples.getMean(),2); 65 lcd.print(a); 66 lcd.setCursor(8,0); 67 a = "+- " + String(mySamples.getStd(),2); 68 lcd.print(a); 69 lcd.setCursor(15,0); 70 lcd.print( mySamples.getN()%2 == 0? "* " : " " ); 71 // second row alternates min/max with current reading 72 lcd.setCursor(0,1); 73 if ((mySamples.getN()/10)%2 == 0){ 74 //display avg+std 75 lcd.setCursor(0,1); 76 77 a = " " + String(T,2) + " N=" + String(mySamples.getN()); 78 lcd.print(a); 79 80 } else { 81 // display min/max 82 a = "m " + String(mySamples.getMin(),2); 83 lcd.print(a); 84 lcd.setCursor(9,1); 85 a = "M " + String(mySamples.getMax(),2); 86 lcd.print(a); 87 } 88 89} 90
AvgStd.h
c_cpp
Header for the simple iterative statistic package
1/** 2 * Library to calculate the average and standard deviation of a 3 * reading iteratively: without storing the whole data set. 4 * 5 * Stores: 6 * double average 7 * double variance 8 * int numberOfReadings 9 * double min 10 * double max 11 **/ 12 13 /* 14Copyright (c) <year> <copyright holders> 15 16Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 17 18The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 19 20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #ifndef AVGSTD_H 24 #define AVGSTD_H 25 26 #include "Arduino.h" 27 28class AvgStd { 29 public: 30 float getMean(); 31 float getStd(); 32 float getMin(); 33 float getMax(); 34 float getVariance(); 35 unsigned int getN(); 36 void reset(); 37 void addReading(float); 38 void checkAndAddReading(float); 39 void setRejectionSigma(float); 40 int getTrend(); 41 void setTrendInterval(int); 42 void setSamplingInterval(int); 43 AvgStd(); 44 private: 45 float min, max, var, avg, r_sigma; 46 unsigned int N; 47 48 }; 49 50 #endif 51
AvgStd.cpp
c_cpp
implementation of simple statistics
1/** 2 * Library to calculate the average and standard deviation of a 3 * reading iteratively: without storing the whole data set. 4 * 5 * Stores: 6 * double average 7 * double variance 8 * int numberOfReadings 9 * double min 10 * double max 11 **/ 12 13 /* 14Copyright (c) 2016 Edoardo Pasca 15 16Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 17 18The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 19 20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23#include "AvgStd.h" 24#include <math.h> 25 26 27AvgStd::AvgStd(){ 28 AvgStd::reset(); 29} 30 31void AvgStd::checkAndAddReading(float val){ 32 33 if (N < 10) 34 AvgStd::addReading(val); 35 else { 36 if (r_sigma == -1) 37 AvgStd::addReading(val); 38 else { 39 if (abs(avg - val) <= (r_sigma * sqrt(var)) ) 40 AvgStd::addReading(val); 41 } 42 } 43 44} 45 46void AvgStd::addReading(float val){ 47 48 if (N == 0) { 49 avg = val; 50 min = val; 51 max = val; 52 } else if (N == 1) { 53 //set min/max 54 max = val > max? val : max; 55 min = val < min? val : min; 56 57 58 float thisavg = (avg + val)/2; 59 // initial value is in avg 60 var = (avg - thisavg)*(avg-thisavg) + (val - thisavg) * (val-thisavg); 61 avg = thisavg; 62 } else { 63 // set min/max 64 max = val > max? val : max; 65 min = val < min? val : min; 66 67 float M = (float)N; 68 69 var = var * ((M-1)/M) + ((val - avg)*(val - avg)/(M+1)) ; 70 71 avg = avg * (M/(M+1)) + val / (M+1); 72 } 73 N++; 74} 75 76 77 78void AvgStd::reset(){ 79 N = 0; 80 avg = 0; 81 var = 0; 82 min = 0; 83 max = 0; 84 r_sigma = -1; 85} 86 87float AvgStd::getMean(){return avg;} 88float AvgStd::getStd() { 89 float ret = -1; 90 if (N>1) 91 ret = sqrt(var); 92 return ret; 93} 94float AvgStd::getVariance() {return var;} 95unsigned int AvgStd::getN(){return N;} 96float AvgStd::getMin() {return min;} 97float AvgStd::getMax() {return max;} 98 99void AvgStd::setRejectionSigma(float sigmas){ 100 r_sigma = sigmas; 101}; 102 103
Downloadable files
Schematic
There is a Genuino board, a LCD 16x2 and a LMP36 temperature sensors and a lot of wires!
Schematic
Schematic
There is a Genuino board, a LCD 16x2 and a LMP36 temperature sensors and a lot of wires!
Schematic
Comments
Only logged in users can leave comments
paskino
0 Followers
•0 Projects
Table of contents
Intro
5
0