How to Separate 0.360 Part from "+000.360 Kg" [Serial Communication]?

I am using Weighing scale RS232 communication and reading values on serial port:

For my project I only need decimal part ie 000.360 not +000.360 Kg

Code used for communication:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4    ); 

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);

}


void loop()
{
    while (mySerial.available())
    {
      Serial.write(mySerial.read());  
      
    }

}

I would suggest that you look at the serial input basics thread. You could modify the Example 3 - Receive with start- and end-markers. Use + for the start marker and K for the end marker. The markers are discarded. The if you want to convert from ASCII text to a float data type, use the atof() function.

@AnshumanFauzdar
Why are you not at work/school ?

Here is a start;

#include <SoftwareSerial.h>

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

char myArray[12] = {"012345678AB"};

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}


void loop()
{
  //myArray[12]
  //012345678911  11 = newline
  //          01
  //+000.360 Kg_

  while (mySerial.available() > 11)
  {
    myArray[0] = mySerial.read();

    //find the first charater, +
    if (myArray[0] == '+')
    {
      //read the remainding characters
      for (byte i = 1; i < 12; i++)
      {
        myArray[i] = mySerial.read();
        delay(1);
      }

      // Okay, myArray[1]thru myArray[7] has the data needed

      //Test print the characters
      for (byte i = 1; i < 8; i++)
      {
        Serial.print(myArray[i]);
      }
      Serial.println();
    } //if

  } //while

} //END of loop()

larryd:
@AnshumanFauzdar
Why are you not at work/school ?

Here is a start;

#include <SoftwareSerial.h>

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

char myArray[12] = {"012345678AB"};

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  //myArray[12]
  //012345678911  11 = newline
  //          01
  //+000.360 Kg_

while (mySerial.available() > 11)
  {
    myArray[0] = mySerial.read();

//find the first charater, +
    if (myArray[0] == '+')
    {
      //read the remainding characters
      for (byte i = 1; i < 12; i++)
      {
        myArray[i] = mySerial.read();
        delay(1);
      }

// Okay, myArray[1]thru myArray[7] has the data needed

//Test print the characters
      for (byte i = 1; i < 8; i++)
      {
        Serial.print(myArray[i]);
      }
      Serial.println();
    } //if

} //while

} //END of loop()

I am doing BTech @larryd, skipped 3 days LOL
Will continue from monday!
Thanks for code @larryd, Only because of you my project will work smoothly!!!

BIG THANK YOU!!

groundFungus:
I would suggest that you look at the serial input basics thread. You could modify the Example 3 - Receive with start- and end-markers. Use + for the start marker and K for the end marker. The markers are discarded. The if you want to convert from ASCII text to a float data type, use the atof() function.

Hey I want to compare values with a predefined value, in code below how can I do this?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); 

char myArray[12] = {"012345678AB"};
#include<string.h>
#include <LiquidCrystal.h>
const int rs = 31, en = 30, d4 = 28, d5 = 26, d6 = 24, d7 = 22;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int relay = 9;
int pot2 = A2;
int button = 7;
int button2 = 6;
int button3 = 5;
int button4 = 4;
int button5 = 3;
int button6 = 2;

int potvalue2 = 0;
int buttonvalue = 1;
int buttonvalue2 = 1;
int buttonvalue3 = 1;
int buttonvalue4 = 1;
int buttonvalue5 = 1;
int buttonvalue6 = 1;


char weight = 1;
char maxvalue = 1;
int maxvalue2 = 1;
char valid[50] = "false";
char valid2[50] = "false";
char valid3[50] = "false";
char valid4[50] = "false";
char valid5[50] = "false";
char valid6[50] = "false";

void setup()
{  
pinMode(relay, OUTPUT);
pinMode(button, INPUT); 
pinMode(button2, INPUT);
Serial.begin(9600);
mySerial.begin(9600);
digitalWrite(relay, 1);
lcd.begin(16, 2);  
}

void loop()
{
 
  
  Serial.println(analogRead(pot2));
  delay(100);
  {
  while (mySerial.available() > 11)
  {
    myArray[0] = mySerial.read();

    //find the first charater, +
    if (myArray[0] == '+')
    {
      //read the remainding characters
      for (byte i = 1; i < 12; i++)
      {
        myArray[i] = mySerial.read();
        delay(1);
      }

      // Okay, myArray[1]thru myArray[7] has the data needed

      //Test print the characters
      for (byte i = 1; i < 8; i++)
      {
        Serial.print(myArray[i]);
      }
      Serial.println();
    } 

  } 

}

  lcd.setCursor(0, 1);
  lcd.print(myArray);
  
  buttonvalue = digitalRead(button);
  buttonvalue2 = digitalRead(button2);
  buttonvalue3 = digitalRead(button3);
  buttonvalue4 = digitalRead(button4);
  buttonvalue5 = digitalRead(button5);
  buttonvalue6 = digitalRead(button6);
  
  if(!strcmp(valid,"true"))
{
  buttonvalue=1;
}
  if(!strcmp(valid2,"true"))
{
  buttonvalue2=1;
}
  if(!strcmp(valid3, "true"))
  {
    buttonvalue3=1;
    }
if(!strcmp(valid4, "true"))
  {
    buttonvalue4=1;
    }

if(!strcmp(valid5, "true"))
{
  buttonvalue5=1;
  } 
if (!strcmp(valid6, "true"))
{
  buttonvalue6=1;
  }     
//FIRST BUTTON
    
  if(buttonvalue == 1)
  {
    digitalWrite(relay, 0);
    strcpy(valid,"true");
    maxvalue = 0.500;
    lcd.setCursor(1,0);
    lcd.print("WT INPUT:");
    lcd.print(maxvalue);
    if(!strcmp(valid,"true"))
   {
    weight = myArray;
   }
 
    if(myArray>maxvalue)
    {
     digitalWrite(relay, 1); 
     strcpy(valid,"false");
    }
    }
    
    // SECOND SWITCH
    
  else if(buttonvalue2 == 1)
  {
    digitalWrite(relay, 0);
    strcpy(valid2,"true");
    maxvalue = 1.000;
    lcd.setCursor(1,0);
    lcd.print("WT INPUT:");
    lcd.print(maxvalue);
    if(!strcmp(valid2,"true"))
    {
      myArray;
      }
      if (myArray>maxvalue)
      {
        digitalWrite(relay, 1);
        strcpy(valid2,"false");
        }
       }
       

// 3rd Button 
       
  else if(buttonvalue3 == 1)
  {
    digitalWrite(relay, 0);
    strcpy(valid3,"true");
    maxvalue = 2.000;
    lcd.setCursor(1,0);
    lcd.print("WT INPUT:");
    lcd.print(maxvalue);
    if(!strcmp(valid3,"true"))
   {
    myArray;
   }
 
    if(myArray>maxvalue)
    {
     digitalWrite(relay, 1); 
     strcpy(valid3,"false");
    }
    }

// 4th Button

else if(buttonvalue4 == 1)
  {
    digitalWrite(relay, 0);
    strcpy(valid4,"true");
    maxvalue = 3.000;
    lcd.setCursor(1,0);
    lcd.print("WT INPUT:");
    lcd.print(maxvalue);
    if(!strcmp(valid4,"true"))
   {
    myArray;
   }
 
   if(myArray>maxvalue)
    {
     digitalWrite(relay, 1); 
     strcpy(valid4,"false");
    }
    }

// 5th Button
    
else if(buttonvalue5 == 1)
  {
    digitalWrite(relay, 0);
    strcpy(valid5,"true");
    maxvalue = 4.000;
    lcd.setCursor(1,0);
    lcd.print("WT INPUT:");
    lcd.print(maxvalue);
    if(!strcmp(valid5,"true"))
   {
    myArray;
   }
 
    if(myArray>maxvalue)
    {
     digitalWrite(relay, 1); 
     strcpy(valid5,"false");
    }
    }

// Manual Button-Set value by Potentiometer 
 
  else if(buttonvalue6 == 1)
    {
    digitalWrite(relay, 0);
    strcpy(valid6,"true");
    
    lcd.setCursor(1,0);
    lcd.print("WT INPUT:");
    lcd.print(maxvalue2);
    if(!strcmp(valid6,"true"))
   {
     potvalue2 = analogRead(pot2);
    myArray;
   maxvalue2 = potvalue2;
   }
 
  if(myArray>maxvalue2)
    {
     digitalWrite(relay, 1); 
     strcpy(valid6,"false");
    }
    }    
  }

What kind of values?

For numbers:
//if - Arduino Reference

if(value1 == value2)
{
  // they are equal
}

For strings (null terminated character arrays);
//http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp
if(strcmp (string1,string2) == 0)
{
// strings are equal
}

for arrays (blocks of memory, not null terminated):

//http://www.cplusplus.com/reference/cstring/memcmp/?kw=memcmp
if(memcmp(array1, array2, sizeof(array1)) == 0
{
  // the blocks of memory are equal
}

Assuming that you have received this weight information: +000.360 Kg. You want to separate 0.360 (Kg)(fractional part) from the total and compare it with 0.250 (Kg) and then take an action (built-in L of UNO will become ON) based on the result of comparison.

The Sketch (The fractional part is in this variable: float ffracWt;.

#include<SoftwareSerial.h>
SoftwareSerial mySUART(3, 4); //SRX, STX
bool flag1 = LOW;
char myWeight[20] = "";
char intPart[3] = "";  //integer part
char fracPart[3] = "";  //fractional part
int wtAP = 0;

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()
{
  if (mySUART.available())// == 13) //+000.360 Kg CR LF
  {
    //Serial.print((char)mySUART.read());
    if (flag1 != HIGH)
    {
      byte x = mySUART.read();
      if (x == '+')
      {
        flag1 = HIGH;
      }
    }
    else
    {
      myWeight[wtAP] = mySUART.read();
      wtAP++;
      if (wtAP == 8)
      {
        Serial.print("Total Weight = ");
        Serial.println(myWeight);

        getFractionalPart();
        myWeight[20] = "";
        wtAP = 0;
        flag1 = LOW;
      }
    }
  }
}

void getFractionalPart()
{
  fracPart[0] = myWeight[4];
  fracPart[1] = myWeight[5];
  fracPart[2] = myWeight[6];
  //----------------------

  unsigned long fracWt = atol(fracPart);

  float ffracWt = (float)fracWt / 1000000.0;

  Serial.print("Fractional Weight = ");
  Serial.println(ffracWt, 3);  //0.360
  Serial.println();
  if(ffracWt > 0.250)
  {
   digitalWrite(13, HIGH); //built-in L of UNO is ON
  }
}

sm.png

sm.png

GolamMostafa:
Assuming that you have received this weight information: +000.360 Kg. You want to separate 0.360 (Kg)(fractional part) from the total and compare it with 0.250 (Kg) and then take an action (built-in L of UNO will become ON) based on the result of comparison.

The Sketch (The fractional part is in this variable: float ffracWt;.

#include<SoftwareSerial.h>

SoftwareSerial mySUART(3, 4); //SRX, STX
bool flag1 = LOW;
char myWeight[20] = "";
char intPart[3] = "";  //integer part
char fracPart[3] = "";  //fractional part
int wtAP = 0;

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()
{
  if (mySUART.available())// == 13) //+000.360 Kg CR LF
  {
    //Serial.print((char)mySUART.read());
    if (flag1 != HIGH)
    {
      byte x = mySUART.read();
      if (x == '+')
      {
        flag1 = HIGH;
      }
    }
    else
    {
      myWeight[wtAP] = mySUART.read();
      wtAP++;
      if (wtAP == 8)
      {
        Serial.print("Total Weight = ");
        Serial.println(myWeight);

getFractionalPart();
        myWeight[20] = "";
        wtAP = 0;
        flag1 = LOW;
      }
    }
  }
}

void getFractionalPart()
{
  fracPart[0] = myWeight[4];
  fracPart[1] = myWeight[5];
  fracPart[2] = myWeight[6];
  //----------------------

unsigned long fracWt = atol(fracPart);

float ffracWt = (float)fracWt / 1000000.0;

Serial.print("Fractional Weight = ");
  Serial.println(ffracWt, 3);  //0.360
  Serial.println();
  if(ffracWt > 0.250)
  {
  digitalWrite(13, HIGH); //built-in L of UNO is ON
  }
}




![sm.png|506x374](upload://aoSOgWt21GdnEyz69t5A9jfJDox.png)

Thank you so much @GolamMostafa, Now I am able to make my project works!

BIG THANK YOU!