i2C Arduino to Arduino 0xFF ÿ problem

Hi please can anyone help me I’m really stuck and have tried to understand whats going on with this.

Arduino Mini Pro > Arduino Mega 2560 over i2C but i get this symbol at end of lines not every line but most. The first say 20 lines are free of it then it appears ?

ÿ

heres the master parts of code it driving a lcd display and output via serial.

Wire.requestFrom(9, 18);

int index = 0;

char dataRx[20] = "";

  while(Wire.available() > 0)   
  { 
    char incominglaptime = Wire.read(); 

    
    dataRx[index++] = incominglaptime;
    dataRx[index] = '\0';
    
  }



Serial.println(dataRx);




void receiveEvent(int howMany)
{
 str[0] = '\0';
 while(0 < Wire.available())
 {
   str[position] = Wire.read();
   if(str[position] == '>')
   {
     str[position+1]='\0';
     position=0;
     break;
   }
   else
   {
   position++;
   }    
 }
 
 receivedMessage = String(str);

Heres the Slave Code:-

int MS1int = ms1;
int MS2int = ms2;
int S1int = s1;
int M1int = m1;
int S2int = s2;
int lap count; ////this is just a single or double number normally like 23 etc
int fasterorslower;////this is a 2 for faster and a 1 for slower
sprintf(laptimeresult, "%02d,%01d,%02d,%03d,%02d,%02d,%03d",lapcount,fasterorslower,S2int,MS2int,M1int,S1int,MS1int);



void requestEvent()
{

Wire.write(laptimeresult);        // sends the Times From Laptimer over i2c 


}

it ejects this note the funny char at end that moves around ?

15,1,36,976,00,42,990ÿ
15,1,36,976,00,42,990ÿ
15,1,36,976,00,42,99ÿÿ

//////ive tried adding the ‘\0’ at the end of the sprintf and add its % to the % list at the front like this %02d but that did not work either.

:confused:

Thank you so much if anyone can give me a bit of help i feel I’m close but yet so far and could do with a smart person to chip in and drop the code that actually works so i can see where I’m going wrong.

Thank you so much.
James

to little code to reproduce. The ÿ is char(255) so apparently you are not closing your char array's correctly

Wire.requestFrom(9, 18);

returns # bytes received if I 'm not mistaken -> might help you

Hi Rob

Thank you so much for your reply....i've read it a few times but i can't quite understand what you mean.

"you are not closing your char array's correctly"

what do i need to do to my code to close them properly?

Many thanks

james

EDIT - Ignore the void receiveEvent code function. Its not used in my code i copied it by mistake it was a test.

could it be a case of maybe filtering it out in some way before we send the char to the dataRx ?

char dataRx[20] = "";
while(Wire.available() > 0)   
  { 



if (incominglaptime != "-1") {

   dataRx[index++] = incominglaptime;
   dataRx[index] = '\0';

}
}

or

char dataRx[20] = "";
while(Wire.available() > 0)   
  { 



char incominglaptime = Wire.read();

if(incominglaptime > 0)
{
   dataRx[index++] = incominglaptime;
   dataRx[index] = '\0';
}

}

but I'm not sure i can do this action with a char ? I've seen PaulS do this with an int?

James

just a thought-

could it be something to do with this bit?

char dataRx[20] = "";

ive actually got 15 characters built into the sender sprintf.

sprintf(laptimeresult, "%02d,%01d,%02d,%03d,%02d,%02d,%03d",lapcount,fasterorslower,S2int,MS2int,M1int,S1int,MS1int);

just a thought ::slight_smile:

The sprintf will generate 21 characters.

Pete

I count 22 if you include the trailing 0x00.

The array is only 20. And you are requesting 18. Better sort all that out.

Thank you Nick and Pete for your replies.

Ive now sorted it and it works !! :smiley:

It was a case of setting the master to request 21 in my case - and make the arrays on both sides empty .

I tried 22 and it gave me the funny Y at end of printed line.

~So happy days.