Do not want to show P/F/N every second

#include <SoftwareSerial.h>

#include <LiquidCrystal.h>


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

SoftwareSerial mySerial(2, 3); // RX, TX   <------<<<< use the pins you want



void setup()
{
  Serial.begin(115200);
  mySerial.begin(4800);
  
  lcd.begin(16, 2);
  lcd.setCursor (0, 0);
  lcd.write("Product name");
  
  
  
}

void loop()
{
  
  if  (mySerial.available())
  {
    lcd.setCursor(0, 1);
    

    float value = mySerial.parseFloat();
    Serial.print(value);
    lcd.print(value);

    char suffix[8];

       while (mySerial.available() == 0) ; // Wait for a character to arrive
    suffix[0] = mySerial.read();  // First blank

    while (mySerial.available() == 0) ; // Wait for a character to arrive
    suffix[1] = mySerial.read();  // Second blank

    while (mySerial.available() == 0) ; // Wait for a character to arrive
    suffix[2] = '\0' ; // Units

    suffix[3] = '\0'; // Null terminator

    Serial.println(suffix);
    lcd.print(suffix);

    // Read until you reach the Linefeed/Newline
    while (mySerial.read() != '\n') ;
  
    

    if( value > 1 && value < 4)
    {
      lcd.setCursor(14,0);
      lcd.write('P');
     
    }
    else if ( value >0.5 )
    {
      lcd.setCursor(14,0);
      lcd.write('F');
    }

      else {
        lcd.setCursor(14,0);
      lcd.write('N'); }

      
    }
  }

In the above code, the serial data from my weighing scale shows correctly. Also, It is showing Pass/Fail/Nothing correctly. However, it is showing this continuously. But, I want the weight to settle for a few seconds ( or until 5 consecutive values are equal) and then show P/F/N. How can I achieve this? I want the weight data to be continuous but the P/F/N to only be after this certain time.

keep the last 5 values into an (rotating) array and check if your stability condition is met before you display

Im sorry I am new so I dont understand how to do this. Can you provide some more help

study something like this

#include <limits.h>

const  byte valuesCount = 5;
int values[valuesCount];
byte nextValueIndex = 0;
bool arrayWasFilled = false;

void getValue() {
  values[nextValueIndex] = random(0, 101); // read your data
  if (++nextValueIndex >= valuesCount) {
    arrayWasFilled = true;
    nextValueIndex = 0;
  }
}

bool isValueStable(float& averageValue ) {

  if (!arrayWasFilled) return false; // we did not get enough data samples yet

  // perform your test, here I find the min and max and see if the difference is less than 10
  int minValue = INT_MAX, maxValue = INT_MIN;
  averageValue = 0;
  for (byte i = 0; i < valuesCount; i++) {
    averageValue += values[i];
    if (values[i] < minValue) minValue = values[i];
    if (values[i] > maxValue) maxValue = values[i];
  }
  averageValue /= valuesCount;
  return abs(maxValue - minValue) < 10; // whatever is your rule
}


void setup() {
  Serial.begin(115200);
}

void loop() {
  float avg;
  getValue();
  if (isValueStable(avg)) {
    Serial.print(F("Value is stable : "));
    Serial.println(avg);
  }
}

should be self explanatory
(typed here, untested. so may be there are some typos left as an exercise to fix :wink: )

The data comes in float so I get this error:

incompatible types in assignment of 'float' to 'float [5]'

OR

incompatible types in assignment of 'float' to 'int[5]' in your code's case

Actually I dont mind hard coding it as well. I just want to wait for 5 seconds before a decision is made. Doesnt matter if there are 5 consecutive readings.

change the array to float
float values[valuesCount];

post your code for more info.

Yeah I did that, I get the following error:

incompatible types in assignment of 'float' to 'float [5]'

#include <SoftwareSerial.h>
#include <limits.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
SoftwareSerial mySerial(2, 3); // RX, TX   <------<<<< use the pins you want

const  byte valuesCount = 5;
float values[valuesCount];
byte nextValueIndex = 0;
bool arrayWasFilled = false;



void setup()
{
  Serial.begin(115200);
  mySerial.begin(4800);
  
  lcd.begin(16, 2);
  lcd.setCursor (0, 0);
  lcd.write("Product name");
  
  
  
}

void loop()
{
  
  if  (mySerial.available())
  {
    lcd.setCursor(0, 1);
    

     values = mySerial.parseFloat();
    Serial.print(value);
    lcd.print(value);

    char suffix[8];

       while (mySerial.available() == 0) ; // Wait for a character to arrive
    suffix[0] = mySerial.read();  // First blank

    while (mySerial.available() == 0) ; // Wait for a character to arrive
    suffix[1] = mySerial.read();  // Second blank

    while (mySerial.available() == 0) ; // Wait for a character to arrive
    suffix[2] = '\0' ; // Units

    suffix[3] = '\0'; // Null terminator

    Serial.println(suffix);
    lcd.print(suffix);

    // Read until you reach the Linefeed/Newline
    while (mySerial.read() != '\n') ;

    
  
    

    if( value > 1 && value < 4 )
    {
      lcd.setCursor(14,0);
      lcd.write('P');

      
      
     
    }
    else if ( value >0.5 )
    {
      lcd.setCursor(14,0);
      lcd.write('F');
    }

      else {
      lcd.setCursor(14,0);
      lcd.write('N'); }

      
    }
  }

Can you just tell me where I need to insert a delay or timer so that the P/F/N comes 5 seconds after a product is on the weighing scale ( I will identify this by checking if the weight is greater than half the min weight and if it is, the timer will start)

Create a function. LCD()

Move all the print statements there.
Read the serial as often as you like.

When ready,
Set the timer
After 5 seconds, call the print function.

values is an array, you can't assign a float to the array, only to one entry in the array. You would have to go with what was in my getValue() function

  if  (mySerial.available()) {
    lcd.setCursor(0, 1);
    values[nextValueIndex] = mySerial.parseFloat();
    if (++nextValueIndex >= valuesCount) {
      arrayWasFilled = true;
      nextValueIndex = 0;
    }
    Serial.print(values[nextValueIndex]);
    lcd.print(values[nextValueIndex]);

note that your handling of the Serial communication is pretty weak. I would suggest to study Serial Input Basics to handle this

You don't want to use "delay()". During delay, NOTHING else happens.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.