I have this really weird problem right now and I would be very glad
if anyone could look at my code...
The code parses a bluetooth adress that is recieved over the serial connection.
As you can see, I have an array listOfDevices, that stores 5 device adresses.
Now my problem is that after I parse the bluetooth code there is a change
in this array that shouldn't be: the first item of the array changes to the
newest parsed adress, even though I didn't change the item.
The change really happens between the first and the second time I print out the adress.
Do you have any idea why this could happen?
I'm thankful for any help with this...
Yves
char device_adress[18];
char* listOfDevices[7]; // all the listOfDevices that may connect to the power thingy
if (message[0] == char(82) && message[1] == char(73) && message[2] == char(78) && message[3] == char(71)){
// RING, device tries to connect
Serial.println("RING RING RING");
Serial.println("device list start:");
Serial.println(listOfDevices[0]); ///////////// shows the latest adress correctly
Serial.println(listOfDevices[1]);
Serial.println(listOfDevices[2]);
Serial.println(listOfDevices[3]);
Serial.println(listOfDevices[4]);
Serial.println("device list end:");
Serial.println("device list start:");
Serial.println(listOfDevices[0]); suddenly shows device_adress, even though I didn't assign it to this array index?!?
Serial.println(listOfDevices[2]);
Serial.println(listOfDevices[3]);
Serial.println(listOfDevices[4]);
Serial.println("device list end:");
It would be better if you posted the whole program using the "code" key (#). Particularly the assignments to "listOfDevices" and the declaration of "message"
no, I actually like for loops :). It was just a very quick thing to nail down
where the problem was...
hm the code is too long to post here, I think but I'll add the assignments here:
the assignment for message:
char message[128];
the code where the addresses are assigned:
if (known_device == false){ // adding device to the list
device_count = 0; // finding a free spot for the new device adress
while(listOfDevices[device_count][0] != '?')// no entry
{
device_count ++;
}
listOfDevices[device_count] = device_adress;
Serial.println("device added to the list");
bluetoothBlink();
friendlyDeviceAround = true;
led_bluetoothOn = 1;
}// end if
I wonder if the symptoms you are seeing are because you have run out of RAM. If you have a lot of strings being printed to the serial port, this along with the buffers you are explicitly allocating, may be causing the problem. Try commenting out all Serial.print code except that needed to verify that the address is correct.
I followed your advice and commented most of the debug serial communication out.
no luck though so far. I have four unsigned longs in use (for timer uses), could they
cause trouble?
Sorry about the misunderstanding about "device_adress" - what I really meant was, "listOfDevices" is an array of pointers, but where is the memory that holds whatever it is that these pointers point to?
Where and what to you assign to, say "listOfDevices [0]" ?
ok, we are getting there I think...!
as far as I can tell, I didn't allocate any memory to them.
I tried to do it like this:
char* listOfDevices[7][18];
This is accepted, but if I try to compare like this:
if (listOfDevices[device_count][0] == device_adress[0]){
It's not accepted because of this error:
In function 'void parseSerial()':
error: ISO C++ forbids comparison between pointer and integer In function 'void emptyDeviceList()':
if (listOfDevices[device_count][0] == device_adress[0]){
It's not accepted because of this error:
In function 'void parseSerial()':
error: ISO C++ forbids comparison between pointer and integer In function 'void emptyDeviceList()':
So how would I do that?
yves
You need to make the pointer, an int.
The way that is done is by 'dereferencing' the pointer as per:
if ( *listOfDevices[device_count][0] == device_adress[0] ){
//
}