0

I have this:

for (int i = 0; i < (DELAY_SIZE - 1); i++) {
   if (check_tag_in_char(delay_chars[i], BOOK_UPLOAD_END_TAG)) {
       tag_pos = 0;
       Serial.println("Uploaded");
       is_uploading_book = false;
   }
}

where

#define DELAY_SIZE 50
const char* BOOK_UPLOAD_END_TAG = "(__BOOK_UPLOAD_END__)";
char delay_chars[DELAY_SIZE];

int check_tag_in_chars(char a, const char* TAG_STR) {
   if (a == TAG_STR[tag_pos]) { tag_pos++; } else if (a == TAG_STR[0]) { tag_pos = 1; } else { tag_pos = 0; }
   if (TAG_STR[tag_pos] == '\0') {
      return true;
   } else {
      return false;
   }
}

The for loop above with the check_tag_in_chars check seems to make my Arduino spit jibberish on Serial.write() (which you can see in the full code).
I suppose the for loop is somehow overloading the Arduino.
The Serial.write() starts normal and then turns into jibberish like that:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla est purus, ultrices in porttitor  in, accus⸮o⸮a⸮a⸮nct⸮oto⸮ou⸮ubt⸮ue⸮tl⸮ei⸮at⸮e⸮i⸮rm⸮⸮tlu⸮or⸮tatcsu⸮om⸮lu⸮oaess⸮vi⸮pdteea⸮aestp⸮r⸮tda⸮lcp⸮ddt⸮ir⸮eo⸮oe⸮i⸮g⸮u⸮aacdeu⸮bt.Qiu⸮pdtiu⸮l g⸮ie⸮ur.Csveamei⸮n⸮ievtumtp⸮vi⸮d⸮eil⸮oi⸮ruiusbeu⸮rs⸮l⸮ae⸮ai⸮t,⸮⸮oe⸮om l⸮cdo⸮aesua⸮i⸮iin⸮⸮ab⸮e⸮vil⸮⸮u⸮u⸮an⸮fiiamssa. C⸮oi⸮tque petu⸮⸮ai⸮sptitmts⸮st⸮ril⸮u⸮tcsu⸮lie⸮t⸮nrnumuscg⸮oa⸮vu⸮vr⸮uci⸮l⸮g⸮oinm⸮msi⸮aq⸮ptb⸮tmn⸮i⸮rrn⸮oe⸮aerrdlsm.Ierbeu⸮gtslu,n⸮aisnl⸮ott t.C⸮oi⸮tu⸮etu⸮⸮ai⸮sptit⸮ns⸮st⸮icu⸮s⸮



Here's the full 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.");
}

const char* BOOK_UPLOAD_START_TAG = "(__BOOK_UPLOAD_START__)";
const char* BOOK_UPLOAD_END_TAG = "(__BOOK_UPLOAD_END__)";
int tag_pos = 0;
char c;
bool is_uploading_book = false;

#define DELAY_SIZE 50
char delay_chars[DELAY_SIZE];
int update_count = 0;

void loop(){
    if (BTserial.available()) {
        c = BTserial.read();
        if (is_uploading_book) {
          Serial.write(c);
          if(update_count < DELAY_SIZE) {
            delay_chars[update_count] = c;
            update_count++;
          } else {
            for (int i = 0; i < (DELAY_SIZE - 1); i++) {
                delay_chars[i] = delay_chars[i + 1];
            }
            delay_chars[DELAY_SIZE - 1] = c;
            // Checking if the end tag is in the delay_chars
            for (int i = 0; i < (DELAY_SIZE - 1); i++) {
              if (check_tag_in_chars(delay_chars[i], BOOK_UPLOAD_END_TAG)) {
                tag_pos = 0;
                Serial.println("Uploaded");
                is_uploading_book = false;
              }
            }
          }
        } else {
          if (check_tag_in_chars(c, BOOK_UPLOAD_START_TAG)) {
            tag_pos = 0;
            Serial.println("Starting");
            is_uploading_book = true;
          }
        }
    }

}

int check_tag_in_chars(char a, const char* TAG_STR) {
  if (a == TAG_STR[tag_pos])
    { tag_pos++; }
  else if (a == TAG_STR[0])
    { tag_pos = 1; }
  else
    { tag_pos = 0; }

  if (TAG_STR[tag_pos] == '\0') {
    return true;
  } else {
    return false;
  }
}

Is there something that is overloading and torturing the Arduino? Are there any alternatives to go about the problem that will not create this problem?

NEW: It turns out that as I decrease the DELAY_SIZE value, the jibberish slowly starts to look normal. Somehow that for loop tortures the Arduino (and it's not so much the for loop but the check it's done in it)

NEW NEW: I tried looping a loop and it seems that making extensive loops overloads the Arduino and it starts jibberish. Is this normal or there's something with my Arduino?

NEW NEW NEW: If my void loop() looks like this:

void loop(){
   if (BTserial.available()) {
      c = BTserial.read();
      Serial.write(c);
      for (int i = 0; i < (DELAY_SIZE - 1); i++) {
         if (check_tag_in_chars(delay_chars[i], BOOK_UPLOAD_END_TAG)) {
            tag_pos = 0;
            Serial.println("Uploaded");
            is_uploading_book = false;
         }
      }
   }
}

The problem of jibberish persists (the jibberish is less but it's still jibberish). The fact that I'm looping through the check_tag_in_chars 50 times every new character is torturing the Arduino.
I don't know what to do.

11
  • You didn't read my typo correction in your other correction. delay_chars[DELAY_SIZE - 1] = c; } should be } delay_chars[DELAY_SIZE - 1] = c; – Majenko Mar 14 '20 at 19:01
  • @Majenko Sorry, fixed it (I got the delay_chars[DELAY_SIZE - 1] = c; out of the loop) but the problem stays. I did some debugging and the jibberish issue originates from the loop I've stated above. – Sleb Lagnej Mar 14 '20 at 19:07
  • @Juraj Please take a look at the full code. – Sleb Lagnej Mar 14 '20 at 19:28
  • 4
    I suspect that the problem is actually SoftwareSerial being a pile of rancid badger vomit. – Majenko Mar 14 '20 at 21:16
  • or the BT module is on different baud rate – Juraj Mar 15 '20 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.