Help for IF condition if only take 2 char

I have a project to make a filtering program, I get the data from the barcode sensor and I want to use IF condition for sorting
example
A023434224 and I want take only 2 characters A0xxxxx
below is the code I use, and what do i need to replace on ???
*Edit: I mean filtering lmao, filtering the code to sorting the item

#include <HardwareSerial.h>

HardwareSerial MySerial(2);
String code;

void setup() {
    Serial.begin(115200);
    MySerial.begin(115200, SERIAL_8N1, 16, 17);
}

void loop() {
    while (MySerial.available()> 0) {
     code = MySerial.readString();
    Serial.println(kode);
    }
    if (MySerial.readString(???)){
      Serial.println("Type A")
      }
}

Why not read the whole String then strip off everything except the first 2 characters if they are the ones that you are interested in ?

If you are reading in a String then you can use any of the String methods to get the first two characters. Arduino Reference - Arduino Reference

Look at .startWith()

#include <HardwareSerial.h>

HardwareSerial MySerial(2);
String code ="";

void setup() {
  Serial.begin(115200);
  MySerial.begin(115200, SERIAL_8N1, 16, 17);
}

void loop() {
  while (MySerial.available() > 0) {
    code = MySerial.readString();
    Serial.println(code);
    Serial.println(code.substring(0, 2));
    if (code.substring(0, 2) == "A0") {
      Serial.println("Type A");
    }
  }
}
1 Like

consider

char buf [80];

void loop() {
    if (Serial.available())  {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf));
        buf [n] = '\0';

        if (! strncmp (buf, "A0", 2))
            Serial.println (buf);
    }
}

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);
}

the code needs to read/digest everything received, including the desired parts

1 Like

For me it is not quite clear what you want to do:

  • Sorting means to bring a number of entities in a certain order (e.g. from small numbers to large numbers, from A to Z,...)
  • Filtering would be to select all strings that fulfill a certain requirement, e.g. start with "A0" or just take the first two characters (whatever they are) of a string for further evaluation.

The code you posted in # 1 suggests that you try to

  • either get all strings that start with "A0"
  • or get the first two characters of strings coming from the barcode sensor...

The following sketch (just based on Serial, not HardwareSerial!) shows how you may handle it:

String code = "";
char   charIn = ' ';
boolean CheckString = false;

void setup() {
    Serial.begin(115200);
    Serial.println("Start");
}

void loop() {
    while (Serial.available()> 0 && !CheckString) {
      charIn = Serial.read();
      if (charIn >= ' ') code += charIn;      // Any control character will stop adding to the string
                  else CheckString = true;      // So you do not have to care whether it is CR and/or LF
    };
    if (CheckString) {    // You will get here only, when a control character appeared

    // This prints the whole string
      Serial.print("The whole string         = ");
      Serial.println(code);
    // This prints the first two characters of the string
      Serial.print("The first two characters = ");
      Serial.println(code.substring(0,2));
    // This checks for "A0" and prints "Type A"
      Serial.print("The string is ");
      if (code.startsWith("A0")){
         Serial.println("of type A");
      } else {
         Serial.println("not of type A");
      }
      code = "";
      CheckString = false;
   }
}

Feel also free to play around with the sketch here

https://wokwi.com/projects/326117636427481683

1 Like

so I want to make a sorting program, for example there are two objects with barcodes A02651616 and A03783727 because only the first two characters are the same and the rest are different.

thanks everyone, I will try it tonight

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.