Pages: [1]   Go Down
Author Topic: [SOLVED] I2C Coms multiple values  (Read 253 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi I am trying to send multiple things over I2C (only x and Trims at the moment) when input 3 on the master is high  
x will end up being a value between 1 and 101, however just for testing it will be 0, 3, 4 over I2C and then can be set to 10 with the master.
Trims will be a value between 0 and 255 from an analog input 0 on the slave
 
I can get both of them working by them selves however when I try to do both at once only one will come through.

I have the following code

Master
Code:
// Master
#include <Wire.h>

int x=0;
int Trims=0;

void setup()
{
  Serial.begin(9600);  
  Wire.begin();  // Start I2C as Master
}

void loop()
{  
  if(digitalRead(3)==HIGH){
    Wire.requestFrom(5,4);  //Also not sure how many bits I should be requesting
    x = Wire.read();
    //Trims = Wire.read();
  }
  
  Serial.print("x = ");
  Serial.println(x);
  //Serial.print("Trims = ");
  //Serial.println(Trims);
  
  if(digitalRead(3)==LOW) x=10;
  
  delay(500);
  
}

Slave
Code:
// Slave
#include <Wire.h>

int x=0;
int Trims=0;


void setup()
{
  pinMode(3, INPUT);
  pinMode(4, INPUT);
    
  Wire.begin(5);  // Start I2C as slave with address 5  
  Wire.onRequest(RequestData);
}

void loop()
{
  Trims = analogRead(0);
  map(Trims, 0, 1024, 0, 255);
  constrain(Trims, 0, 255);
  
  if(digitalRead(3)==HIGH) x=3;
  else if(digitalRead(4)==HIGH) x=4;
  else x=0;
    
  delay(100);
  
}

void RequestData()
{
 Wire.write(x);
 //Wire.write(Trims);
}

All I have been doing to include or remove x or Trims is add or remove // where needed.

Not sure if I am going about this the right way or not.
Any help with this would be appreciated.
« Last Edit: August 01, 2013, 04:22:50 am by grantastley » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
//Also not sure how many bits I should be requesting

a typical int is 2 bytes
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So if I am requesting 2 ints it should be 4 bytes?

anyone know how to send to ints at the same time, or one straight after the other?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could I do this by sending an array of data? Something like
Code:
int I2Cdata[]={x, Trims}; then
Wire.write(I2Cdata[]);  // in void RequestData ()
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never mind everyone.
I have managed to figure this out myself.

I used an array to send the data.  
Bellow is the code for anyone else wanting to know how to accomplish this.

Master
Code:
// Master
#include <Wire.h>

int x=0;
int Trims=0;
int I2Cdata[2];

void setup()
{
  Serial.begin(9600);
  
  Wire.begin();  // Start I2C as Master
}

void loop()
{
  
  if(digitalRead(3)==HIGH){
    Wire.requestFrom(5,4);
    
    I2Cdata[0]=Wire.read();
    I2Cdata[1]=Wire.read();
      
      x = I2Cdata[0];
      Trims = (I2Cdata[1]);
  }
  
  Serial.print("x = ");
  Serial.println(x);
  Serial.print("Trims = ");
  Serial.println(Trims);
  
  if(digitalRead(3)==LOW) x=10;
  
  delay(500);
  
}

Slave
Code:
// Slave
#include <Wire.h>

int x=0;
int Trims=0;

unsigned char I2Cdata[2];

void setup()
{
  pinMode(3, INPUT);
  pinMode(4, INPUT);
    
  Wire.begin(5);  // Start I2C as slave with address 5
  
  Wire.onRequest(RequestData);
}

void loop()
{
  Trims = analogRead(0);
  map(Trims, 0, 1024, 0, 255);
  constrain(Trims, 0, 255);
  
  if(digitalRead(3)==HIGH) x=3;
  else if(digitalRead(4)==HIGH) x=4;
  else x=6;
  
  I2Cdata[0] = x;
  I2Cdata[1] = Trims;
    
  delay(100);  
}

void RequestData()
{
 Wire.write(I2Cdata,2);
}

Cheers
Grant....
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12428
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well done,
now you figured out yourself  you will never forget smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: