Char masking

I have a question about displaying characters from a string. If I was receiving a string of characters from an i2c connection and trying to save them into different variables to be displayed by a function. How would I got about that?

Say if I were receiving ABCDEFGHI into x. and wanted to ABCDE into y and FGHI into z. I'm sure its easy I'm just not sure how to go about it. Thanks.

The variables to receive the characters into need to be arrays.

char x[10]; char y[10]; char z[10];

After you get the characters into x, you need to decide how many of them belong in y and how many belong in z. Then, it's just a matter of 2 for loops to copy from x to y and z.

well I’m doing it inside of a

while(0<Wire.available())
{

}

so it is receiving one character at a time if something is there. I get a compiler error when I try to do

char x[5]=wire.receive();

I was thinking I could do something like

y = x >> 3

or something. Some kind of masking to eliminate the digits I’m not using.

You might need casting. adding (char) into the statement explicitly converts datatypes if possible. As wire.receive() returns a byte , casting to a char should work.
char x[5]= (char) wire.receive();

update: this does not work, as PaulS points out here below. Casting is for individual elements of the array.

@robtillaart Casting won't fix a fundamental flaw like that.

@bsatterwhite You need to separate the declaration of the array from the initialization of the array elements:

char x[5];
int index = 0;
.
.
.
while(Wire.available() > 0)
{
   x[index] = Wire.receive();
   index++;
}

The compiler didn’t like the casting. I think that should have worked.

void receiveEvent(int howmany)       // function for receiving the data off of the bus
{
    
    
    int s=Wire.receive(); 
    boolean z=Wire.receive();      
   
    while(0<Wire.available())
    {
      
      char y= Wire.receive(); 
      char c= Wire.receive();
      
      DisplayMain(s,z,y,c);       // displays on GLCD
    
    }

I need to receive 5 characters into y and 4 characters into c and display them with my display function. My other board is sending them and my display function will display them but this is the part thats messing me up.

The compiler didn't like the casting. I think that should have worked.

Why do you think that should have worked? The wire.receive() function returns what? The version I have returns a single uint8_t sized value.

You are then trying to cast that to an array of fixed size. Is there really a cast that perform that operation?

I don't know. I'm just trying to figure out how to do this. I don't see a way to get it to work.

I don't see a way to get it to work.

Maybe start by re-reading Reply #4.

Oh I'm sorry Paul I didn't even see your second reply. I'm gonna work with it and let you know what comes up.

Just to clarify, the using the code in reply 4 the index will count up until the array is full. So if I put

char c[4];

in also it will receive the strings filling each variable?

I'm getting a lot of gibberish on my display and I believe I know why. I'm sending strings over the i2c and this is trying to receive them as arrays.

Ok, I'm truly stuck here. how does one say that the next six bytes that come across i2c need to be mapped into a character string. I'm getting massive compiler errors and I don't see why this is so difficult.

void receiveEvent(int howmany)       // function for receiving the data off of the bus
{
    char buffer[6];
    char* y[6];
    char* d[5];
    int s=Wire.receive();       
   
    boolean z=Wire.receive();        
    q=3;                  
    while(Wire.available()!='A')
    {
    y=Wire.receive();
     Serial.print(y);
    
    while(Wire.available()>0)
    {
      
    d=Wire.receive();
    
    }

I need 5 or 6 bytes to go into y and the rest of whats available to go into d. I've been trying to do this ALL DAY.

    while(Wire.available()!='A')

The byte read from the wire was compared to 'A' and discarded, regardless of whether it matched, or not. If it did not:

    y=Wire.receive();
     Serial.print(y);

you try to stuff a character into a variable whose type is array of pointer to char. They types don't even vaguely match.

I need 5 or 6 bytes to go into y and the rest of whats available to go into d.

How is the program to decide if it's 5 or 6? Flip a coin? Once you can answer this question, and you KNOW that wire.receive() returns one byte/char at a time, it is easy to store the characters that are retrieved from the wire in the right place.

@bsatterwhite
Have you read the example for two arduinos @ http://www.arduino.cc/en/Tutorial/MasterReader? If you have two boards you can redo this experiment.

Here a sample code for a wire master & slave based upon above example. it uses both the onReceive and onRequest event in the slave.
Slave

#include <Wire.h>

void setup()
{
  Serial.begin(9600); // start serial for output
  Serial.println("START SLAVE"); 

  Wire.begin(2); // join i2c bus with address #2
  Wire.onRequest(requestEvent); 
  Wire.onReceive(receiveEvent);
}

void loop()
{

}


void requestEvent()
{
  Wire.send("hello\n"); // = 6 chars
}


void receiveEvent(int count)
{
    // Serial.println(count);
    char y[6];
 
    for (int i = 0; i< count; i++)
    {
      char c = (char) Wire.receive(); // receive a character
      y[i] = c;  // and place it in array y at position i
      // Serial.print(c);
    }
    // Do something with array y 
}

And the master

#include <Wire.h>

void setup()
{
  Serial.begin(9600);
  Serial.println("START MASTER"); 
  Wire.begin();
}

void loop()
{
  // just add some text in Serial Monitor
  if (Serial.available())
  {
    // trigger onReceive at slave
    Wire.beginTransmission(2);
    while (Serial.available())
    {
      char c = Serial.read();  
      Wire.send(c);
    }
    Wire.endTransmission();
  }
  
  delay(500);
  
  // trigger onRequest at slave
  Wire.requestFrom(2, 6);   
  while(Wire.available())
  {
    char c = (char) Wire.receive(); // receive a byte
    Serial.print(c); // print the character
  }
}