Here's my code:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
Serial.println("Communication with HC-05 successfully started.");
}
bool is_uploading_book = false;
char c = ' ';
String recv_scanner = "";
void loop(){
if (BTserial.available()) {
c = BTserial.read();
recv_scanner += c;
if(recv_scanner.length() >= 50) {
recv_scanner.remove(0, 1);
}
// I believe there's something wrong with the recv_scanner.
if(is_uploading_book) {
Serial.write(c);
} else {
if(recv_scanner.indexOf("(__BOOK_UPLOAD_START__)") >= 0) {
is_uploading_book = true;
}
}
Serial.println("\n\n" + recv_scanner + "\n\n");
}
}
I'm sending a book (long text) in the form of characters one by one from my phone to my HC-05 module (and the Arduino).
My goal is to spot the tag "(_BOOK_UPLOAD_START_)" in the incoming characters.
Arduino allows me to receive the characters of the book one by one on each loop.
I'm storing those characters in a string called recv_scanner where I allow it's maximum size to be 50 characters and once it reaches those 50 characters, I start to remove the first character and add a new one on each loop (so it can only see 50 characters at a time because maximum String length is 200 and for my purposes, 50 is enough).
The thing is, it doesn't work properly.
When I Serial.println the recv_scanner on every loop (so I see how it progresses) it starts to look like this:
⸮qsnieq⸮cn .laslvanm.⸮⸮⸮d⸮alvuv⸮⸮urosqiam tr⸮⸮⸮
(It starts normally and then slowly progresses into this)
I have no idea what that is.
As far as my predictions go, it should show 50 characters and they should be parts of the books.
Is there something fundamental about Strings that I don't know of?
I'm doing this so I'm able to detect a tag (_BOOK_UPLOAD_END_) at the end of the book and finish uploading.
Feel free to ask my questions if something is unclear.