Go Down

Topic: Weird sketch size/memory/compiler (?) related issue (Read 122 times) previous topic - next topic

michalmonday

May 26, 2018, 02:19 pm Last Edit: May 26, 2018, 06:30 pm by michalmonday
Also posted at: arduino.stackexchange.com

Hi, is there anyone who experienced similar problem?

The problem is after uploading the code Arduino Pro Micro hangs (and RX led is constantly on).

The problem occurs instantly after turning on the microcontroller even if I put "delay(10000);" at the begining of the code trying to see at which point the error occurs.


The problem can be "fixed" by adding 'Serial.print("aa");' at random place within the code...
Which results in the following output during upload:
Quote
Sketch uses 18318 bytes (63%) of program storage space. Maximum is 28672 bytes.
Global variables use 1814 bytes (70%) of dynamic memory, leaving 746 bytes for local variables. Maximum is 2560 bytes.
Suprisingly, using 'Serial.print("a");" instead does not fix the problem. It results in the following output during upload:
Quote
Sketch uses 18314 bytes (63%) of program storage space. Maximum is 28672 bytes.
Global variables use 1810 bytes (70%) of dynamic memory, leaving 750 bytes for local variables. Maximum is 2560 bytes.
It seems very weird to me that adding serial print instruction with certain length (at seemingly random place) can solve such problem...

Another circumstance which seems to be solving the problem is commenting out one of the 2 lines (does not matter which):
sprintf(at_cmd, "AT+NAME%s\0", bluetoothName);
sprintf(at_pin_cmd, "AT+PIN%s\0", pin);


It's a spaghetti code so I doubt anyone will analyse it but just in case if anyone wanted to check it for some more or less obvious characteristic (e.g. F()/eeprom/memory extensive use or something...) then here it is: Hastebin - link

I'm using "Arduino Pro Micro 5V" and Arduino IDE 1.8.5 with "Arduino Leonardo" board setting


-dev

Most people will not (should not!) download from Pastebin.

Just attach the INO file to your post.  Select "More" in the lower right corner of your post, then "Modify".  Then choose Attachments and other options in the lower left corner of the full post editor.
Really, I used to be /dev.  :(

johnwasser

Sometimes, if you turn up the warning level to "ALL", you will get warning that point to programming mistakes.  Maybe one of these warnings is significant:
Code: [Select]
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'void ChangeBluetoothCheck()':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:286:49: warning: embedded '\0' in format [-Wformat-contains-nul]
     sprintf(at_cmd, "AT+NAME%s\0", bluetoothName);
                                                 ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:298:12: warning: unused variable 'c' [-Wunused-variable]
       char c = BTSerial.read();
            ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:311:42: warning: embedded '\0' in format [-Wformat-contains-nul]
     sprintf(at_pin_cmd, "AT+PIN%s\0", pin);
                                          ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:324:12: warning: unused variable 'c' [-Wunused-variable]
       char c = BTSerial.read();
            ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'void Check_Protocol(char*)':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:666:48: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
     char key_2[2] = {HexToChar(inStr + 2), "\0"};
                                                ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:670:27: warning: invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
     myKeyboard.press(key_2);
                           ^
In file included from /Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:9:0:
/Users/john/Documents/Arduino/libraries/Keyboard/src/Keyboard.h:106:10: note: initializing argument 1 of 'size_t Keyboard_::press(uint8_t)'
   size_t press(uint8_t k);
          ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:678:18: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
     Print(inStr[0]);
                  ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:825:6: note: initializing argument 1 of 'void Print(const char*)'
 void Print(const char *inStr)
      ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:756:41: warning: embedded '\0' in format [-Wformat-contains-nul]
     sprintf(bluetoothName, "%s\0", inStr);
                                         ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:765:31: warning: embedded '\0' in format [-Wformat-contains-nul]
     sprintf(pin, "%s\0", inStr);
                               ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'int SubStrIndex(const char*, const char*)':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:797:30: warning: invalid conversion from 'char*' to 'int' [-fpermissive]
   int ptr = strstr(str, sfind);
                              ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'bool IsCmd(const char*, const char*)':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:805:38: warning: invalid conversion from 'const char*' to 'char*' [-fpermissive]
   return (StrStartsWith(str, cmdStart) && StrEndsWith(str, ",end"));
                                      ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:813:6: note: initializing argument 1 of 'bool StrStartsWith(char*, const char*)'
 bool StrStartsWith(char* str, const char* desiredStart)
      ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:805:66: warning: invalid conversion from 'const char*' to 'char*' [-fpermissive]
   return (StrStartsWith(str, cmdStart) && StrEndsWith(str, ",end"));
                                                                  ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:818:6: note: initializing argument 1 of 'bool StrEndsWith(char*, const char*)'
 bool StrEndsWith(char* str, const char* desiredEnd)
      ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'int GetKeyIndex(byte, byte*)':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:891:41: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]
   for (byte i = 0; i < strlen(char_array); i++)
                                         ^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Arduino.h:25:0,
                 from sketch/sketch_may03a.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/4.9.2-atmel3.5.4-arduino2/avr/include/string.h:399:15: note: initializing argument 1 of 'size_t strlen(const char*)'
 extern size_t strlen(const char *) __ATTR_PURE__;
               ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino: In function 'void SetNewCharEncoding(char*)':
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:1008:44: warning: embedded '\0' in format [-Wformat-contains-nul]
         sprintf(encodingName, "%s\0", inStr);
                                            ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:1033:39: warning: narrowing conversion of '*(inStr + ((sizetype)offset))' from 'char' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
     byte strValBuff[3] = {inStr[offset], inStr[offset + 1], '\0'};
                                       ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:1033:58: warning: narrowing conversion of '*(inStr + (((sizetype)offset) + 1u))' from 'char' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
     byte strValBuff[3] = {inStr[offset], inStr[offset + 1], '\0'};
                                                          ^
/Users/john/Documents/Arduino/sketch_may03a/sketch_may03a.ino:1035:87: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'char*' [-fpermissive]
     Encoding[encodingFactor][offset / 2] = (byte)strtoul((char*)strtok(strValBuff, " "), NULL, 16);           //(((byte)inStr[offset]) * 16) + (byte)inStr[offset+1];
                                                                                       ^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Arduino.h:25:0,
                 from sketch/sketch_may03a.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/4.9.2-atmel3.5.4-arduino2/avr/include/string.h:576:14: note: initializing argument 1 of 'char* strtok(char*, const char*)'
 extern char *strtok(char *, const char *);
              ^
Sketch uses 18406 bytes (64%) of program storage space. Maximum is 28672 bytes.
Global variables use 1814 bytes (70%) of dynamic memory, leaving 746 bytes for local variables. Maximum is 2560 bytes.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

michalmonday

#3
May 26, 2018, 04:54 pm Last Edit: May 26, 2018, 06:36 pm by michalmonday
Thanks a lot, I would definately not find it out myself.
It seems that removing "\0"s in sprintf functions solved the problem :)

Edit: I think I was a bit too excited that it worked... The issue still occurs if a print statement is added. I'll try to take a closer look at the compiler warnings, thanks anyway.

Edit_2: I tried to remove every occurence of F(), it seems to fix the issue but it's difficult to say whether it's "real" solution unlike adding_some_random_Serial_lines_fix...

sterretje

Usually, if adding something random in a random place seems to solve an issue, you have a memory problem.

Low on memory can cause this, another culprit is writing outside the boundaries of an array.

Can't view your code, the colour scheme is poorly chosen.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

pert

Also posted at:
https://arduino.stackexchange.com/q/53066
If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

Go Up