Hello i have 2x ardiono Mega 2560 with LCD and keypad connected using NRF24L01 module. It is working only sometimes so i dont know where is problem in code or in hardware? Please look at my code and tell me if it is functional. Thanks for help.
Setting address is ok but my real problem is then from one device everithing what i send is registred on second device. But for example only 1 of 10 message is recieved when sending from second to first.
Quote
This needs to be global and not local to setAddress() as you are returning it.
Why? It is perfectly OK for add to be defined in setAddress(), and returned by setAddress(). Returning a global variable is silly.
As I understand it an object, declared locally is created completely on the stack no part of the object is created on the heap!. Thus
a. no explicit destructor is required.
b. its an object, so a pointer to it is returned not a copy of the object
After the return from setAddress() the point gives the address of part of the stack which will be over written by later function calls.
If you want to return an object from a function the it should be created with new().
Retuning something declared/defined globally is far from silly, its a bog standard thing to do. Eg ON/OFF declared as constants.
@OP
If i'm right an add is on the stack and then gets over written in a later function call then you'll get strange results which may explain 1 in 10 of your messages going missing
So i tried to avoid this problematic code so i set Recieving and Target address in code manually for both devices. But still same problem occures. Any other thoughts where problem could be?
String setAddress(String rec) {
...
while (true) {
if ( key == 'C' )
return setAddress(rec);
}
}
means for every 'C' without 'D' you go one level deeper in the setAddress() recursion ?
Looks suspicious to me, but didn't analyse completely ( ... because I hate Strings, which look like they could be used like ordinary variables, e.g. returned by value without fear to get a pointer to invalid stack variable.)
@ holmes4: Assumed it's implemented properly, setAddress returns a valid copy of it's local variable add. Guess how many constructors and destructors have to be called to make it work ...
...
while (true) {
if ( key == 'C' )
return setAddress(rec);
}
}
means for every 'C' without 'D' you go one level deeper in the setAddress() recursion ?
Looks suspicious to me, but didn't analyse completely
<sub>( ... because I hate Strings, which look like they could be used like ordinary variables, e.g. returned by value without fear to get a pointer to invalid stack variable.)</sub>
@ holmes4: Assumed it's implemented properly, setAddress returns a valid copy of it's local variable add. Guess how many constructors and destructors have to be called to make it work ...