Sending variable length strings over i2c

So after a lot of time I've got my basic code which sends input received on one arudino to the other over i2c, making use of a whitelist to restrict what can be sent.

One thing that's stumped me is dealing with the padding characters you get if you don't send exactly how much information the arduino request (e.g. hi thereÿÿ).

I figure there would be two ways around dealing with this, I'd either have to get Wire.requestFrom() to request the right number of characters each time, or I could simply remove the padding characters from the input. Both of which I'm not sure how you'd go around, because the second board doesn't know how long the input is going to be, and if I add an if statement saying

if (input != 'ÿ'){
       Serial.print(input)
}

That doesn't work either. I'm dead certain the solution would be trivial, it seems like something that would be used often, I just can't seem to find it

Have you tried reading until Wire.available() becomes false?

Sorry totally forgot about this and did other stuff on the program instead. Yeah I'm already using that, unfortunately if you request more than is sent you get the filler characters and they're counted as input on wire.available()

Do you have control over what the padding character is? If so just make the padding character a '\0' so you can use strlen to see how much data was received.

strlen(input);

I don't believe so, I think it's always that funny y

I don't believe so, I think it's always that funny y

That "funny y" is you attempting to print -1 as an ascii character. The ASCII symbol at position 255 in the table (which is what -1 as an unsigned char corresponds to) is the "funny y".

The -1 is what is returned when XXX.read() has nothing to read.

Ah that’d be why I couldn’t find it in the ascii table, I’m aware it’s a character that is returned when there is nothing to read in wire.read, it’s not very well documented but I did find it out previously. What would you recommend as the best way to remove it when I want to print my input to serial or save it in a char array? I’d imagine there’s a very easy way to remove it as I can’t be the only person who has to deal with this :stuck_out_tongue:

What would you recommend as the best way to remove it when I want to print my input to serial or save it in a char array?

int aChar = Wire.read();
if(aChar > 0)
{
   // We got a good character. Save it...
}

Always the easy solution isn’t it! I tried filtering when a character was not equal to -1, silly me >.<
And when I say -1 I did something totally wrong, I’ll include it to give you a laugh :slight_smile:

if (charA != "-1") {
}

So yeah I’m a total dilberry

Thank you so much PaulS, I think all bar one issue I’ve had you helped me out with, it is greatly appreciated