Reading weighing scale via serial and use it

Hello.. I'm new here..

I read the serial from the weighing scale and it says "ww0000.00kg"

how to make the relay turn on when the scale is ww0001.00kg?

This is my code to read it:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);  //SRX, STX   

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

void loop()
{
    if(mySerial.available()>0)
    {
       char c = mySerial.read();
       Serial.write(c);
    }
}

thanks..

What relay? How is it powered? How is it connected to the Arduino? Have you tried?

Paul

Paul_KD7HB:
What relay? How is it powered? How is it connected to the Arduino? Have you tried?

Paul

I have to connect a relay to arduino output by pin 6.

int relayPin = 6;

and i want to make the relay turn on when the scale is 1kg.
this code for example:

void loop() {
  weight = ; // serial data from weighing scale.
  target = 1.00kg; // weight target
  

  if (weight > target)  {
  digitalWrite(relayPin, HIGH);
  }
  else {
  digitalWrite(relayPin, LOW);
  }
}

Have a look at the parse example in Serial Input Basics - simple reliable ways to receive data.

The parse example assumes you are using the 3rd style for receiving (with start- and end-markers) but it can easily be adapted for the second style with just an end-marker if that is more suitable for your scale.

...R

case closed. I've found a solution.
first, i remove two "ww" character. second, convert to float.

this is my final code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);  //SRX, STX
String inString = "";
float weight;
int target;
int relayPin1 = 7;

void setup()
{
    Serial.begin(9600);
    mySerial.begin(9600);
    pinMode(relayPin1, OUTPUT);
}

void loop()
{
    if(mySerial.available()>0)
    {
        int inChar = mySerial.read();
        if (inChar != '\n') {
            inString += (char)inChar;
        }
        else {
            target = 1.00;
            inString.remove(0, 2);
            weight = inString.toFloat();
            Serial.println(weight);
            inString = "";
            if (weight > target)  {
                digitalWrite(relayPin1, LOW );
            }
            else {
                digitalWrite(relayPin1, HIGH );
            }
        }

    }
}

thanks for helping me..

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Thank you for the advice. I will try it..