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
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
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.