Send/Read more variables using I2C (SOLVED!!!)

Hello! It's about this situation: I have 2 (in the future more than 2) Atmega328. One has the main program; the other I want to use it to colect data (digital/analogical) and to send the information to the main chip; there are more than 8 parameters to send.

Than, after the master chip precesses the information, it will send to the slave an order to drive up some pins. And so on...

I've searched for information that could help me, but I didn't find anything cogent.

Could somebody help me to do this?

Thank you!

Maybe im misunderstanding the question , are you having problems passing multiple Values VIA I2C ? :)

I don't know how to do this! Would anyone wants to explain me, PLEASE?

Have you done any of te Wire Library Tutotirals ? http://arduino.cc/en/Tutorial/MasterWriter We will help and much as we can , But we have to have a starting point :)

Yes, I do.

What I don't know is how to send/receive more than one variable. I need to send to the master the state of some digital pins and the values from some analogical pins; the master will do some calculations and then will sent to the slave what pins needs to be HIGH/LOW or some analogical out... depending the results from the calculations. So, the starting point needs to be the point where you send/receive the second variable, isn't it? :)

No not realy But ill show you this and see what goesd from there
Master

  Wire.beginTransmission(2);
  Wire.write(vin);
  Wire.write(vin1);
  Wire.write(Direction);
  Wire.write(Limit);
  Wire.endTransmission();

Slave

  vin = Wire.read();    
  vin1 = Wire.read();    
  Direction = Wire.read();
  Limit = Wire.read();

As I see, it matters the order of the sent data. If the slave will assign the values received to "a" and to "b" and the master sends first the state of the pin 5 and then the state of the pin 6, the result will be like this: a=pinState5, b=pinState6. If the master will send first the state of the pin 6, the result will be this: a=pinState6, b=pinState5. Am I understood right?

Well Hmm... Lets say you have a Value read on the master Unit Example -Voltage in- . THe Master unit take that Value asigns it a Varable (vin = 5) Take that Value the sends it VIA I2C (Wire.write(vin);) To a given address ( address x) . Then you would have Slave (address x) recieve that data (vin = Wire.read(); ) . And at that point you could do anything you want. Example

  if (vin == 5) {
    digitalWrite(LED_1, HIGH);
    delay(200);
    digitalWrite(LED_1, LOW);
    delay(200);
  }

of course there will be other code just like any other Sketch , but that will be the basics,ow and order of data sent isnt important,its all send at one time

OK.

I will put this in practice and I will let you know if my problem is solved.

THANK YOU VERY MUCH!!! :)

I have a problem when I try to read from a slave.

How could I send more values from a slave and receive correctly in the master? Now I can read only receive just ONE value. :roll_eyes: Where is the problem?

lets see your code

Here it is:
Slave:

#include <Wire.h>

void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
}
byte x=5;
byte y=6;
byte z=7;
void loop()
{
delay(100);
}

void requestEvent()
{
Wire.write(x);

Wire.write(y);
Wire.write(z);
}

Master:
#include <Wire.h>

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

void loop()
{
Wire.requestFrom(2, 3);

byte a = Wire.read();
Serial.print(a);
byte b = Wire.read();
Serial.print(b);
byte c = Wire.read();
Serial.print(c);

delay(500);
}

Anyone... pleeeeease! :blush:

slave

#include <Wire.h> //starts the library
#define LED_2 11
int x = 0; // give a value to the varable to be change with I/Os
int y = 0;

void setup() {
 // Serial.begin(9600); // used to debug
  Wire.begin(2); // Start I2C Bus as a Slave (Device Number 9)
  Wire.onReceive(receiveEvent); // register event
   pinMode(LED_2, OUTPUT);
    digitalWrite(LED_2, LOW);
}

void loop ()
{
    if (x == 1) {
    digitalWrite(LED_2, HIGH);
    delay(200);
    digitalWrite(LED_2, LOW);
    delay(200);
  }
}

void receiveEvent(int howMany)
{
   x = Wire.read();    // receive byte as an integer
 y = Wire.read();    // receive byte as an integer
}

Master

#include <Wire.h> //starts the library
int INRead = A0;
int x = 0; // give a value to the varable to be change with I/Os
int y = 0;

void setup() {

  pinMode(13, OUTPUT);
  pinMode(INRead, INPUT);

  Wire.begin(1);
}
void loop(){
{
  x = analogRead(INRead);
}

  Wire.beginTransmission(2);
  Wire.write(x);

  Wire.endTransmission();
}

I just wrote this - so take it for whats its worth :slight_smile:
There is alot left out , youll have to add your own code from there

I've done this part. It works good (for now).

My problem is when I want to read FROM a slave (the slave sends data to the master) MORE than 1 variable!!!! I don't know how to build the code to read all the variables is sending the slave. With 1 variable, works; when I send more than 1, it (the master) reads just the last one. How do I solve this??

Thank you!

change your byte to int

Nope! Doesn’t works!
This is the code for MASTER:

#include <Wire.h>

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

int a;
int b;

void loop()
{
Wire.requestFrom(2, 2);

while(Wire.available())
{
a=Wire.read();
Serial.println(a);
b=Wire.read();
Serial.println (b);
}

delay(500);
}


And this is the code for SLAVE:

#include <Wire.h>

void setup()
{
Wire.begin(2);
Wire.onRequest(requestEvent);
}
int x=100;
int y=80;

void loop()
{
delay(100);
}

void requestEvent()
{
Wire.write(x);
Wire.write(y);

}

And this is what I receive:

255
80
255
80
255
80
… and so on

:frowning:

#include <Wire.h>

void setup()
{
  Wire.begin(2);        
  Serial.begin(9600);
Wire.onReceive(receiveEvent); // register event  
}

int a = 0;
int b = 0;

void loop()
{
}
 // Wire.requestFrom(2, 2);    
void receiveEvent(int howMany)
  //while(Wire.available())    
  { 
    a=Wire.read(); 
    Serial.println(a);    
    b=Wire.read();
    Serial.println (b); 
 delay(500);   
  }
#include <Wire.h>

void setup()
{
  Wire.begin(2);                
 // Wire.onRequest(requestEvent); 
}
int x=100;
int y=80;

void loop()
{
  delay(100);



//void requestEvent()
Wire.beginTransmission(2);
  Wire.write(x); 
  Wire.write(y);
 Wire.endTransmission();                      
}

Sorry i modded your code

But this code is for Master sender This is not my problem! My problem is for MASTER READER / SLAVE SENDER with more variables. With one, yes it works. Anyway, thank you very much for your interest in my problem.

So, anyone knows how to send more than 1 variable from a slave (slave - sender) to a master (master - reader) via I2C???

so im guessing you didnt try it ,, sence its sending 100 and 80 is that not what u wanted ?