Pages: [1] 2   Go Down
Author Topic: Wire.send String problem  (Read 3529 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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. smiley-sad

Code:
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. smiley
Thanx in advance smiley-grin
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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... smiley-sad
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried the technique you told.
Following is the code

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote
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.

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Code:
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

Code:
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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Perhaps you should have a look at the Wire.send() method
Cant find suitable reading material on this from Reference section or google . smiley-sad
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...
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tried this too...

Slave
Code:
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
Code:

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?  smiley-confuse
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:

Code:
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.

Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much. That solved the problem.  smiley-wink
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: