Wire.send String problem

Hi
I am trying out the wire.h library for the first time. I have 2 Arduinos. I configured one as a master and other as slave. Sample programs in the library works. I want to send 10bits of info like "1010101100" from slave to master wen it request for it. so i give like

 Wire.requestFrom(1, 10); // 1 is the address of slave and I need 10 bytes of data from slave.
      while(Wire.available())    // slave may send less than requested
      { 
        char c = Wire.receive(); // receive a byte as character
        Serial.print(c);         // print the character
      }

Now problem is how do i send this data to Master using wire.send from the slave side? which datatype should I use at slave side to send a info like 1010101100 to master. String object or byte datatype? I cant find any examples showing how this is done in the Reference section just syntax. :frowning:

void requestEvent()
{
  Wire.send();       // What should i give inside the brackets of wire.send at the slave side? 
}

Please do help me with some examples on how to proceed with sending this data. :slight_smile:
Thanx in advance :smiley:

I want to send 10bits of info like "1010101100"

Where do these 10 bits of info come from? Using bitSet() or bitWrite(), you could pack the 10 bits into two bytes, which the Wire library knows how to send.

Where do these 10 bits of info come from?

I am supposedly giving it explicitly. These are basically High/Low status (High-1 Low-0) of 10 digital pins. I wanted to transmit this info over to master. Not getting how to do it tough... :frowning:

int pinStates = 0;
for(int i=0; i<10; i++)
{
   int pinState = digitalRead(pinNum[i]);
   bitWrite(pinStates, i, pinState);
}

After this code, pinStates is an int (a 2 byte value) containing a number of bits of interest.

Sending this two byte int, using lowByte(pinStates) and highByte(pinStates), as two bytes using the Wire library should be trivial.

Thanx for the help. This should work I guess. lemme try it out :slight_smile:

I tried the technique you told.
Following is the code

int pinNum[8]={
  0,0,0,0,0,0,1,1};

void setup() {
  Serial.begin(9600);
}

void loop()
{
  int pinStates = 0;
  for(int i=0,j=7; j<0,i<8; i++,j--)
  {
    bitWrite(pinStates, j, pinNum[i]);
  }
  Serial.println(pinStates,BIN);
  while(1){
  }
}

now problem is pinStates stores this info as 11 and not as 00000011 although both mean same. some way to make it send as 00000011?

First off, pinNum is the array of pin numbers to read from. Using pins 0 and 1 at the same time you are doing Serial output is not smart.

some way to make it send as 00000011?

The problem isn't with sending the value as 00000011vs. sending it as 11, since the number being stored in memory is 3. You want the Serial Monitor to show the value as 00000011. This will require that you use sprintf to format the string to be printed, and then send that string to the serial monitor.

Of course, sending data to the serial monitor has nothing to do with sending it using the Wire library to another device, so why you want to do one thing after initially posting about the other isn't clear.

First off, pinNum is the array of pin numbers to read from.

I know. here I just initialised it with values just to see if it works.

now I am sending the data from slave as

int status_array[8]={
  1,0,0,0,0,0,1,0};   

void requestEvent()
{
  int status_send = 0;
  for(int i=0,j=7; j<=0,i<8; i++,j--)
  {
    bitWrite(status_send, j, status_array[i]);
  } 

  Wire.send(status_send);  
}

At the master side

Wire.requestFrom(1, 2); // 2 bytes of Info, 1 - address of slave
      while(Wire.available())    // slave may send less than requested
      { 
        int c = Wire.receive(); // receive a byte as character
        Serial.println(c);         // print the character
      }

But what I am getting at the Master is some random values and 255 as the 2nd byte.
Wat could be the problem? Is there any for slave to communicate with Master without calling for Request Form handler?

But what I am getting at the Master is some random values and 255 as the 2nd byte.
Wat could be the problem?

Perhaps you should have a look at the Wire.send() method. As I recall, it sends bytes, not ints.

Perhaps you should have a look at the Wire.send() method

Cant find suitable reading material on this from Reference section or google . :frowning:
Just cant seem to send the data as it is. I guessing its a data type problem but cant get it to fix it.
Someone here if you have worked with wire.h library. please do advice...

Tried this too...

Slave

void requestEvent()
{
  int status_send = 0;
  for(int i=0,j=7; j<=0,i<8; i++,j--)
  {
    bitWrite(status_send, j, status_array[i]);
  } 
    
  Wire.send(0xff & status_send);  //  low byte
  Wire.send(status_send >> 8);    //  high byte
}

Master

Wire.beginTransmission(1);
      Wire.requestFrom(1, 2);
      if(Wire.available())    
      { 
        c = Wire.receive(); 
          Serial.println(c);       
      }

      if(Wire.available())    
      { 
        c |= Wire.receive() << 8;
       }
      Wire.endTransmission();

      Serial.println(c,BIN);
      delay(500);

Seems to get 0 always watever values I give for status_array
Could be the same problem like http://www.neufeld.newton.ks.us/electronics/?p=241&cpage=3#comment-27008

Any solution for this?? Please any help? :~

void requestEvent()
{
int status_send = 0;

// blah
    
  Wire.send(0xff & status_send);  //  low byte
  Wire.send(status_send >> 8);    //  high byte
}

There is an issue in the Wire library. On a requestEvent you can't send more than one reply. So you need to assemble the results into a single send.

For example:

Wire.send((byte *) &status_send, sizeof status_send);

You'll need to work out (at the receiving end) which byte came first, the high-order or the low-order. You could do it other ways, but you need to end up with a single Wire.send.

Thank you very much. That solved the problem. :wink:

I have one doubt. Is it possible for a slave to send data to the master without the master actually asking for it?
Hoping for a fast reply :slight_smile:

Is it possible for a slave to send data to the master without the master actually asking for it?

No. Slaves never initiate communication.

Quote
Is it possible for a slave to send data to the master without the master actually asking for it?
No. Slaves never initiate communication.

You could set up the slave to generate an interrupt signal to the master. When the master sees the interrupt it knows new data is available for it to go get. This is how a lot of I2C slave devices operate.

See my page here:

The "slave" can be a master as well. Thus either end can send to each other, whenever they want to. In either case an interrupt is generated.