fix weight string vanishing

This commit is contained in:
interfisch 2023-04-22 15:15:08 +02:00
parent 75b720f564
commit 03b9779198
2 changed files with 15 additions and 11 deletions

View File

@ -13,7 +13,7 @@ platform = atmelavr
board = nanoatmega328 board = nanoatmega328
framework = arduino framework = arduino
monitor_speed = 9600 monitor_speed = 115200
lib_deps= lib_deps=
bogde/HX711@^0.7.5 bogde/HX711@^0.7.5

View File

@ -104,7 +104,7 @@ unsigned long time_adcwait=1000/spsadc;
int adc_readings=1; //ca. 700ms for 5 readings, ca 75ms for 1 reading, readings=1 10times = 747ms int adc_readings=1; //ca. 700ms for 5 readings, ca 75ms for 1 reading, readings=1 10times = 747ms
double weight=0; double weight=0;
#define ADCMEDIANVALUES_MAX 31 //needs to be uneven, maximum number, for array declaration #define ADCMEDIANVALUES_MAX 93 //needs to be uneven, maximum number, for array declaration
float weightseries[ADCMEDIANVALUES_MAX]; //last n values for median filter float weightseries[ADCMEDIANVALUES_MAX]; //last n values for median filter
uint16_t adcmedianvalues=DEFAULT_ADCMEDIANVALUES; //needs to be uneven //eeprom 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. 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.
@ -190,7 +190,7 @@ void updateLCD();
void setup() { void setup() {
Serial.begin(9600); Serial.begin(115200);
pinMode(PIN_LED, OUTPUT); pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, HIGH); digitalWrite(PIN_LED, HIGH);
@ -300,7 +300,7 @@ void loop() {
currentreading_pos++; currentreading_pos++;
currentreading_pos%=N_CURRENTREADINGS; currentreading_pos%=N_CURRENTREADINGS;
double showweight=getWeightFiltered();
switch(state){ switch(state){
case S_SCALEDISPLAY: case S_SCALEDISPLAY:
if ((getWeightSeriesMax()-getWeightSeriesMin())<LCD_BACKLIGHT_WEIGHT){ if ((getWeightSeriesMax()-getWeightSeriesMin())<LCD_BACKLIGHT_WEIGHT){
@ -315,7 +315,9 @@ void loop() {
lcd0=toStringBar(weight,0,1000); lcd0=toStringBar(weight,0,1000);
//lcd1=toWeightString(weight)+"g"; //lcd1=toWeightString(weight)+"g";
//lcd1=toWeightString(getWeightMedian())+"g"; //lcd1=toWeightString(getWeightMedian())+"g";
lcd1=toWeightString(getWeightFiltered())+"g";
lcd1=toWeightString(showweight)+"g";
Serial.print(showweight,3); Serial.println("g");
//___ //___
if (btn_back_press==2){ //press BACK to tare if (btn_back_press==2){ //press BACK to tare
@ -569,7 +571,8 @@ void loop() {
if (millis() >= time_lastadc+time_adcwait) if (millis() >= time_lastadc+time_adcwait)
{ {
weight=scale.get_units(adc_readings); weight=scale.get_units(adc_readings);
adcmedianposition++; if (adcmedianposition>=adcmedianvalues) adcmedianposition=0; adcmedianposition++;
adcmedianposition%=adcmedianvalues;
weightseries[adcmedianposition]=weight; //save weight to series for medianfilter weightseries[adcmedianposition]=weight; //save weight to series for medianfilter
looptimeadc_margin=millis()-time_lastadc-time_adcwait; looptimeadc_margin=millis()-time_lastadc-time_adcwait;
@ -996,15 +999,16 @@ float getWeightSeriesMax()
} }
String toWeightString(double w){ String toWeightString(double w){
return toWeightString(w,5,4); return toWeightString(w,2,6);
} }
String toWeightString(double w,uint8_t dec,uint8_t len){ String toWeightString(double w,uint8_t dec,uint8_t len){
char outstring[16]; char outstring[16];
char vz; char vz;
if(w<0) if(w<0) {
vz='-'; vz='-';
else }else{
vz='+'; vz='+';
}
dtostrf(abs(w),len,dec,outstring); dtostrf(abs(w),len,dec,outstring);
return String(vz)+""+String(outstring); return String(vz)+""+String(outstring);
} }