Arduino crashing/rebooting when i use a serial write command.

I’m having a problem with this custom library that I’ve implemented. This is my first time creating a library so forgive me for the unprofessional way in which it is written.

The purpose of the library is to allow a the main sketch to use a BT master module to establish a serial communication with another arduino with a slave bluetooth device.

Currently the program is restarting every time I use the sendcmd() function of the library to send a command to the BT master module. More specifically, it fails at the “btSerial->write(cmd);” line.

Any help would be appreciated.

Thanks

Display_R2.ino (1.38 KB)

btmodule.h (773 Bytes)

btmodule.cpp (3.6 KB)

keywords.txt (112 Bytes)

Does the crash occur the first time you call this, or in a subsequent call?

Has this function ever worked, and if so what was changed between it working and not working?

Have you really proved that the crash occurs at that specific line and not, for example in the subsequent call to errorcheck()? Remember that if the Arduino crashes shortly after a message has been written to the serial port, you may not see the message. If you want to be certain that the message has been transmitted before the sketch does anything else, you could call Serial.flush().

PeterH: Does the crash occur the first time you call this, or in a subsequent call?

Has this function ever worked, and if so what was changed between it working and not working?

Have you really proved that the crash occurs at that specific line and not, for example in the subsequent call to errorcheck()? Remember that if the Arduino crashes shortly after a message has been written to the serial port, you may not see the message. If you want to be certain that the message has been transmitted before the sketch does anything else, you could call Serial.flush().

No, it has never worked before. I've used this command in a function within the main sketch before and it worked there but I have not used it in a library.

You bring up a good point with the flushing, however, btSerial is a softwareserial port and I though software serial doesn't have the flush() function. nevertheless, i'll try it out tomorrow and will post results.

btmodule::btmodule(int tempR, int tempT, String addr, int pin) { //Initialize the Master module data variables SoftwareSerial bt(tempR, tempT); btstatuspin = pin; addr.toCharArray(slaveaddr, addr.length()); RxD = tempR; TxD = tempT; btSerial = &bt; }

What is btSerial going to equal when the constructor ends? bt is a local variable that goes out of scope. The instance of SoftwareSerial that you recorded the address of will not longer exist.

PaulS: btmodule::btmodule(int tempR, int tempT, String addr, int pin) { //Initialize the Master module data variables SoftwareSerial bt(tempR, tempT); btstatuspin = pin; addr.toCharArray(slaveaddr, addr.length()); RxD = tempR; TxD = tempT; btSerial = &bt; }

What is btSerial going to equal when the constructor ends? bt is a local variable that goes out of scope. The instance of SoftwareSerial that you recorded the address of will not longer exist.

Oh yeah, you're right. bt will disappear when the constructor ends so that btSerial pointer will point to nothing. I tried making an instance of the software serial object in the .h file but since I don't have the tempR and tempT values until the constructor is called, it doesn't work.

Any suggestions?

Any suggestions?

Change btSerial to a pointer.

btSerial = new SoftwareSerial(tempR, tempT);

Passing addr as a char *, instead of a wasteful String would be better, too.

Kenny03: Any suggestions?

Don't preserve the address of a temporary variable.

Either construct the SoftwareSerial instance as a member variable in your constructor, or pass in a pointer/reference and use that to initialise a pointer/reference in a member variable.

Thanks for the help guys

I did what you had suggested and it worked.

I created a software serial object in the main sketch and passed a pointer to that object instead of trying to create the object in the constructor.