I am trying to extract a substring out of another string, but it doesn't seem to be working. I am new to programming in arduino and c++ in general. Here is my code:
void setup()
{
Serial.begin(9600);
}
char rfid;
void loop()
{
while (Serial.available() > 0)
{
rfid = Serial.read();
String cardnum(rfid);
cardnum.replace("", "");
cardnum.replace("", "");
String id = cardnum.substring(0,2);
Serial.print(id);
}
}
The first argument in cardnum.replace("", "");
and cardnum.replace("", "");
replaces the start and end bit with nothing, but it somehow doesn't display it correctly here.
Now on to the problem at hand. The output for cardnum
is 1F006B471F2C which it should be, but the ouput for id
is the same, when i want it to be only the first two characters of cardnum
. How would i go about achieving that.
Thanks in advance for any answers you can provide.
while{}
loop executes once for each character you read - is that what you intend? I suspect your intent was to read the the whole string and then modify it. If so, perhaps you'd find theSerial.readBytes()
function useful.