Prevent LEDs from blinking during serial communication

I am trying to build a weight automation system using load cell. It is connected to the arduino via another circuit which uses serial communication.

First, I obtained the weight value from the received packet as String then converted it to float.

I then used the float number to create conditions.

I have four LEDs which are connected to pin4-pin7.
pin 4 LED lights up between 1-2kg.
pin 5 LED lights up between 2-3kg
and so on…

My problem is that the LEDs are blinking each time a packet is received. A packet is received every 1 second. How do i keep for example pin 4 LED ON while there is an object that is between 1-2kg on top of my load cell? Any hardware or software solutions will do but I prefer to have this solved by just altering the code.

String readString;
int count=0;
float weight;
int a=4;
int b=5;
int c=6;
int d=7;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
  pinMode(a,OUTPUT);
  pinMode(b,OUTPUT);
  pinMode(c,OUTPUT);
  pinMode(d,OUTPUT);
  
}

void loop() {

//Obtain weight

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    if(count>=2 && count<=7){ readString += c; } ///makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
    count++;
  }
 
  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
  }

  weight=readString.toFloat();

//Conditional Statements

  if(weight>=01.000 && weight<02.000) { digitalWrite(a,HIGH); }
  
  else if (weight>=02.000 && weight<03.000){
    digitalWrite(b,HIGH);
  }
  
  else if (weight>=03.000 && weight<04.000){
    digitalWrite(b,LOW);
    digitalWrite(c,HIGH);
  }

  else if(weight>=04.000 && weight<05.000) {
    digitalWrite(c,LOW);
    digitalWrite(d,HIGH);
  }
  
  else if(weight>=05.000) {
    digitalWrite(d,LOW);
  }
  else  {
    digitalWrite(a,LOW);
    digitalWrite(b,LOW);
    digitalWrite(c,LOW);
    digitalWrite(d,LOW);
  }

// Reset Values
readString="";
count=0;
}
    Serial.println(readString);  //so you can see the captured String

What does this show on serial monitor while the LEDs are blinking? Copy & paste the output into code tags for us.

it shows the weight of the object on top of the load cell

It is that last else clause that turns everything off and is causing the blinking. Remove it and restructure your code to turn off the specific LED when the if faile. Do not use a string of if .. else if.. else if,
Just use a simple if else for each weight case.

@grumpy_mike
i did what you said and the LEDs are not blinking anymore, however,
the LEDs do not turn off after I remove all the weight. If I add another “if” that turns off all the LEDs when weight<1kg, the blinking goes back.

From what I understand my problem is because the weight does not stay at its current value. Its like “weight value” then “zero” then “weight value” then “zero”.

PROBLEM SOLVED
I moved the readString=""; before the conditional statements.
I’m not really sure how that worked, anyone cares to explain why?
Thank you for all the help :slight_smile:

Show all the code please :slight_smile: We're not psychic. Please do it in code tags this time. See How to use the forum.