diff --git a/platformio.ini b/platformio.ini index 0e7cd3a..f378aa9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,4 +18,5 @@ monitor_speed = 9600 lib_deps= bogde/HX711@^0.7.5 marcoschwartz/LiquidCrystal_I2C@^1.1.4 - https://github.com/arduino-libraries/Servo \ No newline at end of file + https://github.com/arduino-libraries/Servo + https://github.com/emilv/ArduinoSort/ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2fd76d4..9928802 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #define EEPROMVERSION 1 @@ -82,10 +83,10 @@ String receivedString =""; char tempstring[16]; //for dtostrf -long time_lastlcd=0; -long looptimelcd_margin=0; //if >0 loop took longer than expected +unsigned long time_lastlcd=0; +unsigned long looptimelcd_margin=0; //if >0 loop took longer than expected uint8_t fps=DEFAULT_FPS; //EEPROM -long time_lcdwait=1000/fps; +unsigned long time_lcdwait=1000/fps; String lcd0=""; //lcd line 0 String lcd1=""; // " 1 #define LCD_BACKLIGHT_TIME 20000 @@ -96,17 +97,18 @@ uint32_t lcd_backlight_offtimer=0; String lcd0_buffer=""; String lcd1_buffer=""; -long time_lastadc=0; -long looptimeadc_margin=0; +unsigned long time_lastadc=0; +unsigned long looptimeadc_margin=0; uint8_t spsadc=DEFAULT_SPSADC; //eeprom -long time_adcwait=1000/spsadc; +unsigned long time_adcwait=1000/spsadc; int adc_readings=1; //ca. 700ms for 5 readings, ca 75ms for 1 reading, readings=1 10times = 747ms double weight=0; #define ADCMEDIANVALUES_MAX 31 //needs to be uneven, maximum number, for array declaration -double weightseries[ADCMEDIANVALUES_MAX]; //last n values for median filter -int adcmedianvalues=DEFAULT_ADCMEDIANVALUES; //needs to be uneven //eeprom -int adcmedianposition=0; +float weightseries[ADCMEDIANVALUES_MAX]; //last n values for median filter +uint16_t adcmedianvalues=DEFAULT_ADCMEDIANVALUES; //needs to be uneven //eeprom +float adcFilterKeepMedianvaluesFactor=0.8; //how many lowest and highest values will be removed from the array before avaraging (as factor). 0.0 means only median is used. 1.0 means all values are used. +uint16_t adcmedianposition=0; float calibrationWeight=100; //gramms @@ -170,11 +172,12 @@ String toString(double w,uint8_t dec); String toString(double w); String toWeightString(double w,uint8_t dec,uint8_t len); String toWeightString(double w); -double getWeightSeriesMax(); -double getWeightSeriesMin(); +float getWeightSeriesMax(); +float getWeightSeriesMin(); double getWeightMedian(); -double getVoltage(); -double getCurrent(); +double getWeightFiltered(); +float getVoltage(); +float getCurrent(); String menuentry_string(); void menuentry_set(uint8_t presstype); void menuentry_back(uint8_t presstype); @@ -311,7 +314,8 @@ void loop() { } lcd0=toStringBar(weight,0,1000); //lcd1=toWeightString(weight)+"g"; - lcd1=toWeightString(getWeightMedian())+"g"; + //lcd1=toWeightString(getWeightMedian())+"g"; + lcd1=toWeightString(getWeightFiltered())+"g"; //___ if (btn_back_press==2){ //press BACK to tare @@ -883,7 +887,7 @@ String menuentry_string(){ //second line string if entry selected return s; } -double getCurrent() +float getCurrent() { uint16_t _current=0; for (uint8_t i=0;i2 && _valuesLeft>(uint16_t)(adcmedianvalues*adcFilterKeepMedianvaluesFactor)) + { + cmin=MAXDOUBLEVALUE; + cmax=-MAXDOUBLEVALUE-1; + for (uint16_t i=0;icmax){ + cmax=weightseries[i]; + cmax_i=i; + } + } + + } + mask[cmin_i]=true; + mask[cmax_i]=true; + _valuesLeft-=2; //2 values have been masked out + } + + + + double sum=0; + uint16_t meanvalues=0; + for (uint16_t i=0;icmax) cmax=weightseries[i]; } @@ -1087,7 +1136,7 @@ uint16_t EEPROMReadInt(uint8_t paddr){ void EEPROMWriteDouble(int ee, double value) { byte* p = (byte*)(void*)&value; - for (int i = 0; i < sizeof(value); i++) + for (uint16_t i = 0; i < sizeof(value); i++) EEPROM.write(ee++, *p++); } @@ -1095,7 +1144,7 @@ double EEPROMReadDouble(int ee) { double value = 0.0; byte* p = (byte*)(void*)&value; - for (int i = 0; i < sizeof(value); i++) + for (uint16_t i = 0; i < sizeof(value); i++) *p++ = EEPROM.read(ee++); return value; }