Pages: [1]   Go Down
Author Topic: Using master_reader and slave_sender for multiple bytes  (Read 961 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm hoping to eventually be able to request a long variable from a slave arduino via I2C. Starting from scratch knowledge-wise, so I've been trying to gradually build on the master-reader and slave-sender examples from the IDE.

I've managed to be able to request a single binary number (byte) from the slave arduino, but I'm running into problems when I try and introduce a second.

byte b0 = B11001110;
byte b1 = B11101101;

Running the code below gives an output of:

b0:11101101   b1:11111111
b0:11101101   b1:11111111
b0:11101101   b1:11111111
b0:11101101   b1:11111111
...

Which is the value of b1 being printed as the value of b0.
I've no idea where the 11111111 has come from. (Where does the 11111111 come from?)

If I instead request a single byte, comment out the second Wire.receive(); in the master code, and also comment out either Wire.send(b0); or Wire.send(b1); in the slave code, then the output gives the correct values.

Where am I going wrong when adding in the second byte? Can somebody please explain what's happening for me (and I'd also be grateful for help in putting in right!)?

Thanks.

Master (reader) code:

Code:
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

byte b0 = 0;
byte b1 = 0;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{

Wire.requestFrom(2, 2);    // request bytes from slave device #2

if (Wire.available()) {
b0 = Wire.receive(); // receive a byte
Serial.print("b0:" ); Serial.print(b0, BIN); Serial.print("\t"); // print b0
  }

if (Wire.available()) {
b1 = Wire.receive(); // receive a byte
Serial.print("b1:" ); Serial.println(b1, BIN); // print b1
  }

delay(500);
}

Slave (sender) code:

Code:
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

// 0001 1111 0100 0010 1110 1101 1100 1110 example long I'm eventually hoping to send

byte b0 = B11001110;
byte b1 = B11101101;


void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
 
  Wire.send(b0);
  Wire.send(b1);
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the slave requestEvent you can only (successfully) do one send. However that send can be any reasonable length (ie. up to 32 bytes).

So change it to something along the lines of:

Code:
void requestEvent()
{

byte buf [2];

  buf [0] = b0;
  buf [1] = b1;
 
  Wire.send(buf, sizeof buf);
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the belated reply, but thanks Nick - it works with that approach.

Next step is to add a second slave arduino into the mix...

Would I be right in thinking I just repeat the section of code on the master, but change the address I'm making it to?
Something like:

Code:
Wire.requestFrom(2, 2);    // request bytes from slave device #2

if (Wire.available()) {
b0 = Wire.receive(); // receive a byte
Serial.print("b0:" ); Serial.print(b0, BIN); Serial.print("\t"); // print b0
  }

if (Wire.available()) {
b1 = Wire.receive(); // receive a byte
Serial.print("b1:" ); Serial.println(b1, BIN); // print b1
  }

Wire.requestFrom(2, 3);    // request bytes from slave device #3

if (Wire.available()) {
c0 = Wire.receive(); // receive a byte
Serial.print("c0:" ); Serial.print(c0, BIN); Serial.print("\t"); // print c0
  }

if (Wire.available()) {
c1 = Wire.receive(); // receive a byte
Serial.print("c1:" ); Serial.println(c1, BIN); // print c1
  }
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Apart from the fact that you have the address and number of bytes backwards, yes:

From the documentation:

Quote
Wire.requestFrom(address, quantity);
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ha! Good catch!
That'll teach me to try and sneakily do these things while I'm at work...
Logged

Pages: [1]   Go Up
Jump to: