#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.
#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 )
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.
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)
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