I am using Arduino Mega 2560, Weight Indicator RS232 data is forwarded to arduino using TTL module and the result is in this form US,GS,+00743.5kg I want to separate the weight which is 00743.5 and store as it will compare with other floating no. please guide me in this matter.
Read Robin2's excellent tutorial Serial Input Basics to see how to gather and parse serial data
Yes I have gone through it but still getting same response data is coming continuously how to store it and use use to operate led or relay using if else statement data using terminal is attached in excell sheet
Let's see your code. Use Code Tags.
EDIT:
Also, show us the full spec on the serial data format. Include all variations, delimiters, non-printing characters, control characters, etc.
When Weight is connected with MAX3232 TTL Convertor which is connected to RX pin 0 and TX pin 1 of arduino mega 2560. The data is coming " US,GS,+00743.5kg" continuously on serial terminal of Arduino IDE even without code. but weight changes as increase the weight. I want only numerical values so it can be compared with Set values or Target value and Set one of Digital output high
The format of the data generated by weight indicator is attached.
Please guide me in this matter
Thanks

blh64:
Read Robin2's excellent tutorial Serial Input Basics to see how to gather and parse serial data
rasengr:
The data is coming " US,GS,+00743.5kg" continuously on serial terminal....Please guide me in this matter
if you have been though the tutorial suggested by @blh64 and is storing the received characters to an array, then you could do something like this to split and get the numerical weight out of the characters:
(compiles, NOT tested!)
void setup() {
char serial_in[20] = "US,GS,+00743.5kg\r\n";
char s[] = ",";
char *token;
Serial.begin(115200);
/* get the first string token */
token = strtok(serial_in, s);
//loop through string tokens
while ( token != NULL ) {
//print string to serial monitor
Serial.print(token);
Serial.print(", ");
//convert 'float string' to float number
float val = atof(token);
//print float value to 1dp to serial monitor
Serial.println( val,1);
//continue splitting string
token = strtok(NULL, s);
}
}
void loop() {
}
what you should be getting with the above code is something like this:
US, 0.0 <--- zero is return if split string does not contain number
GS, 0.0
+00743.5kg
, 743.5 <--- 743.5 'extracted' from +00743.5kg
hope that helps...
You can not use pin 0 and 1 to both talk to the scale and the serial monitor. The mega has more than one serial port so I would suggest you wire you scale up to one of the other Serial ports. This leaves pin 0 and 1 available for printing debugging information, etc. to the serial monitor.
If you have read Robin2's tutorial that I shared in reply #1, there is a perfectly good example that shows you how to capture the data, and parse it and even convert it from ASCII to the numerical value. Give it a try. Post your best attempt and what isn't working if you get stuck.
I got my required output using this code. Thanks to all
String inString = ""; // string to hold input
int a = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial1.begin(9600);
Serial.begin(9600);
pinMode(13, OUTPUT);
while (!Serial1) {
; // wait for serial port to connect. Needed for native USB port only
}
// send an intro:
// Serial1.println("\n\nString toInt():");
//Serial1.println();
}
void loop() {
// Read serial input:
while (Serial1.available() > 0) {
int inChar = Serial1.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
a = inString.toInt()/10;
Serial.println(a);
inString = "";
if (a >=15)
{
digitalWrite(13,HIGH);
Serial.println("LED13");
}
else
{
digitalWrite(13,LOW);
}
}
}
}
It would be much better to actually use the code Robin2 provides as it does not use the String class which can be problematic on an arduino mega.
You also need to step up your game and use code tags when posting your code, just like @sherzaad did. It is all explained (and more) in the sticky post at the top of the forum. It is well worth the read
This code is guaranteed to fail after some random period of time, on an Arduino Uno or Mega:
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
