I2c read from Slave

Hello, ist there anybody who can help me?

I’d like to reade 2 Values from 0-100 ( J1 and J2) from a Slave Device (Arduino Nano)
On The Master ( Arduino Uno) There is runnig following code:

#include <Wire.h>

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

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

while(Wire.available())
{
int J1 = Wire.read();
Serial.println(J1);
}
delay(500);
}

on the Slave:

in the Setup part :
Wire.begin(2);
Wire.onRequest(requestEvent);

and after the loop part :

void requestEvent()
{
Wire.write(J1);
}

So for J1 this already work. But how can I do this with both ? J1 and J2 ?
Can I do this with a array? How is the best way?

Kind Regards
Martin

But how can I do this with both ? J1 and J2 ?

You ? could ? post ? ALL ? of ? your ? code ? so ? we ? can ? see ? what ? you ? are ? talking ? about.

You could use punctuation properly, too.

Hello,

for the Master device I've already posted the complete code.
For the Slave only the I2c important parts because the code is 18 pages long.

In the Slave code I have 2 Variables (J1 and J2) The values of them are between 0 and 100.
The Master should ask for them and print them in the Serial Monitor.

Currently I ony send the value of J1 to the Master and print it to Serial.
Regards

Nobody here who can help me how I can I request the values of 2 variables from a slave arduino via i2c?

Take a look at the code I posted in this thread

for reading registers from a DS1307.
The master should send similar messages to the Nano/slave, and the slave should respond accordingly - recognize its address, recognize the register(variable) being requested, and send the appropriate data.

thanks, but I do not understand that :frowning:

in the meantime I tried the following on the slave:

void requestEvent()
{
byte buf [2] = { J1,J2 };
Wire.write (buf, 2);
}

on the Master:
#include <Wire.h>
int Platine1 = 0;

void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
Wire.requestFrom(2,2);

while(Wire.available())
{
Platine1 = Wire.read();
Serial.println(Platine1);
}
delay(500);
}

the Serial.println now gives me following output of int variable “Platine1”:
Value of J1
Value of J2
Value of J1
Value of J2 …
(J1 and J2 is swiching)

So I already receive J1 and J2 but how can I assign there values to two new variables?
thanks for any help

Maybe this?

  while(Wire.available())    
   { 
    if (flag == 1){
     flag = 0;
     Platine1 = Wire.read(); 
     Serial.println(Platine1);   
     } 
   if (flag == 0){
     flag = 1;
     Platine0 = Wire.read(); 
     Serial.println(Platine0);   
     } 
}

Before I read your post I’d tried that:

Wire.requestFrom(2,2);
while(Wire.available())
{
Platine1J1 = Wire.read();
Platine1J2 = Wire.read();
}

Platine 1J1 is my J1 value from Slave and Platine 1J2 the J2 value .
Is this methode ok?

I give it out to Serial Monitor with :
Serial.println(String(Platine1J1)+"<-Platine1J1__Platine1J2->"+(Platine1J2));

So I can see J1 on the left and J2 on the right side.

The next step is to read the values out of 9 Arduino nanos:

//Master für Jalousiensteuerung 11.12.13

#include <Wire.h>
int Platine1J1 = 999;
int Platine1J2 = 999;
int Platine2J1 = 999;
int Platine2J2 = 999;
int Platine3J1 = 999;
int Platine3J2 = 999;
int Platine4J1 = 999;
int Platine4J2 = 999;
int Platine5J1 = 999;
int Platine5J2 = 999;
int Platine6J1 = 999;
int Platine6J2 = 999;
int Platine7J1 = 999;
int Platine7J2 = 999;
int Platine8J1 = 999;
int Platine8J2 = 999;
int Platine9J1 = 999;
int Platine9J2 = 999;

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

void loop()
{
Wire.requestFrom(1,2);
while(Wire.available())
{
Platine1J1 = Wire.read();
Platine1J2 = Wire.read();
}

Wire.requestFrom(2,2);
while(Wire.available())
{
Platine2J1 = Wire.read();
Platine2J2 = Wire.read();
}

Wire.requestFrom(3,2);
while(Wire.available())
{
Platine3J1 = Wire.read();
Platine3J2 = Wire.read();
}

Wire.requestFrom(4,2);
while(Wire.available())
{
Platine4J1 = Wire.read();
Platine4J2 = Wire.read();
}

Wire.requestFrom(5,2);
while(Wire.available())
{
Platine5J1 = Wire.read();
Platine5J2 = Wire.read();
}

Wire.requestFrom(6,2);
while(Wire.available())
{
Platine6J1 = Wire.read();
Platine6J2 = Wire.read();
}

Wire.requestFrom(7,2);
while(Wire.available())
{
Platine7J1 = Wire.read();
Platine7J2 = Wire.read();
}

Wire.requestFrom(8,2);
while(Wire.available())
{
Platine8J1 = Wire.read();
Platine8J2 = Wire.read();
}

Wire.requestFrom(9,2);
while(Wire.available())
{
Platine9J1 = Wire.read();
Platine9J2 = Wire.read();
}

Serial.println(String(Platine1J1)+"<-Platine1J1__Platine1J2->"+(Platine1J2));
Serial.println(String(Platine2J1)+"<-Platine2J1__Platine2J2->"+(Platine2J2));
Serial.println(String(Platine3J1)+"<-Platine3J1__Platine3J2->"+(Platine3J2));
Serial.println(String(Platine4J1)+"<-Platine4J1__Platine4J2->"+(Platine4J2));
Serial.println(String(Platine5J1)+"<-Platine5J1__Platine5J2->"+(Platine5J2));
Serial.println(String(Platine6J1)+"<-Platine6J1__Platine6J2->"+(Platine6J2));
Serial.println(String(Platine7J1)+"<-Platine7J1__Platine7J2->"+(Platine7J2));
Serial.println(String(Platine8J1)+"<-Platine8J1__Platine8J2->"+(Platine8J2));
Serial.println(String(Platine9J1)+"<-Platine9J1__Platine9J2->"+(Platine9J2));

delay(200);
}

I hope this will work

thanks to all

Why are you storing the bytes you get from Wire.read() in ints?

I suspect that that is related to your problem.